您的位置 首页 新能源

PIC C18自带串口程序

官方写的串口程序,值得学习一下!(串口无论接收还是发送都有轮询法和中断法)读一个字节#includep18cxxx.h>#includeusart.h>/*******…

官方写的串口程序,值得学习一下!
(串口不管接纳仍是发送都有轮询法和中止法)

读一个字节

#include
#include
/********************************************************************
* Function Name: ReadUSART *
* Return Value: char: received data *
* Parameters: void *
* Description: This routine reads the data from the USART *
* and records the status flags for that byte *
* in USART_Status (Framing and Overrun). *
********************************************************************/
#if defined (AUSART_V1) || defined (EAUSART_V3) || defined (EAUSART_V4) || defined (EAUSART_V5)
char ReadUSART(void) //this function can be removed by macro #define ReadUSART RCREG
{
char data; // Holds received data
USART_Status.val &= 0xf2; // Clear previous status flags
if(RCSTAbits.RX9) // If 9-bit mode
{
USART_Status.RX_NINE = 0; // Clear the receive bit 9 for USART
if(RCSTAbits.RX9D) // according to the RX9D bit
USART_Status.RX_NINE = 1;
}
if(RCSTAbits.FERR) // If a framing error occured
USART_Status.FRAME_ERROR = 1; // Set the status bit
if(RCSTAbits.OERR) // If an overrun error occured
USART_Status.OVERRUN_ERROR = 1; // Set the status bit
data = RCREG; // Read data
return (data); // Return the received data
}
#endif

获取字符串

#include
#include
/********************************************************************
* Function Name: getsUSART *
* Return Value: void *
* Parameters: buffer: pointer to string *
* len: length of characters to receive *
* Description: This routine receives a string of characters *
* from the USART of length specified by len. *
********************************************************************/
#if defined (AUSART_V1) || defined (EAUSART_V3) || defined (EAUSART_V4) || defined (EAUSART_V5)
void getsUSART(char *buffer, unsigned char len)
{
char i; // Length counter
unsigned char data;
for(i=0;i
{
while(!DataRdyUSART());// Wait for data to be received
data = getcUSART(); // Get a character from the USART =ReadUSART(void)
// and save in the string
*buffer = data;
buffer++; // Increment the string pointer
}
}
#endif

写一个字符

#include
#include
/********************************************************************
* Function Name: WriteUSART *
* Return Value: none *
* Parameters: data: data to transmit *
* Description: This routine transmits a byte out the USART. *
********************************************************************/
#if defined (AUSART_V1) || defined (EAUSART_V3) || defined (EAUSART_V4) || defined (EAUSART_V5)
void WriteUSART(char data)
{
if(TXSTAbits.TX9) // 9-bit mode?
{
TXSTAbits.TX9D = 0; // Set the TX9D bit according to the
if(USART_Status.TX_NINE) // USART Tx 9th bit in status reg
TXSTAbits.TX9D = 1;
}
TXREG = data; // Write the data byte to the USART
}
#endif
写一个字符串

#include
#include
/**********************************************************************
* Function Name: putsUSART *
* Return Value: void *
* Parameters: data: pointer to string of data *
* Description: This routine transmits a string of characters *
* to the USART including the null. *
**********************************************************************/
#if defined (AUSART_V1) || defined (EAUSART_V3) || defined (EAUSART_V4) || defined (EAUSART_V5)
void putsUSART( char *data)
{
do
{ // Transmit a byte
while(BusyUSART());
putcUSART(*data); //=WriteUSART
} while( *data++ );
}
#endif

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qiche/xinnengyuan/272420.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部