# Formula calculation and printing negative value.

 Unsigned int adc;


I am getting ADC value as decimal number range between "0 to 65535". I want to convert it into analog voltage using the fallowing formula.

Code = 2N – 1 × [(AIN × GAIN / VREF) + 1]

where:
N = 16 for the AD7798, and N = 24 for the AD7799.

I have simplified the formula and calculating

// formula to caluculate analog voltage from digital output :Code = 2N – 1× [(AIN × GAIN/VREF) + 1] // GAIN / VREF = 1 / 2.5 => 0.4
// 0.4 Ain = ((adc / 32768) - 1)

float Ain;

Ain=(((((float) adc) / 32768) - 1) / 0.416);

printf("value:%.2f [v]\r\n", Ain);

The above formula returns always negative value. I want to print that negative value. I have tried as above but its not working. How to print Ain value? here adc is unsigned value so I am converting into float.

I have tried like this also

Ain = (((adc / 32768.0) - 1) / 0.416);

printf("value:%.2f [v]\r\n", Ain);


but I am not able to print that value.

• I added some example code to my answer to your other question. If that doesn't help, let us know what what is being printed out by the code you've given above. If we can see what is being output, we can help figure out what is wrong with the input. – embedded.kyle Oct 19 '12 at 13:11

The AVR libraries don't compile in support for printing of floating point values by default. This is to save space on their limited resource environments. You need to tell the compiler and the linker to enable support for this feature.

From the avr-libc documentation on vfprintf():

Since the full implementation of all the mentioned features becomes fairly large, three different flavours of vfprintf() can be selected using linker options. The default vfprintf() implements all the mentioned functionality except floating point conversions. A minimized version of vfprintf() is available that only implements the very basic integer and string conversion facilities, but only the # additional option can be specified using conversion flags (these flags are parsed correctly from the format specification, but then simply ignored). This version can be requested using the following compiler options:

-Wl,-u,vfprintf -lprintf_min

If the full functionality including the floating point conversions is required, the following options should be used:

-Wl,-u,vfprintf -lprintf_flt -lm

(emphasis mine)

Since you are using CodeVisionAVR as we discussed in chat, they have this option built in to their configuration window as seen on Page 4, Step 9 of this tutorial: