USART Baud rate calculation Part-1
Now let’s understand the USART baud rate calculation. The formula to calculate the baud rate is shown in Figure 1.
Remember that whenever the word USART is used, we are talking about the asynchronous mode of USART peripheral, i.e., UART.
There are two formulas to calculate the baud rate based upon the value of the OVER8 parameter.
If OVER8=1, that means we are using an oversampling by 8, and the receiver block of the peripheral takes 8 samples to understand a bit.
The first formula in Figure 1 is used to calculate the baud rate if you are using oversampling by 8, and the second formula is used if you are using oversampling by 16.
Here the USARTDIV is the divide factor to generate different baud rates, and the minimum value for this is 1. You have to vary the USARTDIV value based on your baud rate requirements. You know that the minimum value of USARTDIV will be 1. Therefore, the maximum baud rate you can generate out of the peripheral that uses oversampling by 8 will be peripheral clock divide by 8.
If the oversampling by 16 is used, then the maximum baud rate generated out of the peripheral will be peripheral clock divided by 16.
Note: The baud rate for the receiver and transmitter (Rx and Tx) are both set to the same value as programmed in the Mantissa and Fraction values of USARTDIV.
When two USART peripherals are communicating, both should communicate with the same baud rate.
For example, while communicating between your PC and microcontroller board over the serial port communication, remember that the baud rate you program in your computer and the baud rate configured in the microcontrollers UART peripherals must be the same.
The generic formula for baud rate calculation is as shown in Figure 2, where the OVER8 value can be either 0 or 1.
Examples:
Now let’s discuss some examples for baud rate calculation.
Let’s say you want to generate the baud rate of 9600 bits per second. So, here the desired baud rate is 9600. Let’s consider the peripheral clock is 16MHz, and the oversampling by 16 is used.
What will you do now?
You have to use the formula,
In this example Tx/Rx baud = 9600bps and FPCLK = 16MHz. Now you have to calculate the division factor. For that first, substitute Tx/Rx baud and FPCLK values in equation 1.
Now you have to calculate the division factor (USARTDIV), which is then stored into the USART_BRR register or baud rate generation register. If you solve equation 2, you will get the USARTDIV value as follows:
USARTDIV = 16M/ (8 * 2 * 9600)
= 104.17
After that, you have to program the calculated value of USART DIV, i.e., 104.17 into the UART BRR register.
Steps to program the USARTDIV value into the USART_BRR register:
First of all, you have to convert the USARTDIV value into hex and then program the USART_BRR register to achieve the desired baud rate.
The USART_BRR register contains two sections.
One is a fraction part of 4-bits, and another is a mantissa part of 12 bits, as shown in Figure 3.
In 104.1875, 1875 is a fractional part, and 104 is a mantissa part.
Now let’s see how to program these parts.
If you look into the USART_BRR register, there are two sections, as shown in Figure 4. One is for fraction and another for the mantissa.
Fraction bits are only four bits from 0 to 3, whereas the mantissa is from 4 to 15, which is of total 11 bits.
1. Take the fraction part, i.e., 0.1875, and then multiply it by 16 because we are using oversampling by 16. After multiplication, you will get the value as follows:
DIV_Fraction = 0.1875 *16
= 3
2. You know that the mantissa part is 104 and convert that into hex i.e., 0x68.
3. Therefore, now the USARTDIV value will be 0x683. You have to program the value 0x683 into the USART_BRR register to generate a baud rate of 9600bps. The fraction part should be programmed into the fraction part of the USART_BRR register, and the mantissa part you have to program into the mantissa section of the baud rate register.
Now let’s take another example of generating a baud rate of 115200bps. Let’s consider the peripheral clock of 16MHz and oversampling by 8.
Now you have to calculate the USARTDIV,
USARTDIV = 16M / (115200 * 8)
= 17.361
Therefore, 17.361 is a value of USARTDIV.
First, let’s calculate the fraction as follows:
DIV_Fraction = 0.361 * 8
= 2.88
After multiplication, DIV_Fraction will be 2.88, which you can consider as 3.
Then convert the mantissa part into hex, i.e., 0x11. Therefore, now the USARTDIV value will be 0x113. You have to program 3 into the fraction part and 0x11 into the mantissa part of USART_BRR register. When you do this, the peripheral can generate a baud rate of 115200bps.
In the next article, let’s continue USART baud rate calculation.
FastBit Embedded Brain Academy Courses
Click here: https://fastbitlab.com/course1