利用usart0,通过rs232,与pc通信,接收pc发送的命令,只有一个字符时,可以正常接收,当多个字符时,接收出错,有相关例子可供参考一下吗?谢谢
This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
利用usart0,通过rs232,与pc通信,接收pc发送的命令,只有一个字符时,可以正常接收,当多个字符时,接收出错,有相关例子可供参考一下吗?谢谢
串口每次只能接收并处理一个字符。如果有多个字符,处理方法是开个缓存,全部接收下来再慢慢处理。以下代码供参考
#include "include.h"
void Uart_Init()//串口初始化 9600波特率 8-N-1
{
U0CTL |= CHAR;//UART Mode,8-bits,1 stop bit,no parity
U0TCTL |= SSEL0;//Source select-ACLK
//波特率计算9600
U0BR1 = 0x00;
U0BR0 = 0x03;
U0MCTL = 0x4A;
UCTL0 &= ~SWRST;// Initialize USART state machine
ME1 |= UTXE0 + URXE0;//使能串口发送和接收
IE1 |= URXIE0;//开启串口接收中断使能
P3SEL |= BIT4 + BIT5;//开启IO口第二功能(作为串口收发端)
}
/*串口接收中断子程序*/
#pragma vector = USART0RX_VECTOR
__interrupt void UART_RX()
{
_DINT();
unsigned char tp_rx;
tp_rx = U0RXBUF;
if(!rx_finish)//如果一帧没接收完,继续接收
{
if(rx_enable)//如果接收允许
{
buf_gps[count_gps] = tp_rx; //接收
if(count_gps==4) get_gpsmode(); //接收到5个数据的时候,check mode
if(tp_rx=='*'){ rx_enable = 0; rx_finish = 1;} //接收到'*'的时候,一帧结束
else count_gps++;
}
else
{
if(tp_rx=='$'){ rx_enable = 1; count_gps = 0; rmc_gga = 0;}//如果接收到的字符是'$',则表示一帧的开始,使能串口接收
}
}
if(tp_rx=='#') upload_enable = 1; //收到'#',准备上传记录
_EINT();
}
void UART_TX(uchar tp_tx)
{
IE1 &= ~URXIE0;//关闭串口接收中断使能
U0TXBUF = tp_tx;//发送数据
while((IFG1 & UTXIFG0)==0);//等待发送完成
IE1 |= URXIE0;//开启串口接收中断使能
}