[SOLVED] how to covter floating point to bits

flyboy74
Fri Jun 22, 2018 10:55 am
I am now working on setting a a UART so that I can send debug info to a putty serial terminal on my computer.

I need to be able to set the baud rate and it works in an unusual way. I have to set the USARTDIV and it needs floating point accuracy.

USARTDIV = Fck / (16 * baud) which quite often gives a floating point outcome. See some examples from datasheet

uart.JPG
uart.JPG (51.56 KiB) Viewed 268 times

dannyf
Fri Jun 22, 2018 11:01 am
depending on usartdiv’s construction. if it takes the form of _mantissa.._fraction, and the fraction is in 1/16th, then

USARTDIIV = Fclk/baud;

will do.


flyboy74
Fri Jun 22, 2018 11:19 am
ok so does this mean if I take the example of 9600 at a Fck of 36Mhz means the USARTDIIV = 234.375

Does this mean the top 12 bits are _mantissa = 234 and the bottom 4 bits are _fraction = 375/16 = 23.4375 so round to 23 ????


flyboy74
Fri Jun 22, 2018 11:22 am
23 in binary is 10111 and I only have 4 bits

stevestrong
Fri Jun 22, 2018 11:36 am
https://github.com/rogerclarkmelbourne/ … #L165-L183

dannyf
Fri Jun 22, 2018 3:06 pm
36Mhz / 9600 = 3750

dannyf
Fri Jun 22, 2018 3:21 pm
try 115.2kbps:

36Mhz/115.2kbps = 312.5 -> usartdiv = 312.

the chart tells you to code 19.5 -> 19 * 16 + 0.5*16 = 304 + 8 = 312. you may also consider implement rounding here.

the key here is that the fraction is the lowest 4 bits coded in 1/16th and the mantissa the upper bits.

if not, you will need to play around with some integer math: on that the msp432 has some of the most painful baud rate calculations I have ever seen.


flyboy74
Fri Jun 22, 2018 9:22 pm
@dannyf

36Mhz / 9600 = 3750

if I look at your method 3750 = 0b 0000 1110 1010 0110

if I look at the data sheet it gives 234.375 which is 234 in mantissa with fraction being 375/1000 expressed in 1/16th’s
mantissa= 234 << 4 = 0b 0000 1110 1010 0000
fraction = 375/(1000/16) = 6 = 0b 0110
doing mantissa | fraction = 0b 0000 1110 1010 0110

It seems just doing fck/baud and rounding is a quick cut to the same answer. Not sure why the datasheet gives us the long way around

@stevestrong
thanKs for the link. I looked at the code and understand how it works. It does what the data sheet explains but as dannf has posted there is a simpler way to do it.

Thanks everyone that has helped me out. I think that I understand it now :)


dannyf
Fri Jun 22, 2018 11:44 pm
Not sure why the datasheet gives us the long way around

if you think the approach in the datasheet is nuts, take a look at how SPL did it – it is insane.

it is part of the free benefits of using ST parts, :) things like that aren’t unique to this particular module / part.


Pito
Sat Jun 23, 2018 8:10 am
The UART works fine with baudrates <=3% off.
You do not need floating point, imho.

dannyf
Sat Jun 23, 2018 1:15 pm
it depends. with low clock frequencies and high transmission speed, the fractionals are critical.

Leave a Reply

Your email address will not be published. Required fields are marked *