I am successfully reading external ADC value using ATmega32-A.
Unsigned int adc;
adc = AD7798_16(0x58, 0xFFFF); // read register
printf("ADC value is %d\r\n", 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:
AIN is the analog input voltage.
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.
Best Answer
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()
:(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: