STM32 MCU serial communication baud rate calculation method

Publisher:TranquilMind88Latest update time:2017-11-11 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. What is baud rate?

No matter what microcontroller it is, when using serial communication, there is a very important parameter: baud rate. What is baud rate: baud rate is the number of bytes transmitted per second. The baud rate is consistent during data transmission between the two parties, which is the basic guarantee for successful communication. The following takes the STM32 microcontroller as an example to explain how to calculate the serial port baud rate.

2. STM32 baud rate related registers

The STM32 microcontroller has only one register for setting the baud rate: the USART_BRR register, as shown in the figure below.

The effective number of this register is 16 bits, the first 4 bits are used to store the decimal part, and the last 12 bits are used to store the integer part. After calculating the baud rate, fill in the value of this baud rate. The following describes how to calculate.

3. Baud rate calculation method

The STM32 data sheet gives the calculation method, there is a formula, as shown below:


In this formula, there are three variables, two of which we know, Fck and Tx/Rx baud rate. These two are known, and USARTDIV is unknown. From the description of the formula, we can see that if USART1 is used, then Fck is PCLK2=72MHz, otherwise it is PCLK1=36MHz, and the parameter of Tx/Rx baud rate is known. We only need to calculate the value of USARTDIV and assign it to the USART_BRR register. Taking 115200 as an example, after transforming the formula, we get: USARTDIV = 72×1000000/(16×115200) = 39.0625. That is, write 39.0625 into USART_BRR.

As mentioned above, the first 4 bits of USART_BRR store the decimal part, and the last 12 bits store the integer part.

The decimal part DIV_Fraction = 0.0625×16 = 1 = 0x01;

The integer part DIV_Mantissa = 39 = 0x27;

Then USART_BRR = 0X271;

The data sheet provides us with a data table:


In this data sheet, the commonly used baud rate values ​​have been calculated, and we can use them directly. However, if we want to make the baud rate calculation a parameter-passing function,

For example: USART_INIT(uint_t 16 Baud), when calling, you only need to write USART_INIT(9600), USART_INIT(115200), so how should the program be written?

4. Program Implementation

The following is the register version of the program.


The first step of the program, line 45: first calculate USART_DIV, which is a variation of the previous formula;

The second step of the program, line 46: Get the integer part of USART_DIV;

Program step 3, line 47: Get the decimal part of USART_DIV;

The fourth step of the program, line 48: Shift the integer part left by 4 bits, that is, store it in the last 12 bits of the BRR register;

The fifth step of the program, line 49: combine the integer and the decimal part, that is, reassemble the first 4 digits and the last 12 digits;

Program step 6, line 50: Assign the calculated value to the BRR register.

In this way, a function with baud rate as a parameter is completed. From the above, it seems that the calculation of baud rate is not very difficult.


Keywords:STM32 Reference address:STM32 MCU serial communication baud rate calculation method

Previous article:Detailed explanation of STM32 general timer
Next article:ARM Notes: GPIO Interface

Recommended ReadingLatest update time:2024-11-23 12:16

The "unplug jlink" program in stm32 cannot run correctly
Q: When debugging the stm32 program, why can't the program on the development board run correctly after unplugging the jlink from the computer USB port? It also doesn't work after the breakpoint reset. A: First, unplug the connection between JLink and the development board (not just the connection between JLink and
[Microcontroller]
STM32 jumps from boot to application address
1. Set the starting address of IROM1 and the size of the program space in the keil c application project. 2. In boot, define the target address and function pointer type: #define ApplicationAddress 0x08001000 typedef void (*pFunction)(void); pFunction Jump_To_Application; u32 JumpAddress; 3. In boot, comp
[Microcontroller]
The concept of priority in STM32 (Cortex-M3)
There are two concepts of priority in STM32 (Cortex-M3) - preemptive priority and response priority. Some people call response priority "sub-priority" or "sub-priority". Each interrupt source needs to be assigned these two priorities. An interrupt with a high preemptive priority can be responded to during the process
[Microcontroller]
STM32 TFT learning notes - read ID
Host environment: Windows 7 SP1 Development environment: MDK5.14 Target board: ST NUCLEO-F303RE TFT model: 2.4 inches, with touch, SD card, 240*320 resolution, 260,000 colors Driver IC:ILI9325 ST library version: STM32Cube_FW_F3_V1.1.0 I got a NUCLEO-F303RE development board from an ST community event. I w
[Microcontroller]
STM32 TFT learning notes - read ID
Design and implementation of 32-bit MCU development board based on STM32F100VBT6
    RISC (reduced instruction set computer) is a microprocessor that executes fewer types of computer instructions. It originated from the MIPS host (i.e. RISC machine) in the 1980s. The microprocessors used in RISC machines are collectively called RISC processors. In this way, it can perform operations at a faster spe
[Microcontroller]
Design and implementation of 32-bit MCU development board based on STM32F100VBT6
STM32 learning: discussion on timer programming
Assume that timer 3 is used to time every 1 millisecond; the function saved to the SD card is StartSave(); Case 1: timer is fast, main loop is slow 1. Code design 1 (wrong design) view plain copy int cnt = 0; //Counting   //TIM3 interrupt handling function   void TIM3_IRQHandler(void)   {       if(TIM_GetITStatus(TIM
[Microcontroller]
STM32 custom frequency and duty cycle PWM output method
Figure 1 Figure II PWM is widely used, but different projects have special requirements for the output PWM. To meet these requirements, we need more experiments to verify. Next, we will describe the output method steps of the waveform shown in Figure 1 (Figure 2 is an abnormal waveform). 1. Resources used in this
[Microcontroller]
STM32 custom frequency and duty cycle PWM output method
STM32 Notes (I) GPIO port configuration
GPIO port usage: 1. The entire GPIO and AFIO  series supports   GPIO registers (1) Two 32-bit configuration registers (GPIOx_CRL, GPIOx_CRH); (2) Two 32-bit data registers (GPIOx_IDR, GPIOx_ODR); (3) A 32-bit set/reset register (GPIOx_BSRR); (4) A 16-bit reset register (GPIOx_BRR); (5) A 32-bit latch (GPIOx_LC
[Microcontroller]
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号