This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Printing floating point numbers to UART

Hey everyone,

I'm trying to print floating point numbers to the UART to debug my application. I know the nrf51 is doing the operations correctly because if I convert the variable to an int I can print it correctly and I get (~)accurate values. When I try to print floats nothing happens. I read that this may be because I'm linking against a library (for sprintf) that has no floating point support.

Any workarounds?

  • You answered your question. Need to compile with Full Printf support. For IAR it is in General Options for example.

  • Either link with floating point support. newlib-nano has that option or you do it manually using integer. Cast to int to printf integer part, then take the floating part multiply by 10, 100, 1000 depending on how many digits you want to keep. Don't forget to add .5 to round up. Then print the int part again. For example 2 decimal points : multiply by 100.0 and printf format is "%d.%02d", for 4 decimal point : multiply by 10000.0 using format "%d.%04d".

  • Do I just have to follow what's written here devzone.nordicsemi.com/.../ to link with newlib-nano?

  • Well, it depends on which development environment you use. Each has different way to do it. If you are using makefile with gcc then yes, follow that instruction. If you are using Eclipse project, it is in linker settings...

  • I'm using both actually, never figured out how to get eclipse to compile for the nrf51 so I'm using my custom makefile that I call from eclipse using "external tools"

1 2