Programming AVR ADC module with WinAVR using Atmega8 microcontroller
Most of AVR microcontrollers have Analog to Digital Converter (ADC) integrated in to chip. Such solution makes embedded designers life much easier when creating projects and programming them. With no need external ADC PCB takes less space, easier to create programs – it saves time and money. As an example lets take Atmega8 microcontroller which have up to 8 ADC inputs most with 10-bit resolution(excluding ADC4 and ADC5 inputs that are 8-bit). All features of AVR internal ADC can be found on official ATMEL AVR datasheets, but most important to mention are:
±2 LSB accuracy – so measurements aren’t very accurate. If AREF voltage is 5V then error may reach ±0.04V but this is still good results for most of tasks;
Integral nonlinearity ±0.5 LSB;
Conversion speed up to 15kSPS at maximum resolution. This is far not enough for 20kHz audio signal sampling.
ADC unit is powered with separate power supply pins AVCC with AGND, but AVCC must not differ ±0.3V of VCC. Also ADC unit can have different voltage reference sources selectable in ADMUX register. References may be taken from AREF pin, AVCC with external capacitor or internal 2.56V voltage reference. All ADC inputs are multiplexed via multiplexer. Each channels can be selected by changing 4 bits in ADMUX register. ADC unit can operata in two modes:
Single conversion – when ADC converter makes one conversion and then stops. As this mode require ADC initialization for each conversion it takes 25ADC clock cycles;
Free running conversion – conversion is continuous. Once initialized it takes 13 ADC cycles for single conversion. In this mode ADC data register has to be read before new value is written.
AVR ADC has a nice feature ADC noise reduction technique which allows to perform conversion with minimal noise induced from AVR core and io peripherals. It is simple – when noise canceling is enabled MCU is put to sleep(CPU clock stops). After conversion completes interrupt wakes processor to read and process converted data.
All theory is nicely explained in AVR datasheets better lets go to some practical issues. Connect photo resistor to ADC0 input and simple potentiometer to ADC2. As an example lets do single conversions with AVCC voltage as reference. Lets generate an interrupt when conversion is complete. Interrupt service routine will output ADC value on LCD. Circuit is really simple – on Proteus simulator it is even simpler:
There are three simple steps needed to achieve desired results. First of all we need to initialize ADC. For this adc_init() function is written which prepares reference voltage source (AVCC with external capacitor at AREF pin), then enables ADC peripheral and performs single dummy conversion to initialize ADC.
//select reference voltage
//AVCC with external capacitor at AREF pin
//set prescaller and enable ADC
ADCSRA|=(1<<ADEN)|(1<<ADIE);//enable ADC with dummy conversion
//set sleep mode for ADC noise reduction conversion
Next step is to convert data itself. As we need to read values from two channels, there also multiplexing is needed.
void adc_start_conversion(uint8_t channel)
//remember current ADC channel;
//select ADC channel
//Start conversion with Interrupt after conversion
//enable global interrupts
ADCSRA |= (1<<ADSC)|(1<<ADIE);
If this conversion mode invokes an interrupt after conversion is complete, thidr step is writing interrupt service routine, which takes 10 ADC value and displays it on LCD:
For more detail: Programming AVR ADC module with WinAVR using Atmega8 microcontroller
This free embedded project also found using:
- what is the functioning of ADC module in atmega microcontrollers
- avr adc program