|
我配置好串口3、定时器2,串口3不定长度接收。做回环测试,串口3接收的数据老是错误,串口4、5就没有问题。下面是相关程序
//串口3初始化
/**********串口3***************/
void task_uart3(void)
{
// u8 i = 0;
//
// for(i = 0; i < Uart4_Isr_DataNum; i++)
// {
// rcvBuffer[commCount] = Uart4_Isr_RxData;
// if(commCount < 39)
// commCount++;
// }
// memset((void*)Uart4_Isr_RxData, 0, sizeof(Uart4_Isr_RxData));
// Uart4_Isr_DataNum = 0;
// test
u3_send_bytes(Uart3_Isr_RxData, Uart3_Isr_DataNum);
memset((void*)Uart3_Isr_RxData, 0, sizeof(Uart3_Isr_RxData));
Uart3_Isr_DataNum = 0;
// test
}
//串口3中断服务程序
//发送数据时,必须关闭中断,这里只负责处理接受中断
void uart3_isr() interrupt 16
{
EA = 0;
if((0x01 & SCON1))//是串口接收中断
{
stop_timer2();
SCON1 &= ~(1 << 0);
SCON1 &= ~(1 << 0);
// ET0 = 0;
// ET1 = 0;
// ES2R = 0;
// timer0_isr_flag = false;
// timer1_isr_flag = false;
Uart3_Isr_RxData[Uart3_Isr_DataNum++] = SBUF1;
if(false == task_uart3_id)
{
task_uart3_id = true;
}
start_timer2();
}
EA = 1;
}
void uart3_init(u32 baud)
{
MUX_SEL |= 0x20;//bit6置1表示将uart2接口引出到P0.4和P0.5
P0MDOUT &= 0x3F;
P0MDOUT |= 0x40;//设置对应的IO口输出输入
SCON1 = 0x90;
SCON1 = 0x90;
baud = 1024 - (u16)(6451200.0f / baud);
SREL1H = (baud >> 8) & 0xff;
SREL1L = baud & 0xff;
IEN2 = 0x01;//中断使能
EA = 1;
memset((void*)Uart3_Isr_RxData, 0, sizeof(Uart3_Isr_RxData));
Uart3_Isr_DataNum = 0;
task_uart3_id = false;
}
//发送一个字节
void u3_send_byte(u8 byte)
{
IEN2 = 0x00;
SBUF1 = byte;
while(!(0x02 & SCON1));
SCON1 &= ~(1 << 1);
SCON1 &= ~(1 << 1);
IEN2 = 0x01;//中断使能
}
//发送数据
void u3_send_bytes(u8 *bytes, u16 len)
{
u16 i;
IEN2 = 0x00;
for(i = 0; i < len; i++)
{
SBUF1 = bytes;
while(!(0x02 & SCON1));
SCON1 &= ~(1 << 1);
SCON1 &= ~(1 << 1);
}
IEN2 = 0x01;
}
//我在定时器2的中断里面进行4ms超时判断,4ms到了说明没有数据接收了,然后把接收到的数据再发出去
void timer2_isr() interrupt 5
{
EA = 0;
TF2 = 0;//清除定时器2的中断标志位
t2_cnt_1ms++;
if(t2_cnt_1ms == 4)
{
stop_timer2();
// if(task_uart4_id == true)
// {
// task_uart4();
//
// task_uart4_id = false;
// }
if(task_uart3_id == true)
{
task_uart3();
// ET0 = 1;
// ET1 = 1;
// ES2R = 1;
task_uart3_id = false;
}
}
EA = 1;
}
|
|