为什么在串口中断发送接受的一包数据很不问题,有时数据变短有时还有乱码?中断代码如下:
void USART1_IRQHandler(void) //
{
uint8_t Data,i,RxBuffer[256],length;
if (USART_GetFlagStatus(USART1, USART_FLAG_ORE) != RESET)//注意!不能使用if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)来判断
{
USART_ClearFlag(USART1, USART_FLAG_ORE);
USART_ReceiveData(USART1);
}
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
// RxBuffer[length++]= USART_ReceiveData(USART1);
Data = USART_ReceiveData(USART1);
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
USART_SendData(USART1, Data);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
}
串口软件显示:
[2012:09:21:12:21:05][发送]66 00 C8 00 00 C8 6A 00 F9
[2012:09:21:12:21:05][接收]66 00 C8 00 00 C8 6A 00 F9
[2012:09:21:12:21:06][发送]66 00 C8 00 00 C8 6A 00 F9
[2012:09:21:12:21:06][接收]66 00 C8 20 00 C8 6A 44 F9
[2012:09:21:12:21:06][发送]66 00 C8 00 00 C8 6A 00 F9
[2012:09:21:12:21:06][接收]01 C8 00 00 C8 6A 00 F9
而且我想取一包数据中有效的位时,当加入 RxBuffer[length++]= USART_ReceiveData(USART1);这样的语句时,程序运行时已进入中断就死掉了。
请高手来给菜鸟详细讲解一下! |