欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 名人名企 > 串口接收不到数据,串口RX配置(f407),f103和f407的区别

串口接收不到数据,串口RX配置(f407),f103和f407的区别

2025/3/21 6:26:39 来源:https://blog.csdn.net/qq_44139306/article/details/146364100  浏览:    关键词:串口接收不到数据,串口RX配置(f407),f103和f407的区别

问题

芯片:STM32F407,软件:标准库

使用串口时,直接把之前STM32F103的串口配置移植过来,同样以串口4为例,代码如下:

STM32F103 UART4:

void UART4_Configuration(uint32_t BaudRate)
{USART_InitTypeDef USART_InitStructure;GPIO_InitTypeDef  GPIO_InitStructure;  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); //配置端口的TXGPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOC, &GPIO_InitStructure);//配置端口的RXGPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;GPIO_Init(GPIOC, &GPIO_InitStructure); USART_InitStructure.USART_BaudRate = BaudRate;USART_InitStructure.USART_WordLength = USART_WordLength_8b;USART_InitStructure.USART_StopBits = USART_StopBits_1;USART_InitStructure.USART_Parity = USART_Parity_No;USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;USART_Init(UART4, &USART_InitStructure); //Enables UART4 Receive Data register not empty interruptUSART_ITConfig(UART4,USART_IT_RXNE,ENABLE);//Enables UART4USART_Cmd(UART4,ENABLE);      
}

STM32F407 UART4(错误):


void UART4_Init(uint32_t BaudRate)
{GPIO_InitTypeDef GPIO_InitStructure;USART_InitTypeDef USART_InitStructure;RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC);RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_UART4);GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_UART4);//UART4_TX	  PC10GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;GPIO_Init(GPIOC, &GPIO_InitStructure);
//UART4_RX    PC11GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;GPIO_Init(GPIOC, &GPIO_InitStructure);  USART_InitStructure.USART_BaudRate = BaudRate;USART_InitStructure.USART_WordLength = USART_WordLength_8b;USART_InitStructure.USART_StopBits = USART_StopBits_1;USART_InitStructure.USART_Parity = USART_Parity_No;USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;USART_ITConfig(UART4, USART_IT_RXNE, ENABLE); //Enable rx enable, /* Configure the USARTx */ USART_Init(UART4, &USART_InitStructure);/* Enable the USARTx */USART_Cmd(UART4, ENABLE);
}

问题来了,同样的配置,在F103中串口的收发都是正常的。但在F407中,串口就死活接收不到数据。为啥嘞?

解决

仔细检查代码发现:时钟使能,串口参数配置都没有问题,
那问题应该出现在引脚配置上。
果然! 是UART4_RX的配置出了问题,没有开启AF功能。

STM32F407 UART4(正确):


void UART4_Init(uint32_t BaudRate)
{GPIO_InitTypeDef GPIO_InitStructure;USART_InitTypeDef USART_InitStructure;RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC);RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_UART4);GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_UART4);//UART4_TX	  PC10GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;GPIO_Init(GPIOC, &GPIO_InitStructure);
//UART4_RX    PC11GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;GPIO_Init(GPIOC, &GPIO_InitStructure);  USART_InitStructure.USART_BaudRate = BaudRate;USART_InitStructure.USART_WordLength = USART_WordLength_8b;USART_InitStructure.USART_StopBits = USART_StopBits_1;USART_InitStructure.USART_Parity = USART_Parity_No;USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;USART_ITConfig(UART4, USART_IT_RXNE, ENABLE); //Enable rx enable, /* Configure the USARTx */ USART_Init(UART4, &USART_InitStructure);/* Enable the USARTx */USART_Cmd(UART4, ENABLE);
}

补充

那F103和F407关于串口的配置有什么区别呢?大概研究了一下,主要有两点区别:
1,AF(Alternate Function)映射

F103:有 AFIO 模块,需要使能 AFIO 时钟(RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);),且很多复用操作通过 AFIO 实现。

F407:F4 系列没有独立的 AFIO 模块(外设复用通过独立的 GPIO_PinAFConfig 函数配置),所以不需要使能 AFIO 时钟。

2,GPIO配置

F103中,UART_RX只需要配置为GPIO_Mode_IN即可,AF功能在很多情况下默认已经匹配。

而F407中,UART_RX必须明确配置为GPIO_Mode_AF,并且还需要调用GPIO_PinAFConfig()函数来指定该引脚的具体复用功能,否则UART模块与引脚之间并没有正确关联。

初次使用F407串口时容易遇到的一个小bug,还是挺折磨人的。
如果对您有所帮助,麻烦点赞分享,这对我非常重要,感谢!

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词