STM32 UART串口在中断中不能循环接收数据帧的问题
我要用STM32的串口接收一帧这样类型的数据:01 xx xx xx xx xx xx xx FD
这个数据长度不超过30个,结束字节是FD(数据里面没有FD)。我的程序设计是这样的,当接收到串口数据后进中断处理,循环接收数据,直到接收到的数据是FD就退出中断(中间断接收超时处理)。程序思路如:进中断后,关闭接收中断,通过读取USART_FLAG_RXNE标志来接收数据。我现在遇到的问题是在中断中不能根据USART_FLAG_RXNE标志来完成数据帧接收,
不知道为什么啊?代码如下:
- void Systick_Handler(void)
- {
- if (TimingDelayBuffer != 0x00)
- {
- TimingDelayBuffer--;
- }
- }
- void USART1_IRQHandler (void)
- {
- unsigned char i = 0;
- if (USART1->SR & USART_FLAG_RXNE) // read interrupt
- {
- USART1->CR1 &= USART_FLAG_RXNE; // Disable the USART Receive interrupt
-
- RxCounter = 0;
- RxBuffer[RxCounter++] = ((int)(USART1->DR & 0x1FF));
- USART1->SR &= ~USART_FLAG_RXNE; // clear interrupt flag
-
- for (i=0; i<30; i++)
- {
- SysTick->CTRL |= SysTick_Counter_Enable; //允许计数//用SysTick做的一个定时器。
- TimingDelayBuffer = 1000;
- while (!(USART1->SR & USART_FLAG_RXNE))
- {
- if (TimingDelayBuffer == 0) //定时一秒没有数据就退出
- {
- USART1->CR1 |= USART_FLAG_RXNE; //Eneble the USART Receive interrupt
- return;
- }
-
- }
-
- SysTick->CTRL &= SysTick_Counter_Disable; //禁止计数
- SysTick->VAL = SysTick_Counter_Clear; //计数器清0
-
- RxBuffer[RxCounter] = ((int)(USART1->DR & 0x1FF));
- USART1->SR &= ~USART_FLAG_RXNE; // clear interrupt flag
-
- if (RxBuffer[RxCounter] == 0xFD)
- {
- RxdDisposeFlag = 1;
- return;
- }
-
- RxCounter++;
- }
-
- USART1->CR1 |= USART_FLAG_RXNE; //Eneble the USART Receive interrupt
- }
- }
复制代码
一周热门 更多>