前言

对于STM32F1系列的开发板,串口波特率设置之后,就开始进行通信了,一般情况下是不需要修改的,但是在实际的项目开发中,我们可能需要通过串口对模块进行配置等操作,配置完模块之后进行数据之间的通信,又需要更改其波特率,所以下面要介绍如何修改串口波特率。


一、串口配置

在开发板上电之后,main函数下需要有配置好的串口参数及中断函数,下面介绍均以串口2为例。

1.串口初始化

代码示例如下:

void Usart2_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  USART_InitTypeDef USART_InitStructure;
	
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);	 //使能串口2时钟     
	
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;	       //选中串口默认输出管脚         
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //定义输出最大速率 
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//定义管脚9的模式  
  GPIO_Init(GPIOA, &GPIO_InitStructure);           //调用函数,把结构体参数输入进行初始化		   
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;	        
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;  
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
  GPIO_Init(GPIOA, &GPIO_InitStructure);    	 //A端口 
	
  USART_InitStructure.USART_BaudRate = 115200;						//速率115200bps
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;		//数据位8位
  USART_InitStructure.USART_StopBits = USART_StopBits_1;			//停止位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(USART2, &USART_InitStructure); //将以上赋完值的结构体带入库函数USART_Init进行初始化
  
  USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
  USART_Cmd(USART2, ENABLE);//开启USART2,注意与上面RCC_APB2PeriphClockCmd()设置的区别
  
}

GPIO_Init(GPIOA, &GPIO_InitStructure); 这句代码中GPIO_InitStructure被定义为结构体类型,类型名为GPIO_InitTypeDef ,进入下面函数:

typedef struct
{
  uint16_t GPIO_Pin;             /*!< Specifies the GPIO pins to be configured.
                                      This parameter can be any value of @ref GPIO_pins_define */

  GPIOSpeed_TypeDef GPIO_Speed;  /*!< Specifies the speed for the selected pins.
                                      This parameter can be a value of @ref GPIOSpeed_TypeDef */

  GPIOMode_TypeDef GPIO_Mode;    /*!< Specifies the operating mode for the selected pins.
                                      This parameter can be a value of @ref GPIOMode_TypeDef */
}GPIO_InitTypeDef;

可以发现对于引脚初始化需要对引脚,速度和模式进行配置。

USART_Init(USART2, &USART_InitStructure);对于串口初始化函数,和以上类似,USART_InitStructure也是被定义为结构体类型USART_InitTypeDef ,进入下面函数:

typedef struct
{
  uint32_t USART_BaudRate;            /*!< This member configures the USART communication baud rate.
                                           The baud rate is computed using the following formula:
                                            - IntegerDivider = ((PCLKx) / (16 * (USART_InitStruct->USART_BaudRate)))
                                            - FractionalDivider = ((IntegerDivider - ((u32) IntegerDivider)) * 16) + 0.5 */

  uint16_t USART_WordLength;          /*!< Specifies the number of data bits transmitted or received in a frame.
                                           This parameter can be a value of @ref USART_Word_Length */

  uint16_t USART_StopBits;            /*!< Specifies the number of stop bits transmitted.
                                           This parameter can be a value of @ref USART_Stop_Bits */

  uint16_t USART_Parity;              /*!< Specifies the parity mode.
                                           This parameter can be a value of @ref USART_Parity
                                           @note When parity is enabled, the computed parity is inserted
                                                 at the MSB position of the transmitted data (9th bit when
                                                 the word length is set to 9 data bits; 8th bit when the
                                                 word length is set to 8 data bits). */
 
  uint16_t USART_Mode;                /*!< Specifies wether the Receive or Transmit mode is enabled or disabled.
                                           This parameter can be a value of @ref USART_Mode */

  uint16_t USART_HardwareFlowControl; /*!< Specifies wether the hardware flow control mode is enabled
                                           or disabled.
                                           This parameter can be a value of @ref USART_Hardware_Flow_Control */
} USART_InitTypeDef;

由标准库中的代码,我们可以知道,配置串口时,需要对波特率,字节长度,停止位,校验位,模式和硬件数据流五项进行配置。
以上对串口配置完成,下面对配置好的串口修改其波特率。

2.修改波特率

之前串口2配置的波特率为115200,下面我们把波特率修改为57600,代码如下:

void USART_Config(uint32_t baud)
{
	USART_InitTypeDef USART_InitStructure;
	USART_Cmd(USART2, DISABLE);
	USART_InitStructure.USART_BaudRate =baud;
	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(USART2, &USART_InitStructure);
	USART_Cmd(USART2, ENABLE);
}

这是一个无返回值但是需要输入参数的函数,其输入参数为修改值波特率。配置完成之后,直接调用函数 USART_Config(57600);即可。

以下有两点特别说明:
其一:此函数的作用是把串口2的波特率修改为一特定值,如果想要多次修改波特率值,可以写一个数组,里面包括你想要切换的波特率,实时监测切换就行。
其二:修改串口波特率时,一定要先把串口关闭,调用函数USART_Cmd(USART2, DISABLE); ,以免造成数据传输错误,出现乱码,配置完成之后,再次打开串口,调用函数USART_Cmd(USART2, ENABLE); 。


总结

新的一天新的开始,美好的一天从学习串口开始!哈哈在这里插入图片描述

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐