# Analog to digital conversion

I am using an Atmega32-A microcontroller and an AD7798 external ADC. I am able to set the ADC registers and read back ADC values. I have written the following code:

  statusreg = AD7798_8(0x40, 0xFF);          // read STATUS register default value

AD7798_16(0x10, 0x0010);            // write Configuration reg = 0x0010.  2.5V range

if (value != 0x0010)
printf("unexpected conf setting %04x\r\n", value);

while ((statusreg & 0x80) != 0);                         // wait till ADC is ready


From the above code, I am reading the ADC values. According to the AD7798 datasheet and according to my configuration register setup, I have to find out the analog input voltage using following formula:

When the ADC is configured for bipolar operation, the output code is offset binary, with a negative full-scale voltage resulting in a code of 000...000, a zero differential input voltage resulting in a code of 100...000, and a positive full-scale input voltage resulting in a code of 111...111. The output code for any analog input voltage can be represented as

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

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

I am getting an ADC value of 1EF2(hex) for 0.6V analog input. I have measured using an oscilloscope. I have calculated the analog input voltage using the above formula and I am getting 0.15V. Here GAIN is 1, VREF = 2.5V. Which is wrong because I have to get 0.6V. I have converted resultant hex ADC value into decimal 7922 and also binary 0001111011110010. My analog input voltage is varying from 0V to 0.6V. According to my analog input voltage my ADC output is also changing from 0002 to 1EF2.

If I change the configuration register setup to any other setup like bipolar to unipolar (from 0x0010 to 0x1010), at that time I am getting an ADC value of 0000 always. It is giving an ADC value of 0000 for any configuration other than 0x0010 in the configuration register.

More over, I am not able to understand what the following sentence means:

with a negative full-scale voltage resulting in a code of 000...000, a zero differential input voltage resulting in a code of 100...000, and a positive full-scale input voltage resulting in a code of 111...111.

How do I properly convert resultant digital signal to an analog voltage?
Why does my code not work for any other configuration?

The AD7798 is a differential ADC. It has a negative input and a positive input. When configured for bipolar operation:

• When the voltage between in+ and in- is positive (in+ > in-) at full scale the output will be all one.

• When the voltage between in+ and in- is 0 (in+ == in-) the output will be 1000...0000.

• And When the voltage between in+ and in- is negative (in+ < in-) at full scale the output will be all zero.

• I have checked my AIN(+)=0.6(+) and AIN(-)=2.5(+), which means AIN(+)<AIN(-) then i have to get output as all zero's but I am getting values from "0000" to "1EF2". – verendra Oct 18 '12 at 14:15
• Now I understand clearly. ADC is a diffrential amplifier so it will give difference of two inputs as output so here AIN(-) is 2.6 and AIN(+) is 0.6. so I am getting negative -1.9 voltage as output. my ADC result also giving same value. am I correct. – verendra Oct 18 '12 at 14:37

As Blup1980 mentioned, the AD7798 is a differential ADC. That means that it takes the difference of the two inputs and converts that value.

You have:

AIN(+) = +0.6V with reference to GND
AIN(-) = +2.5V with reference to GND

But the difference between the two is:

$$AIN = 0.6 - 2.5 = -1.9$$

So we have the following:

$$Code = 2^{N – 1} × [(AIN × GAIN/VREF) + 1]$$ $$7922 = 32768 × [(AIN × 1/2.5) + 1]$$ $$0.24176025390625 = (AIN × 1/2.5) + 1$$ $$-0.75823974609375 = AIN × 1/2.5$$ $$-1.895599365234375 = AIN$$

Which, is correct.

Converted into C code, we have:

int      code_dec;
float    code_flt;
float    ain_valu;

/* Initialize variables */
code_dec = 0U;
code_flt = 0.0F;
ain_valu = 0.0F;


Casting the code value into a float and then using float values during every part of the calculation is good practice. Then you need the %f format specifier to ensure that the value is printed out as a floating point number which will include the negative sign if the value is negative. If you wish to include the positive sign for positive values, use %+f instead.
• @verendra Nothing is printing? Maybe the problem isn't with the conversion but with the connection to whatever screen you're using. Can you try printf("Hello World!\n");? Does that print out? – embedded.kyle Oct 19 '12 at 13:19