Posted on at

Objectives:
 To program and use the ADC feature of ATmega16
 Show the lower byte of ADC digital value on Port D and higher byte on Port B
Introduction:
ADC  is  used  to  convert  the  analog  voltages  into  digital  value.  ADC  is  widely  used  in  data
acquisition  so  most  of  the  modern microcontrollers  have  on-chip  ADC  peripheral. ATmega16
has on-chip  ADC  of 10-bit resolution. It  has 8 analog  input  channels, out  of which  7  input
channels can be used for differential input. Two differential input channels (ADC0 and ADC2)
can have the input gain of 10x and 200x.
As the ADC is 10-bit, so the converted digital output is stored in two 8-bit registers ADCL and
ADCH. Reference  voltages  for  ADC  can  be  connected  to  AVCC  (Analog  Vcc),  internal  2.56V
reference or external AREF pin. Minimum 0V and maximum Vcc
can be converted to a digital
value. Successive approximation circuitry converted and analog voltage into digital value. This
circuitry requires a clock frequency between 50 kHz to 100 kHz.
Following five registers are associated with the ADC of AVR:
ADCL : Has 8 LSBs of converted digital result
ADCH : Has 2 MSBs of converted digital result
ADMUX : For left / right adjusted result, reference voltage and channel selection
SFIOR : Three MSBs of this register are used to select the auto trigger source of ADC
Single ended result can be found from following formula:

Vref

where Vin is  the  voltage  on  the  selected  input channel, Vref the  selected  voltagereference and ADC is the 10-bit converted digital decimal value.
Similarly, differential input result can be found from following formula:
ADC=(Vpos −Vneg ) ×Gain × 512

Vref

where Vpos and Vneg are the two differential input channels and the Gain can be selected as 1x, 10x and 200x from ADMUX register.

Bit #  7           6            5          4         3           2             1                 0
Bit Name REFS1  REFS0 ADLAR MUX4 MUX3 MUX2 MUX1 MUX0
REFS1:0 Reference selection bits
0 0 AREF, Internal Vref turned off
0 1 AVCC with external capacitor at AREF pin
1 0 Reserved
1 1 Internal 2.56V Voltage Reference with external capacitor at AREF pin
the result at position D7 and D6. Remaining bits are not used
MUX4 : 0 :- Analog channel and gain selection bits. See following table for details

MUX4:0  Single EndedI/P Positive Differential I/P Negative Differential I/P  Gain  00000 ADC0
01000*      NA
11110 1.22V(VBG)     NA
11111 0V (GND)
* Not applicable Steps to program A/D converter using interrupts:
1. Make the pin input which is selected as ADC input channel
4. Load  the  appropriate  value  in  ADMUX  to  select  the  input  channel,  reference  voltages
and left of right aligned result.
result if right aligned
8. Go to step 6 to read the selected channel again

C Code to Display ADC value on Port A and Port B:

/* This program converts the analog input to 10-bit digital output. ADC0 (channel 0) is analog input. Lower byte of digital output is shown on Port D and 2 MSB bits out of 10 bits is shown on Port B */.

#include<avr\io.h>
#include<avr\interrupt.h>
/////////////// Macros Definition/////////////////
#define Bit(x) (0x01 << (x))
#define BitSet(p, m) ((p) |= (m))
#define BitClr(p, m) ((p) &= ~(m))
{

}
int main(void)
{
DDRA = 0x00; //PortA as input for ADC input
DDRD = 0xFF; //PortD as output for lower byte of Digital value
DDRB = 0xFF; //PortB as output for higher byte of Digital value
sei(); //Enable global interrupt
while(1);
}
{
BitSet(ADMUX, Bit(7)); //Internal 2.56V selected as Vref
BitSet(ADMUX, Bit(6)); //Internal 2.56V selected as Vref
BitClr(ADMUX, Bit(4)); //Single ended (GND as common ground)
//ADMUX = 0xC0; //Same settings as given above
//ADCSRA = 0xCF ; //Same settings as described above
}

Simulation: 