#include <cms.h> unsigned char UART_RX_COUNT; unsigned char UART_RX_BUFF[10]; void UART_INIT() //串口初始化 { SPBRG = 102; //设置波特率为9600 bps,误差0.16% SYNC = 0; //0为异步模式,1为同步模式 SCKP = 0; SPEN = 1; //允许串口操作 RCIE = 1; //接收中断 TXIE = 0; //发送中断 RX9EN = 0; //0为8位接收,1为9位接收 TX9EN = 0; //0为8位发送,1为9位发送 CREN = 1; //0为禁止接收,1为使能接收 TXEN = 1; //0为禁止发送,1为使能发送 } void UART_SEND_DATA(unsigned char DATA) //串口发送数据 { TXREG=DATA; while(TRMT==0); } void interrupt time0(void) //串口接收中断 { if(RCIF) { if(FERR) { RCREG; //帧错误 return; } UART_RX_BUFF[UART_RX_COUNT] = RCREG; //将接收缓冲区内容读出 if(UART_RX_COUNT<9) UART_RX_COUNT++; if(OERR) //如果有溢出错误 { CREN = 0; //清零CREN位可将OERR位清零 CREN = 1; //再次将CREN置一,以允许继续接收 } } }