Author Topic: STM32CubeMX sprintf does not work with float  (Read 586 times)

0 Members and 1 Guest are viewing this topic.

Offline ebclr

  • Super Contributor
  • ***
  • Posts: 2326
  • Country: 00
STM32CubeMX sprintf does not work with float
« 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));
   }
 

Online langwadt

  • Super Contributor
  • ***
  • Posts: 3460
  • Country: dk
Re: STM32CubeMX sprintf does not work with float
« Reply #1 on: September 23, 2022, 10:21:59 am »
mcu settings:  -u _printf_float
 

Online peter-h

  • Super Contributor
  • ***
  • Posts: 2662
  • Country: gb
  • Doing electronics since the 1960s...
Re: STM32CubeMX sprintf does not work with float
« Reply #2 on: September 23, 2022, 11:10:17 am »
Have a read around here
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
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 
The following users thanked this post: thm_w

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 3632
  • Country: es
Re: STM32CubeMX sprintf does not work with float
« Reply #3 on: September 23, 2022, 10:15:38 pm »
I remember some option in the compiler/linker whatever settings to enable floats in printf
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 3632
  • Country: es
Re: STM32CubeMX sprintf does not work with float
« Reply #4 on: September 24, 2022, 09:01:49 am »
As I said, here it is
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Online peter-h

  • Super Contributor
  • ***
  • Posts: 2662
  • Country: gb
  • Doing electronics since the 1960s...
Re: STM32CubeMX sprintf does not work with float
« Reply #5 on: September 24, 2022, 10:53:01 am »
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).
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf