单片机 uart中断处理函数例子

2019-04-15 12:14发布

#define Max_SendLen 100 #define Max_RecvLen 100 unsigned char TxData[Max_SendLen] = {0}; unsigned char RxData[Max_RecvLen] = {0}; void Digital_Recv_Int_xxx(unsigned char RX_buf) { static unsigned char RxCounter = 0; static unsigned short packageSize = 0;//数据包总字节数 USART_ClearITPendingBit(UART_x, USART_IT_RXNE); RxData[RxCounter] = RX_buf;//保存一个字节 if(RxCounter < 数据包头长度) { RxCounter++; } else { if(RxData[0] == 数据包开头1 ……)//判断数据包开头 { RxCounter++; 获取数据长度并计算整个数据包的长度packageSize if(RxCounter == packageSize ) { 数据包处理函数(); packageSize = 0; RxCounter = 0; // Disable the UART5 Transmit interrupt USART_ClearFlag(Bluetooth_UART,USART_IT_ORE) ; USART_ITConfig(Bluetooth_UART, USART_IT_RXNE, DISABLE); //clear bluetooth_RxData[] memset(bluetooth_RxData,0,Bluetooth_MaxRecvLen); } } else {RxCounter = 0;} } }