4
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Oct 19, 2012 at 13:11

1 Answer 1

1
\$\begingroup\$

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:

enter image description here

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.