EEVblog® Electronics Community Forum
Electronics => Microcontrollers => Topic started by: ebclr on September 23, 2022, 10:17:21 am
-
I have those 2 similar routines, one to write an integer to the LCD and the other to write a float, the integer ones work perfectly but the very similar one for float does not work, I'm guessing that sprintf fails for a float on stm32cubemx compiler, any tips on that?
void writeInteger(LiquidCrystal *lcd, int valueToBedisplayed, uint8_t MaxLenghtOfDigitis)
{
char StringValue[MaxLenghtOfDigitis];
sprintf(StringValue, "%d", valueToBedisplayed );
size_t Lengh = strlen(StringValue);
for (uint8_t i = 0; i < Lengh; i++){
write(lcd ,*(StringValue+i));
}
}
void writefloat(LiquidCrystal *lcd, float floatToBeDisplayed, uint8_t MaxLenghtOfDigitis)
{
char StringValue[MaxLenghtOfDigitis+2];
sprintf(StringValue, "%f", floatToBeDisplayed );
size_t Lengh = strlen(StringValue);
for (uint8_t i = 0; i < Lengh; i++){
write(lcd ,*(StringValue+i));
}
-
mcu settings: -u _printf_float
-
Have a read around here
https://www.eevblog.com/forum/microcontrollers/is-st-cube-ide-a-piece-of-buggy-crap/msg4363183/#msg4363183 (https://www.eevblog.com/forum/microcontrollers/is-st-cube-ide-a-piece-of-buggy-crap/msg4363183/#msg4363183)
You can have lots of fun with the Newlib libc.a library and it's duff mutexes, its use of the heap, etc :)
In the end I replaced it with an open source one - all in the thread and here
https://www.eevblog.com/forum/programming/best-thread-safe-printf-and-why-does-printf-need-the-heap-for-f-etc/msg4325479/#msg4325479 (https://www.eevblog.com/forum/programming/best-thread-safe-printf-and-why-does-printf-need-the-heap-for-f-etc/msg4325479/#msg4325479)
-
I remember some option in the compiler/linker whatever settings to enable floats in printf
-
As I said, here it is
-
That gives you the crappy Newlib library from 1990 which uses empty mutex function stubs, and the heap (which also has empty mutex function stubs) :)
Best to follow the instructions I posted and replace the printf family entirely.
But if not using an RTOS and the program is just simple, it doesn't matter. You do need to set up a heap though (_sbrk etc).