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

📄 as1.c

📁 FREESCALE 16位单片机片MC9S12DG128的串口驱动程序,环行缓冲区大小可设置,支持半双工RS485通信,可调用用户事件
💻 C
📖 第 1 页 / 共 2 页
字号:
{
  word count;                          /* Number of sent chars */
  byte result = ERR_OK;                /* Last error */

  if(!Size)                            /* Test variable Size on zero */
    return ERR_OK;                     /* If zero then OK */
  for(count = 0; count < Size; count++) {
    result = AS1_SendChar(*Ptr++);
    if(result != ERR_OK) {             /* Sending given number of chars */
      *Snd = count;                    /* Return number of sent chars */
      return result;                   /* Return last error */
    }
  }
  *Snd = count;                        /* Return number of sended chars */
  return result;                       /* OK */
}

/*
** ===================================================================
**     Method      :  AS1_ClearRxBuf (bean AsynchroSerial)
**
**     Description :
**         Clear receive buffer.
**         This method is available only if non-zero lenght of the
**         input buffer is defined and the receiver property is
**         enabled.
**     Parameters  : None
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
** ===================================================================
*/
byte AS1_ClearRxBuf(void)
{
  EnterCritical();                     /* Save the PS register */
  InpLen = 0;                          /* Set number of chars in the transmit buffer to 0 */
  InpPtrR = InpPtrW = InpBuffer;       /* Set pointers on the first item in the transmit buffer */
  ExitCritical();                      /* Restore the PS register */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  AS1_ClearTxBuf (bean AsynchroSerial)
**
**     Description :
**         Clear transmit buffer.
**         This method is available only if non-zero lenght of the
**         output buffer is defined and the receiver property is
**         enabled.
**     Parameters  : None
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
** ===================================================================
*/
byte AS1_ClearTxBuf(void)
{
  EnterCritical();                     /* Save the PS register */
  OutLen = 0;                          /* Set number of chars in the receive buffer to 0 */
  OutPtrR = OutPtrW = OutBuffer;       /* Set pointers on the first item in the receive buffer */
  ExitCritical();                      /* Restore the PS register */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  AS1_GetCharsInRxBuf (bean AsynchroSerial)
**
**     Description :
**         Return number of characters in the input buffer. This
**         method is available only if the receiver property is
**         enabled.
**     Parameters  : None
**     Returns     :
**         ---             - Number of characters in the input
**                           buffer.
** ===================================================================
*/
word AS1_GetCharsInRxBuf(void)
{
  return InpLen;                       /* Return number of chars in receive buffer */
}

/*
** ===================================================================
**     Method      :  AS1_GetCharsInTxBuf (bean AsynchroSerial)
**
**     Description :
**         Return number of characters in the output buffer. This
**         method is available only if the transmitter property is
**         enabled.
**     Parameters  : None
**     Returns     :
**         ---             - Number of characters in the output
**                           buffer.
** ===================================================================
*/
word AS1_GetCharsInTxBuf(void)
{
  return OutLen;                       /* Return number of chars in the transmitter buffer */
}

/*
** ===================================================================
**     Method      :  AS1_InterruptRx (bean AsynchroSerial)
**
**     Description :
**         This method is internal. It is used by Processor Expert
**         only.
** ===================================================================
*/
#define ON_ERROR    1
#define ON_FULL_RX  2
#define ON_RX_CHAR  4
#pragma INLINE
void AS1_InterruptRx(void)
{
  AS1_TComData Data;                   /* Temporary variable for data */
  byte OnFlags = 0;                    /* Temporary variable for flags */

  SCI0SR1;                             /* Dummy read of SCS1 register - clear the SCRF bit */
  Data = SCI0DRL;                      /* Read data from the receiver */

  
  if(InpLen < 80) 
  {                 				 /* Is number of bytes in the receive buffer lower than size of buffer? */
	  InpLen++;                          /* Increse number of chars in the receive buffer */
  
	  //若收到帧结束符
	  if(Data == 0x0D) 
	  {
	      DataFrameFlag =1; 
	      //一帧数据长度
	      DataFramLen =  AS1_GetCharsInRxBuf();
	  }
	  
	    *(InpPtrW++) = Data;               /* Save received char to the receive buffer */
	    if(InpPtrW >= InpBuffer + 80)     /* Is the pointer out of the receive buffer? */
	      InpPtrW = InpBuffer;             /* Set pointer on the first item into the receive buffer */
	    OnFlags |= ON_RX_CHAR;             /* Set flag "OnRXChar" */
	    if(InpLen== 80)                   /* Is number of bytes in the receive buffer equal as a size of buffer? */
	      OnFlags |= ON_FULL_RX;           /* If yes then set flag "OnFullRxBuff" */
  }
  else 
  {
	    SerFlag |= FULL_RX;                /* If yes then set flag buffer overflow */
	    OnFlags |= ON_ERROR;               /* Set flag "OnError" */
  }

  
  if(OnFlags & ON_ERROR) 				/* Was error flag detect? */
  {             					
  		AS1_OnError();                     /* If yes then invoke user event */
  }
  else 
  {
	    if(OnFlags & ON_RX_CHAR) 			/* Is OnRxChar flag set? */
	    {         
	      AS1_OnRxChar();                  /* If yes then invoke user event */
	    }
	    if(OnFlags & ON_FULL_RX) 			/* Is OnFullRxBuf flag set? */
	    {         
	      AS1_OnFullRxBuf();               	/* If yes then invoke user event */
	    }
  }
  
}

/*
** ===================================================================
**     Method      :  AS1_InterruptTx (bean AsynchroSerial)
**
**     Description :
**         This method is internal. It is used by Processor Expert
**         only.
** ===================================================================
*/
#define ON_FREE_TX  1
#define ON_TX_CHAR  2
#pragma INLINE
void AS1_InterruptTx(void)
{
  byte OnFlags = 0;                    /* Temporary variable for flags */

  if(SerFlag & RUNINT_FROM_TX)         /* Is flag "running int from TX" set? */
    OnFlags |= ON_TX_CHAR;             /* Set flag "OnTxChar" */
  SerFlag &= ~RUNINT_FROM_TX;          /* Reset flag "running int from TX" */
  if(OutLen) {                         /* Is number of bytes in the transmit buffer greater then 0? */
    OutLen--;                          /* Decrease number of chars in the transmit buffer */
    SerFlag |= RUNINT_FROM_TX;         /* Set flag "running int from TX"? */
    SCI0SR1;                           /* Reset interrupt request flag */
    SCI0DRL = (byte)*(OutPtrR++);      /* Store char to transmitter register */
    if(OutPtrR >= OutBuffer + 80)     /* Is the pointer out of the transmit buffer? */
      OutPtrR = OutBuffer;             /* Set pointer on the first item into the transmit buffer */
  }
  else {
    OnFlags |= ON_FREE_TX;             /* Set flag "OnFreeTxBuf" */
    SCI0CR2_SCTIE = 0;                 /* Disable transmit interrupt */
  }
  if(OnFlags & ON_TX_CHAR) {           /* Is flag "OnTxChar" set? */
    AS1_OnTxChar();                    /* If yes then invoke user event */
  }
  if(OnFlags & ON_FREE_TX) {           /* Is flag "OnFreeTxBuf" set? */
    AS1_OnFreeTxBuf();                 /* If yes then invoke user event */
  }
}

/*
** ===================================================================
**     Method      :  AS1_InterruptError (bean AsynchroSerial)
**
**     Description :
**         This method is internal. It is used by Processor Expert
**         only.
** ===================================================================
*/
#pragma INLINE
void AS1_InterruptError(void)
{
  if(SCI0SR1_PF)                       /* Is parity error detected? */
    SerFlag |= PARITY_ERR;             /* If yes then set an internal flag */
  if(SCI0SR1_OR)                       /* Is overrun error detected? */
    SerFlag |= OVERRUN_ERR;            /* If yes then set an internal flag */
  if (SCI0SR1_FE)                      /* Is framing error detected? */
    SerFlag |= FRAMING_ERR;            /* If yes then set an internal flag */
  if(SCI0SR1_NF)                       /* Is noise error detected? */
    SerFlag |= NOISE_ERR;              /* If yes then set an internal flag */
  SCI0DRL;                             /* Dummy read of data register - clear error bits */
  if(SerFlag & (OVERRUN_ERR|FRAMING_ERR|PARITY_ERR|NOISE_ERR)) { /* Was any error set? */
    AS1_OnError();                     /* If yes then invoke user event */
  }
}

/*
** ===================================================================
**     Method      :  AS1_Interrupt (bean AsynchroSerial)
**
**     Description :
**         This method is internal. It is used by Processor Expert
**         only.
** ===================================================================
*/
#pragma CODE_SEG __NEAR_SEG NON_BANKED /* Interrupt section for this module. Placement will be in NON_BANKED area. */
__interrupt void AS1_Interrupt(void)
{
  if ((SCI0SR1_OR)||(SCI0SR1_FE)||(SCI0SR1_NF)||(SCI0SR1_PF)) /* Is any error flag set? */
    AS1_InterruptError();              /* If yes, then invoke the internal service routine. This routine is inlined. */
  if (SCI0SR1_RDRF)                    /* Is the receiver interrupt flag set? */
    AS1_InterruptRx();                 /* If yes, then invoke the internal service routine. This routine is inlined. */
  if (SCI0SR1_TC)                      /* Is the transmitter interrupt flag set? */
    AS1_InterruptTx();                 /* If yes, then invoke the internal service routine. This routine is inlined. */
}

#pragma CODE_SEG AS1_CODE              /* Code section for this module. */
/*
** ===================================================================
**     Method      :  AS1_Init (bean AsynchroSerial)
**
**     Description :
**         This method is internal. It is used by Processor Expert
**         only.
** ===================================================================
*/
void AS1_Init(void)
{
  SerFlag = 0;                         /* Reset flags */
  /* SCI0CR1: LOOPS=0,SCISWAI=0,RSRC=0,M=0,WAKE=0,ILT=0,PE=0,PT=0 */
  SCI0CR1 = 0;                         /* Set the SCI configuration */
  /* SCI0SR2: ??=0,??=0,??=0,??=0,??=0,BRK13=0,TXDIR=0,RAF=0 */
  SCI0SR2 = 0;                         /* Set the Break Character Length and Transmitter pin data direction in Single-wire mode */
  SCI0SR1;                             /* Reset interrupt request flags */
  InpLen = 0;                          /* No char in the receive buffer */
  InpPtrW = InpPtrR = InpBuffer;       /* Set pointer on the first item in the receive buffer */
  OutLen = 0;                          /* No char in the transmit buffer */
  OutPtrW = OutPtrR = OutBuffer;       /* Set pointer on the first item in the transmit buffer */
  /* SCI0CR2: SCTIE=0,TCIE=0,RIE=0,ILIE=0,TE=0,RE=0,RWU=0,SBK=0 */
  SCI0CR2 = 0;                         /* Disable error interrupts */
  SCI0BD = 52;                         /* Set prescaler bits */
  HWEnDi();                            /* Enable/disable device according to status flags */
  RS485_DE_RE = 0; 
}


/* END AS1. */


/*
** ###################################################################
**
**     This file was created by UNIS Processor Expert 03.33 for 
**     the Motorola HCS12 series of microcontrollers.
**
** ###################################################################
*/

⌨️ 快捷键说明

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