STM32F407使用USART1从串口助手接收一个字符后,发送数据,为什么每次用串口助手接收数据时会多收一个。

2019-07-21 01:16发布

我用STM32F407进行串口通信,串口助手发送一个数据时如0x12,STM32F07接收到后判断,然后向串口调试助手发送相关数据,但在正确的数据前面会多了刚才发送的数据(如0x12),不知道这个是什么原因????


============================================================
主函数代码 [mw_shl_code=c,true]#include<stm32f4xx.h> void config_clock(void); void delay_us(u32 nus); void config_GPIOs(void); void config_USART1(void); int flag = 1, i = 0; char a[15] = "hellow world "; void main(void) { config_clock(); config_GPIOs(); config_USART1(); NVIC_EnableIRQ(USART1_IRQn); while (1) { if (flag == 0) { USART1->DR = 's'; while ((USART1->SR & USART_SR_TC) == 0) ; USART1->SR &= ~USART_SR_TC; flag=2; } else if (flag == 1) { flag = 2; for (i=0; i<=12; i++) { USART1->DR = a; while ((USART1->SR & USART_SR_TC) == 0) ; USART1->SR &= ~USART_SR_TC; } } } } void USART1_IRQHandler() { if ((USART1->SR & USART_SR_RXNE) == USART_SR_RXNE) { if (USART1->DR == 0x12) flag=1; else if (USART1->DR == 0x13) flag = 0; } } [/mw_shl_code]




========================================================================================
配置USART1的代码 [mw_shl_code=c,true]#include<stm32f4xx.h> void config_USART1(void) { USART1->CR1 |= USART_CR1_UE; //USART1 enable USART1->CR1 &= ~USART_CR1_M; //use 1 start bit, 8 data bit, n stop bit USART1->CR2 &= ~USART_CR2_STOP; //1 stop bit USART1->BRR = 0x5161; //brad rate 2400 USART1->CR1 |= USART_CR1_TE; //send an idle frame as first transmission USART1->CR1 |= USART_CR1_RE; //receive is enabled and begins searching for a start bit USART1->SR &= ~USART_SR_TC; USART1->CR1 |= USART_CR1_RXNEIE; //RXNE interrupt enable } [/mw_shl_code]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。