First question for people who have doubts about Analog to digital conversion. It has been very nicely explained in Wikipedia and here is the link.
How it works
The Analog to Digital Converter (ADC) is used to convert an analog voltage (a voltage that vary continuously within a known range) to a 10-bit digital value. For instance, it can be used to log the output of a sensor (temperature, pressure, etc) at regular intervals, or to take some action in function of the measured variable value. There are several types of ADCs. The one used by AVR is of the "succesive approximation ADC" kind. The following is a simplified scheme of the ADC.At the input of the ADC itself is an analog multiplexer, which is used to select between eight analog inputs. That means that you can convert up to eight signals (not at the same time of course). At the end of the conversion, the correponding value is transferred to the registers ADCH and ADCL. As the AVR's registers are 8-bit wide, the 10-bit value can only be held in two registers.
The analog voltage at the input of the ADC must be greater than 0V, and smaller than the ADC's reference voltage AREF. The reference voltage is an external voltage you must supply at the Aref pin of the chip. The value the voltage at the input is converted to can be calculated with the following formula:
ADC conversion value = round( (vin/vref)*1023)
Since it is a 10-bit ADC, you have 1024(1024=2^10) possible output values (from 0 to 1023). So, if Vin is equal to 0V, the result of the conversion will be 0, if Vin is equal to vref, it will be 1023, and if vin is equal to vref/2 it will be 512. As you can see, since you are converting a continuous variable (with infinite possible values) to a variable with a finite number of possible values (called a "discrete variable"), the ADC conversion produces an error, known as "quantization error".
settings for selection of ADC in atmega 16 is as follows : -
ATmega16 has an inbuilt 10 bit, 8-channel ADC system. Some of the basic features of Armega16 ADC are:
· 8 Channels.
· 10-bit Resolution.
· Input voltage range of 0 to Vcc.
· Selectable 2.56V of internal Reference voltage source.
· AREF pin for External Reference voltage.
· ADC Conversion Complete Interrupt.
ADC channels in Atmega16 are multiplexed with PORTA and use the common pins (pin33 to pin40) with PORTA. ADC system of Atmega16 microcontroller consists of following pins:
i. ADC0-ADC7: 8 Channels from Pin 40 to Pin 33 of Atmega16 ADC peripheral.
ii. AREF: Pin32 of Atmega16 microcontroller, the voltage on AREF pin acts as the reference voltage for ADC conversion, reference voltage is always less than or equal to the supply voltage, i.e., Vcc.
iii. AVCC: Pin30, this pin is the supply voltage pin for using PORTA and the ADC; AVCC pin must be connected to Vcc (microcontroller supply voltage) to use PORTA and ADC.
Note: External reference voltage source can be used at AREF pin. However, Atmega16 also has internal reference voltage options of 2.56V and Vref = Vcc.
The figure below shows the pin configuration for ADC system of Atmega16 microcontroller.
ADC Registers
To use the ADC peripheral of Atmega16, certain registers need to be configured.
i. ADMUX (ADC Multiplexer And Selection Register)
REFS[0:1] bits determine the source of reference voltage whether it is internal or the external voltage source connected to AREF pin. MUX[4:0] bits are used to select between the channels which will provide data to ADC for conversion. ADLAR bit when set to 1 gives the left adjusted result in data registers ADCH and ADCL.
ii. ADCSRA (ADC Control and Status Register)
ADEN: ADC Enable bit, this bit must be set to 1 for turning ADC on.
ADSC: ADC Start Conversion bit, this bit is set to 1 to start ADC conversion, as soon as conversion is completed this bit is set back to 0 by the hardware.
ADATE: ADC Auto Trigger Enable, this bit is set to 1 to enable auto triggering of ADC conversion.
ADIF: ADC Interrupt Flag, this bit is set to 1 when ADC conversion gets complete.
ADIE: ADC Interrupt Enable, this bit is set to 1 if we want to activate the ADC conversion complete interrupt.
ADPS[0:2]: ADC Prescaler bits, these bits are used to set the ADC clock frequency, the configuration of these bits determine the division factor by which the microcontroller clock frequency is divided to get the ADC clock frequency. The figure above shows the prescaler bit values for respective division factor.
The ADC clock frequency must lie somewhere between 50 KHz to 200 KHz.
iii. ADCH & ADCL (ADC Data Registers)
When the ADC conversion is complete the data is stored in these two registers. The data configuration depends on the ADLAR bit value of ADMUX register. If ADLAR=0, data is right adjusted and if ADLAR=1, data is left adjusted. Always read ADCL first and then ADCH. In cases where the 8-bit precision is enough set the ADLAR bit to 1 to left adjust the data and read only the ADCH data register.
When ADLAR = 0,
When ADLAR = 1,