串口中断接收程序

2019-07-21 01:38发布

拷贝原子哥的程序,发现每次通过串口调试助手发送 23 45 0d 0a 只能接收到23  再发送68 97 0d 0a  又只能接收到67  真是什么问题啊  求大神指导 谢谢

int main(void)
{ [mw_shl_code=c,true]int main(void) { u8 t; u8 len; u16 times=0; RccConfig(); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// delay_init(168); // UART_Init(9600); while(1) { if(USART_RX_STA&0x8000) { len=USART_RX_STA&0x3fff; for(t=0;t<len;t++) { USART_SendData(USART2, USART_RX_BUF[t]); while(USART_GetFlagStatus(USART2,USART_FLAG_TC)!=SET); } USART_RX_STA=0; }else { times++; if(times%10==0) USART_SendData(USART2, 0x55); delay_ms(10); } } } void UART_Init(U32 bound) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //使能GPIOA时钟 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//使能USART1时钟 //串口1对应引脚复用映射 GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2); //GPIOA9复用为USART1 GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2); //GPIOA10复用为USART1 //USART1端口配置 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; //GPIOA9与GPIOA10 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度50MHz GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉 GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化PA9,PA10 //USART1 初始化设置 USART_InitStructure.USART_BaudRate = bound;//波特率设置 USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式 USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位 USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式 USART_Init(USART2, &USART_InitStructure); //初始化串口1 USART_Cmd(USART2, ENABLE); //使能串口1 USART_ClearFlag(USART2, USART_FLAG_TC); USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); /*开启中断*/ NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3; NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } void USART2_IRQHandler(void) { U8 Res; if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //接收中断( { Res =USART_ReceiveData(USART2);//(USART1->DR); //读取接收到的数据 USART_ClearITPendingBit(USART2, USART_IT_RXNE); if((USART_RX_STA&0x8000)==0)//接收未完成 { if(USART_RX_STA&0x4000)//接收到了0x0d { if(Res!=0x0a)USART_RX_STA=0;//接收错误,重新开始 else USART_RX_STA|=0x8000; //接收完成了 } else //还没收到0X0D { if(Res==0x0d)USART_RX_STA|=0x4000; else { USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ; USART_RX_STA++; } } } } } [/mw_shl_code]

 
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。