cancel
Showing results for 
Search instead for 
Did you mean: 

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

NJP.1
Associate III

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);

1 ACCEPTED SOLUTION

Accepted Solutions
NJP.1
Associate III

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.

View solution in original post

4 REPLIES 4
TDK
Guru

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
Associate III

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
Associate III

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

0693W00000HpJTvQAN.png

Piranha
Chief II

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 .