|
AVR Studio 5 snprintf / vsnprintf problem with FloatsBy Radu Motisan Posted on February 8th, 2012 , 1111 Views (Rate 2.38) |
By default, using snprintf or the similar vsnprintf with %f flag for floats will have a different result than expected. For instance, here is a LCD function I wrote, for displaying strings, accepting a variable number of parameters (like printf), including floats:
void HD44780::lcd_string_format(char *szFormat, ...) { char szBuffer[256]; //in this buffer we form the message int NUMCHARS = sizeof(szBuffer) / sizeof(szBuffer[0]); int LASTCHAR = NUMCHARS - 1; va_list pArgs; va_start(pArgs, szFormat); vsnprintf(szBuffer, NUMCHARS - 1, szFormat, pArgs); va_end(pArgs); lcd_string(szBuffer); }
Using it to display a float:
float x= 3.141592653; lcd_string_format("%f", x);
Will simply show ? (question mark):

To fix this , configure your AVR Studio project as follows:
1. Go to Project->Properties (ALT+F7) and under AVR/GNU C++ Linker select Libraries. Under Librarier (-Wl,-l) use the Add button twice and insert libprintf_flt.a then libm.a
2. Go to Miscellaneous and add -Wl,-u,vfprintf -lprintf_flt -lm
Save and rebuilt. The result looks much better:

|
|























January 6th, 2013 at 1:23 am
Cool man. Thank you. It works nice
but every time i create new project these setings must be done :/
January 6th, 2013 at 11:25 am
At least we have a solution.