ADC on the ATMEGA

Share

Ok, I know its been long and I am rusty with my writing, so here’s a quickie.

ADC stands for Analog to Digital Converter. What do we need it for ? Well a lot of sensors that are used in Electronics(and Robotics) give output voltages in the form of potential differences. Doesn’t make sense ? Simply speaking, a sensor will give out its readings in the form of voltages. So what do we do with it ?? Well, we convert it to a number which will help us determine the actual implications of that reading. We fix a maximum reading as the reference number (255 in our case,since we will be ignoring the last 2 bits) and calculate the corresponding value of our reading on the new scale.

So, moving on, the ADC feature is available on PORTA of the ATMEGA16/32.

Features :

1. The reference (maximum) value can be taken from the AREF pin or an internal reference level of 2.56V is provided which can be accessed by the software. The AVCC may also be used as AREF by simply decoupling it(connecting to ground through a capacitor).

2. 10-bit is available. This means that you can distinguish between two readings which have a difference of (Reference Value/210) . This means that the max value is broken up into 210-1 = 1023 parts and if you have a reference of say 5V, you can distinguish between readings 4.88mV apart (yeah, that’s right !!)

3. The ADC can be used in free running mode(which means it will keep on converting continuously) or single conversion mode.

4. An interrupt may be generated once a conversion is complete, so that the MCU need not keep on checking for completion of conversion.

5. There is a ADC Noise Cancelling mode, which allows the user to switch off the other digital circuitry which might affect the conversion.

6. Since the result is 10 bit, the result is stored in 2 registers : ADCH and ADCL. If the ADLAR bit is not set, the result is right aligned. In that case, the 2 MSBs of the result are the 2 LSBs of the ADCH register and the remaining 8 LSBs of the result are in the ADCL. In this case, it is necessary to read the ADCL first and the ADCH second, only then will the ADC registers be updated with the next result.

7. Accuracy is best between 50 and 200 Khz clock frequency. Running at 16 Mhz,we select the prescaler as 128, which gives a clock frequency of 8/128, which is 125 Mhz.

Code :

Now, we set up the registers. Here’s how.

Next, we write the functions to initialise ADC and call a conversion. Here’s the sample code.

Resources :

1. ATMEGA16 Datasheet Pages 204 – 221

2. ADC Tutorial on AVRFreaks

3. Sample code

4. Registers

Share