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

📄 sm1.c

📁 MC9S08DZ60的一个SPI实例!比较适合初学者阅读!
💻 C
📖 第 1 页 / 共 2 页
字号:
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_DISABLED - Device is disabled (only
**                           if output DMA is supported and enabled)
**                           ERR_TXFULL - Transmitter is full
** ===================================================================
*/
byte SM1_SendChar(SM1_TComData Chr)
{
  if (SerFlag & FULL_TX) {             /* Is any char in the TX buffer? */
    return ERR_TXFULL;                 /* If yes then error */
  }
  EnterCritical();                     /* Save the PS register */
  BufferWrite = Chr;                   /* Store char to the temporary variable */
  if(EnUser) {                         /* Is the device enabled by user? */
    SPID = Chr;                        /* Store char to transmitter register */
  }
  SerFlag |= FULL_TX;                  /* Set the flag "full TX buffer" */
  ExitCritical();                      /* Restore the PS register */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  SM1_CharsInRxBuf (bean SynchroMaster)
**
**     Description :
**         Returns the number of characters in the input buffer.
**         Note: If the Interrupt service is disabled, and the
**         Ignore empty character is set to yes, and a character has
**         been received, then this method returns 1 although it was
**         an empty character.
**         (deprecated method)
**     Parameters  :
**         NAME            - DESCRIPTION
**       * Chr             - A pointer to number of characters in
**                           the input buffer
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
** ===================================================================
*/
byte SM1_CharsInRxBuf(word *Chr)
{
  *Chr = (word)((SerFlag >> 3) & 1);   /* Return number of chars in receive buffer */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  SM1_GetCharsInRxBuf (bean SynchroMaster)
**
**     Description :
**         Returns the number of characters in the input buffer.
**         Note: If the Interrupt service is disabled, and the
**         Ignore empty character is set to yes, and a character has
**         been received, then this method returns 1 although it was
**         an empty character.
**     Parameters  : None
**     Returns     :
**         ---             - Number of characters in the input
**                           buffer.
** ===================================================================
*/
word SM1_GetCharsInRxBuf(void)
{
  return (word)((SerFlag >> 3) & 1);   /* Return number of chars in receive buffer */
}
/*
** ===================================================================
**     Method      :  SM1_CharsInTxBuf (bean SynchroMaster)
**
**     Description :
**         Returns the number of characters in the output buffer.
**         (deprecated method)
**     Parameters  :
**         NAME            - DESCRIPTION
**       * Chr             - A pointer to the number of characters
**                           in the output buffer
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
** ===================================================================
*/
byte SM1_CharsInTxBuf(word *Chr)
{
  *Chr = (word)((SerFlag >> 4) & 1);   /* Return number of chars in the transmit buffer */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  SM1_GetCharsInTxBuf (bean SynchroMaster)
**
**     Description :
**         Returns the number of characters in the output buffer.
**     Parameters  : None
**     Returns     :
**         ---             - Number of characters in the output
**                           buffer.
** ===================================================================
*/
word SM1_GetCharsInTxBuf(void)
{
  return (word)((SerFlag >> 4) & 1);   /* Return number of chars in the transmit buffer */
}
/*
** ===================================================================
**     Method      :  SM1_GetError (bean SynchroMaster)
**
**     Description :
**         Returns a set of errors on the channel (errors that
**         cannot be returned in given methods). The errors
**         accumulate in a set; after calling [GetError] this set is
**         returned and cleared.
**     Parameters  :
**         NAME            - DESCRIPTION
**       * Err             - A pointer to the returned set of errors
**     Returns     :
**         ---             - Error code (if GetError did not succeed),
**                           possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
** ===================================================================
*/
byte SM1_GetError(SM1_TError *Err)
{
  EnterCritical();                     /* Save the PS register */
  Err->err = 0;
  Err->errName.OverRun = ((ErrFlag & OVERRUN_ERR) != 0); /* Overrun error */
  ErrFlag = 0x00;                      /* Reset error flags */
  ExitCritical();                      /* Restore the PS register */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  SM1_Interrupt (bean SynchroMaster)
**
**     Description :
**         The method services the error interrupt of the selected 
**         peripheral(s) and eventually invokes the bean's event(s).
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
#define ON_ERROR    0x01
#define ON_FULL_RX  0x02
#define ON_RX_CHAR  0x04
#define ON_FREE_TX  0x08
#define ON_TX_CHAR  0x10
ISR(SM1_Interrupt)
{
  SM1_TComData Data;                   /* Temporary variable for data */
  byte Flags = 0;                      /* Temporary variable for flags */
  byte Status;                         /* Temporary variable for flags */

  Status = SPIS;                       /* Read the device error register */
  Data = SPID;                         /* Read data from receiver */
  if (SerFlag & CHAR_IN_RX) {          /* Is the overrun error flag set? */
    SerFlag |= OVERRUN_ERR;            /* If yes then set the OnError flag */
    Flags |= ON_ERROR;                 /* If yes then set the OnError flag */
  }
  SerFlag |= CHAR_IN_RX;               /* Set flag "char in RX buffer" */
  BufferRead = Data;                   /* Read data from receiver */
  ErrFlag |= SerFlag;                  /* Update error flag mirror */
  SerFlag &= ~FULL_TX;                 /* Reset flag "full TX buffer" */
  if(Flags & ON_ERROR) {               /* Is the error flag set? */
    SM1_OnError();                     /* If yes then invoke user event */
  }
  else {
    SM1_OnRxChar();                    /* If yes then invoke user event */
    SM1_OnRxCharExt(Data);             /* If yes then invoke user event */
  }
  SM1_OnTxChar();                      /* If yes then invoke user event */
}

/*
** ===================================================================
**     Method      :  SM1_Init (bean SynchroMaster)
**
**     Description :
**         Initializes the associated peripheral(s) and the bean internal 
**         variables. The method is called automatically as a part of the 
**         application initialization code.
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
void SM1_Init(void)
{
  SerFlag = 0;                         /* Reset all flags */
  ErrFlag = 0;                         /* Reset all flags in mirror */
  EnUser = TRUE;                       /* Enable device */
  #pragma MESSAGE DISABLE C4002        /* Disable warning C4002 "Result not used" */
  (void)SPIS;                          /* Read the status register */
  (void)SPID;                          /* Read the device register */
  /* SPIBR: ??=0,SPPR2=1,SPPR1=0,SPPR0=0,??=0,SPR2=0,SPR1=1,SPR0=1 */
  setReg8(SPIBR, 0x43);                /* Set the baud rate register */ 
  /* SPIC2: ??=0,??=0,??=0,MODFEN=0,BIDIROE=0,??=0,SPISWAI=0,SPC0=0 */
  setReg8(SPIC2, 0x00);                /* Configure the SPI port - control register 2 */ 
  /* SPIC1: SPIE=0,SPE=0,SPTIE=0,MSTR=1,CPOL=0,CPHA=1,SSOE=0,LSBFE=1 */
  setReg8(SPIC1, 0x15);                /* Configure the SPI port - control register 1 */ 
  HWEnDi();                            /* Enable/disable device according to the status flags */
}


/* END SM1. */

/*
** ###################################################################
**
**     This file was created by UNIS Processor Expert 3.01 [03.92]
**     for the Freescale HCS08 series of microcontrollers.
**
** ###################################################################
*/

⌨️ 快捷键说明

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