我用STM32F407进行串口通信,串口助手发送一个数据时如0x12,STM32F07接收到后判断,然后向串口调试助手发送相关数据,但在正确的数据前面会多了刚才发送的数据(如0x12),不知道这个是什么原因????
主函数
- #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[i];
- 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;
- }
- }
复制代码 ==========================================================================
配置USART1
- #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
- }
复制代码
|
发送什么数据就会先发送这个数据后,然后再发送其他数据
换了一个USB转串口模块就好了……