Skip to main content
NJP.1
Associate III
December 21, 2021
Solved

The %f floating point is not working properly in sprintf in my code, it put some garbage value

Hi all i am facing a problem with sprintf . the %u,%d are working fine in sprintf but the %f is throwing some garbage value in my code any thoughts please share me here. i have shared a snippet of my code over here.

float Channel_5_Volt;
Channel_5_Volt = ((float)Adc_5_Data/4095)*3.3;
sprintf(Buf,"adc0 %f \r\n", Channel_5_Volt);
HAL_UART_Transmit(&huart1,Buf, sizeof(Buf), 100);

This topic has been closed for replies.
NJP.1
NJP.1Best answer
Associate III
December 21, 2021

Thankyou for the reply I have not added the two function and now i added it and also checked it is working fine. Thankyou

 HAL_UART_Transmit(&huart1,Buf, sizeof(Buf), 100);

You should use strlen here and not sizeof.

Thakyou i will change it.

4 replies

TDK
December 21, 2021

Did you enable printf support in your program?

0693W00000HpJQwQAN.png 

> HAL_UART_Transmit(&huart1,Buf, sizeof(Buf), 100);

You should use strlen here and not sizeof.

"If you feel a post has answered your question, please click ""Accept as Solution""."
NJP.1
NJP.1AuthorBest answer
Associate III
December 21, 2021

Thankyou for the reply I have not added the two function and now i added it and also checked it is working fine. Thankyou

 HAL_UART_Transmit(&huart1,Buf, sizeof(Buf), 100);

You should use strlen here and not sizeof.

Thakyou i will change it.

NJP.1
NJP.1Author
Associate III
December 21, 2021

As tdk said check the two option in the mcu settings.

0693W00000HpJTvQAN.png

Piranha
Principal III
December 21, 2021

And take a note that 3.3 is of a double type, not float. If you want it to be of a float type, write 3.3f .