**USART Baud rate calculation Part-2**

Now let’s see how to calculate the USARTDIV value in our program. Because at the end of the day, you have to take the user given baud rate and then must calculate the mantissa and fraction part, which should be then configured into the BRR register.

The formula to calculate the USARTDIV (if oversampling by 8 is used) is as follows:

Where the FPCLK is a peripheral clock or the clock of the bus on which the peripheral is hanging. Either it is an APB1 clock or an APB2 clock, which you need to calculate by using your code. For that, there is one API or helper function that calculates and returns the value of the APB1 clock or APB2 clock.

Let’s say you substitute all values in the above equation and get some result as 4.71, which is a real number.

4.71 is a decimal number, and you have to use float data type in order to hold this number because it is not an integer. Instead of dealing with a decimal number, you can convert it into a whole number.

For that, you have to multiply the equation 1 by 100.

i.e., USARTDIV = (FPCLK / (8 * Baud rate)) * 100

When you multiply the equation 1 by 100, the fractional part 0.71 will come into the integer part, and it becomes a whole number. Therefore, the equation becomes,

Now the equation 2 will give you 471. Let’s consider 471 as X. Now you got the integer number, and it is very easy to calculate the integer part, i.e., the mantissa part.

Now,

Integer part (I) = X /100

= 4

You got the mantissa part as 4, and you can straight away go and program the mantissa part of the USART baud rate register.

The fraction part is calculated as follows:

F = X – (I * 100)

= 471 – 400

= 71

But according to our theory, we have to take the fractional part and should multiply it by 8 if it is oversampling by 8. When you multiply 0.71 with 8, you will get 5.68. If you round off 5.68, it gives 6. Same thing you have to do here.

F = 71 * 8

= 568.

Now you got F as 568. To round off a number, you have to add a round off factor 50 (568 + 50). Now the F value becomes 618, and then divide it by 100, you will get 6. After that, you have to program the value 6 into the fraction part of the USART baud rate register.

In the next lecture, let’s understand **USART baud rate coding**.

**FastBit Embedded Brain Academy Courses**

Click here: https://fastbitlab.com/course1