PocketMagic

PocketMagic

Where Technology meets magic


Android
45 Posts
BlackBerry
4 Posts
Electronics
68 Posts
Hardware
120 Posts
High Voltage
49 Posts
iPhone
4 Posts
Linux
2 Posts
Nuclear
20 Posts
Optics
11 Posts
Photography
7 Posts
Photoshop
3 Posts
Research
19 Posts
Reviews
18 Posts
Robotics
8 Posts
Security
7 Posts
Software
73 Posts
Symbian
2 Posts
Tubes
18 Posts
Windows
3 Posts
Windows Mobile
11 Posts

Top Articles!       See PocketMagic on Facebook


uRADMonitor - Online Radiation monitoring station | 14810 Views | Rate 70.52
uRADMonitor - Online Radiation monitoring station
Developing for Blackberry 10 | 58 Views | Rate 58
Developing for Blackberry 10
Bluetooth and iOS - Use Bluetooth in your iPhone apps | 17760 Views | Rate 57.85
Bluetooth and iOS - Use Bluetooth in your iPhone apps
NMEA GPS Library for AVR Microcontrollers | 4778 Views | Rate 57.57
NMEA GPS Library for AVR Microcontrollers
Building a robot – Part 2 | 4528 Views | Rate 44.83
Building a robot – Part 2
Programmatically Injecting Events on Android - Part 2 | 4862 Views | Rate 44.2
Programmatically Injecting Events on Android - Part 2
Simple Switched power Supplies | 15881 Views | Rate 41.46
Simple Switched power Supplies
Capacitor Discharge Microspot Welder / Cutter | 11167 Views | Rate 36.14
Capacitor Discharge Microspot Welder / Cutter

 
  

AVR Studio 5 snprintf / vsnprintf problem with Floats


By 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:

  1.  
  2. void HD44780::lcd_string_format(char *szFormat, ...)
  3. {
  4. char szBuffer[256]; //in this buffer we form the message
  5. int NUMCHARS = sizeof(szBuffer) / sizeof(szBuffer[0]);
  6. int LASTCHAR = NUMCHARS - 1;
  7. va_list pArgs;
  8. va_start(pArgs, szFormat);
  9. vsnprintf(szBuffer, NUMCHARS - 1, szFormat, pArgs);
  10. va_end(pArgs);
  11.  
  12. lcd_string(szBuffer);
  13. }
  14.  

Using it to display a float:

  1.  
  2. float x= 3.141592653;
  3. lcd_string_format("%f", x);
  4.  

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:






  

More on PocketMagic:

Programmatically Injecting Events on Android - Part 1 | 9992 Views | Rate 25.89
Programmatically Injecting Events on Android - Part 1
Atmega8 and enc28J60 for ethernet support | 7123 Views | Rate 24.31
Atmega8 and enc28J60 for ethernet support
Victoreen CDV-717 Remote Gamma Survey dosimeter | 97 Views | Rate 24.25
Victoreen CDV-717 Remote Gamma Survey dosimeter
How to set the AVR Fusebits | 1650 Views | Rate 23.57
How to set the AVR Fusebits
CDV-742 Dosimeter and CDV-750 Dosimeter Charger | 94 Views | Rate 23.5
CDV-742 Dosimeter and CDV-750 Dosimeter Charger
ATMega128 and HD44780 LCD using 3 Wires with the 74HC164 | 2008 Views | Rate 22.82
ATMega128 and HD44780 LCD using 3 Wires with the 74HC164

2 Responses to “AVR Studio 5 snprintf / vsnprintf problem with Floats”

  1. 1
    Deimis:

    Cool man. Thank you. It works nice :) but every time i create new project these setings must be done :/

  2. 2
    Radu Motisan:

    At least we have a solution.

Thank you for commenting. Your comment won't show until approved, which can take some time.

Please copy the string a4GEHT to the field below: