AVR Tutorials
The Best AVR Microcontroller Tutorials on the Web

AVR ADC Interrupt Feature

A Hardware/Software Example utilizing the AVR Analog-to-Digital Converter Interrupt Feature


This tutorial will look at writing a simple program that utilizes the interrupt feature of the Analog-to-Digital Converter (ADC) of the ATMega16 AVR microcontroller.

In this example we will use the ADC of the AVR ATMega16 microcontroller to convert an analog voltage presented on channel ADC0 to its binary value and output that value to portD of the microcontroller. The input voltage to ADC0 is varied using the variable resistor. Below is the circuit diagram to achieve this task. The AVR C program follows the circuit diagram followed by a video.

Below is the AVR C code for the ATMega16 Interrupt feature example discussed above. This code was written in AVR Studio 5.


Important Note: In order for our example to work the internal oscillator of the ATMega16 AVR microcontroller must be enabled and set to 1MHz.

/*
 * Author:  AVR Tutoruals
 * Website: www.AVR-Tutorials.com
 *
 * Written in AVR Studio 5
 * Compiler: AVR GNU C Compiler (GCC)
 */ 
 
#include <avr/io.h>
#include <avr/interrupt.h>
 
/*ADC Conversion Complete Interrupt Service Routine (ISR)*/
ISR(ADC_vect)
{
 
	PORTD = ADCH;			// Output ADCH to PortD
	ADCSRA |= 1<<ADSC;		// Start Conversion
}
 
int main(void)
{
	DDRD = 0xFF;			// Configure PortD as output
	DDRA = 0x00;			// Configure PortA as input
					// PA0 is ADC0 input
 
	ADCSRA = 0x8F;			// Enable the ADC and its interrupt feature
					// and set the ACD clock pre-scalar to clk/128
	ADMUX = 0xE0;			// Select internal 2.56V as Vref, left justify 
					// data registers and select ADC0 as input channel 
 
	sei();				// Enable Global Interrupts
	ADCSRA |= 1<<ADSC;		// Start Conversion
 
	while(1);			// Wait forever
} 

Video demonstrating the interrupt feature of the ATMega16

AVR Tutorials hope this tutorial on the AVR Analog-to-Digital Interrupt feature was benificial to you and looks forward to your next visit.