cancel
Showing results for 
Search instead for 
Did you mean: 

sprintf doesn't work well to show float (I already added -u _printf_float in linker options)

Jordi Becares
Associate II

Hi all,

I have a problem to show float numbers via sprintf, I'm working with STM32F334

I did a program to print via serial the voltage read from ADC, in float format like %4.2f, the problem is that the first time the sprintf generate a valid string with decimal separator, but the next iterations not.

I followed the tip recommended for @Hae Ryon Jung​  in this entry of forum, but I didn't get a good result

https://community.st.com/s/question/0D50X00009XkYaySAF/how-to-print-float-value-with-printf-in-truestudio-

Any idea?

This is the code:

adcVal = HAL_ADC_GetValue(&hadc1);
adcRes = adcVal*3.3/4096;
sprintf(buffTmp, "[%d]ADC %d %4.2f V\n\r", (int) counter, (int)adcVal, adcRes);

This is the output:

[0]ADC 2531 2.04 V
[1]ADC 2493 204 V
[2]ADC 2495 204 V

Thanks for your support

Regards

Jordi

10 REPLIES 10
Jordi Becares
Associate II

Dear all,

ok ... It's a little shameful, but the problem was a stack fail. I defined the task stack with 512 bytes, and it was not enough to use sprintf with floats. The example works, but it does not work well.

So now it is fixed.

Thanks for all you support.

Regards

Jordi