void USART2_IRQHandler() { if(USART_GetITStatus(USART2,USART_IT_RXNE) != RESET) //中断产生 { USART_ClearITPendingBit(USART2,USART_IT_RXNE); //清除中断标志 Uart2_Buffer[Uart2_Rx] = USART_ReceiveData(USART2); //把数据存储到缓冲数组 Uart2_Rx++; Uart2_Rx &= 0xFF; } if(Uart2_Buffer[Uart2_Rx-1] == 0x5A) //如果收到的是 0x5A 则下面就是有效数据 Uart2_Tx = Uart2_Rx-1; if((Uart2_Buffer[Uart2_Tx] == 0x5A)&&(Uart2_Buffer[Uart2_Rx-1] == 0xA5)) //检测到头的情况下检测到尾 { Uart2_Len = Uart2_Rx-1- Uart2_Tx; //长度 Uart2_Sta=1; //标志位 } if(USART_GetFlagStatus(USART2,USART_FLAG_ORE) == SET) //溢出 { USART_ClearFlag(USART2,USART_FLAG_ORE); //读SR USART_ReceiveData(USART2); //读DR } } main.c################################################################### if( Uart2_Sta )//数据帧接收完毕 { for(tx2=0;tx2 <= Uart2_Len;tx2++,Uart2_Tx++) USART2_SendByte(Uart2_Buffer[Uart2_Tx]); //发送数据 Uart2_Rx = 0; //初始化 Uart2_Tx = 0; Uart2_Sta = 0; } 比如数据帧为 5A 00 11 65 33 22 23 43 A5 此为一帧
时间: 2024-11-05 13:36:53