串口接收不到数据,串口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); 
     
    //配置端口的TX
    GPIO_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);
    
    //配置端口的RX
    GPIO_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 interrupt
    USART_ITConfig(UART4,USART_IT_RXNE,ENABLE);
		//Enables UART4
    USART_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	  PC10
	GPIO_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    PC11
	GPIO_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	  PC10
	GPIO_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    PC11
	GPIO_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,还是挺折磨人的。
如果对您有所帮助,麻烦点赞分享,这对我非常重要,感谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值