本帖最后由 飘雪殇 于 2018-7-5 14:02 编辑
void UART_Init (UINT32 u32Baudrate)
{
P07_Input_Mode;
P06_OpenDrain_Mode;
SCON = 0x50; //UART0 Mode1,REN=1,TI=1
TMOD |= 0x20; //Timer1 Mode1
set_SMOD; //UART0 Double Rate Enable
set_T1M;
clr_BRCK; //Serial port 0 baud rate clock source = Timer1
#ifdef FOSC_160000
TH1 = 256 - (1000000/u32Baudrate+1);
#endif
#ifdef FOSC_166000
TH1 = 256 - (1037500/u32Baudrate);
#endif
set_TR1;
set_TI; //For printf function must setting TI = 1
clr_ET1;
set_ES;
set_EA;
set_PS; //设置串口中断优先级最高
set_PSH;
}
void UART_SendByte(u8 c)
{
TI = 0;
SBUF = c;
while(TI==0);
}
void SerialPort0_ISR(void) interrupt 4
{
if(TI == 1)
{
clr_TI; /* if emission occur */
}
if (RI == 1)
{
clr_RI;
// clr_P14; //TEST
//接收数据
ReceivedBuf[ReceivedBufIndex] = SBUF;
//根据第一个数据决定此帧有多少个数据
if(ReceivedBufIndex == 0)
{
ReceivedBufByteNumLimit = ReceivedBuf[0];
}
ReceivedBufFrameAutoResetCount = 50;
//判断是否接收完毕
ReceivedBufIndex++;
if(ReceivedBufIndex == ReceivedBufByteNumLimit)
{
ReceivedBufIndex = 0;
UART_ReceivedCompleteFlag = 1;
}
}
}
串口的初始化和接收函数如上,其中还用了一个定时器3的100us的定时器中断,想请问下有什么原因有可能会导致串口接收数据不准确呢?将TX和RX的引脚连在一起来发送接收数据有没有可能影响到串口的接收数据的准确性呢? |