Detailed explanation of baud rate calculation under STM32

Publisher:SHow111timeLatest update time:2016-06-07 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Baud rate calculation

    The baud rate of STM32 is closely related to the serial port peripheral clock. The clock of USART 1 comes from APB2, and the clock of USART 2-5 comes from APB1. In STM32, there is a baud rate register USART_BRR, as follows:

Original detailed explanation of baud rate calculation under STM32
 
    The baud rate of the STM32 serial port is set through USART_BRR. The baud rate register of STM32 supports fractional settings to improve accuracy. The first 4 bits of USART_BRR are used to represent decimals, and the last 12 bits are used to represent integers. However, it is not the baud rate we want to set. If we want to set the baud rate of our serial port, we still need to calculate it. In fact, the calculation of the baud rate is the following expression:
Original detailed explanation of baud rate calculation under STM32
 
    From the above expression, we introduce a new quantity USARTDIV, which represents the frequency division of the serial port clock source fck. Assuming that we already know the baud rate and the fck clock frequency, we can calculate the specific size of USARTDIV through the above formula, and then set the baud rate register according to the value of USART.
    USARTDIV is a floating point number with a decimal (such as 27.75) obtained from the above expression. Separate the decimal part from the integer part to get an integer value n (such as 27) and a decimal value m (such as 0.75). With these two values, we can fill in the USART_BRR register and set the baud rate of our serial port.
    Write the integer part m (27 = 0x1B) directly into the last 12 bits of USART_BRR; multiply the decimal part n by 16 to get the integer value (such as 0.75 x 16 = 12 = 0xC) and write it into the first 4 bits of USART_BRR. Finally, the value of USART_BRR is 0x1BC.
Note: If the fractional part still contains a decimal after multiplying it by 16, round it off to get a new integer and write it into the first four bits of USART_BRR.
 
Why do we multiply by 16 in the formula for calculating the baud rate?
    We know that serial communication is carried out through the two lines TXD and RXD. When the receiver's RXD is connected to the transmitter's TXD, and the receiver's TXD is connected to the transmitter's RXD, the receiver and the transmitter can exchange data through RXD and TXD. When the receiver detects that the level of the RXD line is pulled to a low level, it immediately starts to receive the data sent by the transmitter. The low level just now is just a start bit that tells the receiver that it can receive data.
    During data transmission, the signal may be interfered with and generate some jitter, as shown in the figure below. If the receiving end only samples the signal data once, it is possible that the sampled data is jittery and inaccurate, which will make the data transmission inaccurate. Therefore, the receiving end usually samples the data on the sampled data line multiple times and then obtains accurate data through comparison.
  
    Original detailed explanation of baud rate calculation under STM32
 
    As mentioned before, USARTDIV means dividing the serial port clock source fck, and 16 represents the number of sampling times of 1 bit data. Why?
    Original detailed explanation of baud rate calculation under STM32
, reversing the numerator and denominator of this expression, we can get the following expression
    Original detailed explanation of baud rate calculation under STM32
    The transmission time of each bit is only 1/TX_baud, and this total time is divided by 16, so the time for each sampling is exactly T1, which is the new divided period. The initial serial port clock signal comes from APBx, and the APBx clock signal needs to be divided to be equal to T1, so the USARTDIV division is required.
Keywords:STM32 Reference address:Detailed explanation of baud rate calculation under STM32

Previous article:Printf usage of multiple serial ports under STM32
Next article:Learning and experience of STM32 USART serial port

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号