⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sm1.c

📁 MC9S08DZ60的一个SPI实例!比较适合初学者阅读!
💻 C
📖 第 1 页 / 共 2 页
字号:
/** ###################################################################
**     THIS BEAN MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.
**     Filename  : SM1.C
**     Project   : Project
**     Processor : MC9S08DZ60MLF
**     Beantype  : SynchroMaster
**     Version   : Bean 02.270, Driver 01.18, CPU db: 2.87.131
**     Compiler  : CodeWarrior HCS08 C Compiler
**     Date/Time : 2008-12-10, 上午 09:28
**     Abstract  :
**         This bean "SynchroMaster" implements MASTER part of synchronous
**         serial master-slave communication.
**     Settings  :
**         Synchro type                : MASTER
**
**         Serial channel              : SPI
**
**         Protocol
**             Init baud rate          : 0_100MHz
**             Clock edge              : rising
**             Width                   : 8 bits (always)
**             Empty character         : 0
**             Empty char. on input    : RECEIVED
**
**         Registers
**             Input buffer            : SPID      [$0055]
**             Output buffer           : SPID      [$0055]
**             Control register        : SPIC1     [$0050]
**             Mode register           : SPIC2     [$0051]
**             Baud setting reg.       : SPIBR     [$0052]
**
**             Priority                : 
**
**             Priority                : 
**
**         Used pins                   :
**         ----------------------------------------------------------
**              Function    | On package |    Name
**         ----------------------------------------------------------
**               Input      |     16     |  PTE5_SDA_MISO
**               Output     |     15     |  PTE4_SCL_MOSI
**               Clock      |     14     |  PTE3_SPSCK
**         ----------------------------------------------------------
**
**     Contents  :
**         Enable          - byte SM1_Enable(void);
**         Disable         - byte SM1_Disable(void);
**         RecvChar        - byte SM1_RecvChar(SM1_TComData *Chr);
**         SendChar        - byte SM1_SendChar(SM1_TComData Chr);
**         CharsInRxBuf    - byte SM1_CharsInRxBuf(word *Chr);
**         GetCharsInRxBuf - word SM1_GetCharsInRxBuf(void);
**         CharsInTxBuf    - byte SM1_CharsInTxBuf(word *Chr);
**         GetCharsInTxBuf - word SM1_GetCharsInTxBuf(void);
**         GetError        - byte SM1_GetError(SM1_TError *Err);
**
**     (c) Copyright UNIS, spol. s r.o. 1997-2006
**     UNIS, spol. s r.o.
**     Jundrovska 33
**     624 00 Brno
**     Czech Republic
**     http      : www.processorexpert.com
**     mail      : info@processorexpert.com
** ###################################################################*/


/* MODULE SM1. */

#include "SM1.h"
#include "Events.h"


/* Internal method prototypes */

#define OVERRUN_ERR      0x01          /* Overrun error flag bit   */
#define CHAR_IN_RX       0x08          /* Char is in RX buffer     */
#define FULL_TX          0x10          /* Full transmit buffer     */
#define RUNINT_FROM_TX   0x20          /* Interrupt is in progress */
#define FULL_RX          0x40          /* Full receive buffer      */

static bool EnUser;                    /* Enable/Disable SPI */
static byte SerFlag;                   /* Flags for serial communication */
                                       /* Bits: 0 - OverRun error */
                                       /*       1 - Unused */
                                       /*       2 - Unused */
                                       /*       3 - Char in RX buffer */
                                       /*       4 - Full TX buffer */
                                       /*       5 - Running int from TX */
                                       /*       6 - Full RX buffer */
                                       /*       7 - Unused */
static byte ErrFlag;                   /* Error flags mirror of SerFlag */
static SM1_TComData BufferRead;        /* Input char SPI commmunication */
static SM1_TComData BufferWrite;       /* Output char SPI commmunication */

/*
** ===================================================================
**     Method      :  HWEnDi (bean SynchroMaster)
**
**     Description :
**         Enables or disables the peripheral(s) associated with the bean.
**         The method is called automatically as a part of the Enable and 
**         Disable methods and several internal methods.
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
static void HWEnDi(void)
{
  if (EnUser) {                        /* Enable device? */
    SPIC1_SPE = 1;                     /* Enable device */
    SPIC1_SPIE = 1;                    /* Enable interrupts */
    if (SerFlag & FULL_TX) {           /* Is any char in transmit buffer? */
      SPID = BufferWrite;              /* Store char to transmitter register */
    }
  }
  else {
    SPIC1_SPE = 0;                     /* Disable device */
    SPIC1_SPIE = 0;                    /* Disable interrupts */
  }
}

/*
** ===================================================================
**     Method      :  SM1_Enable (bean SynchroMaster)
**
**     Description :
**         Enable the bean - it starts send and receive functions.
**         Events may be generated ("DisableEvent"/"EnableEvent").
**     Parameters  : None
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
** ===================================================================
*/
byte SM1_Enable(void)
{
  if (!EnUser) {                       /* Is the device disabled by user? */
    EnUser = TRUE;                     /* If yes then set the flag "device enabled" */
    HWEnDi();                          /* Enable the device */
  }
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  SM1_Disable (bean SynchroMaster)
**
**     Description :
**         Disable the bean - it stops the send and receive
**         functions. No events will be generated.
**     Parameters  : None
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
** ===================================================================
*/
byte SM1_Disable(void)
{
  if (EnUser) {                        /* Is the device enabled by user? */
    EnUser = FALSE;                    /* If yes then set the flag "device disabled" */
    HWEnDi();                          /* Disable the device */
  }
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  SM1_RecvChar (bean SynchroMaster)
**
**     Description :
**         If any data is received, this method returns one
**         character, otherwise it returns an error code (it does
**         not wait for data).
**     Parameters  :
**         NAME            - DESCRIPTION
**       * Chr             - A pointer to the received character
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_RXEMPTY - No data in receiver
**                           ERR_OVERRUN - Overrun error was detected
**                           from the last char or block received. In
**                           polling mode, this error code is
**                           returned only when the hardware supports
**                           detection of the overrun error. 
**                           ERR_FAULT - Fault error was detected
**                           from the last char or block received.
**                           This error may not be supported on some
**                           CPUs (see generated code).
** ===================================================================
*/
byte SM1_RecvChar(SM1_TComData *Chr)
{
  byte FlagTmp;

  if (!(SerFlag & CHAR_IN_RX)) {       /* Is any char in RX buffer? */
    return ERR_RXEMPTY;                /* If no then error */
  }
  EnterCritical();                     /* Save the PS register */
  *Chr = BufferRead;                   /* Read the char */
  FlagTmp = SerFlag;                   /* Safe the flags */
  SerFlag &= ~(OVERRUN_ERR | CHAR_IN_RX | FULL_RX); /* Clear flag "char in RX buffer" */
  ExitCritical();                      /* Restore the PS register */
  if ((FlagTmp & OVERRUN_ERR) != 0) {  /* Has the overrun occured? */
    return ERR_OVERRUN;                /* If yes then return error */
  } else {
    return ERR_OK;
  }
}

/*
** ===================================================================
**     Method      :  SM1_SendChar (bean SynchroMaster)
**
**     Description :
**         Sends one character to the channel.
**     Parameters  :
**         NAME            - DESCRIPTION
**         Chr             - Character to send
**     Returns     :

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -