Hi,

I am writing a function which will work out the contents of the USART1->BRR register when using STM32 microcontrollers.

This register sets up the USART speed, and works in the following manner:

Tx/Rx baud = fCK/(16*USARTDIV)

USARTDIV is programmed into the USARTx->BRR register to set up the speed.

The integer part is programmed into bits 4 to 16, while the decimal part goes into bits 0 to 3.

I would like to write a function that can do the sum above, and work out what goes in each portion and, given the baud rate and fCK, return the value that can be programmed into the BRR register.

I can get the integer part fine, but I don't know how to deal with the decimal part - could anyone please help me understand what I need to do?

I've been experimenting on an online compiler with the following on

http://en.cppreference.com/w/c/numeric/math/modf :

`#include <stdio.h>`

#include <math.h>

#include <float.h>

int main(void)

{

float fck = 48000000;

float baudrate = 9600;

float temp;

double topout;

float bottom;

temp = (fck/baudrate)/16;

bottom = modf(temp, &topout);

//topout stores the mantissa

//bottom stores the divider

//temp is the original number

printf("temp = %.4f\n bottom = %.4f\n topout = %.5f\n",temp,bottom,topout);

//to extract this and make the brr:

int mantissa;

int divisor;

mantissa = (int)topout;

divisor = ((int)(bottom*10));

divisor = divisor % 10000;

printf("mantissa = %.4d\n divisor = %.4d\n",mantissa,divisor);

}

http://coliru.stacked-crooked.com/view?id=d2fb35446965eb41Thanks