Plugin ADCs
Plugin ADCs
Baby-EDL 2009
Tutorial 2, October 2
Features
1. Adjustable 8-bit/10-bit resolution.
4. Auto-triggering mode
1. ADMUX – ADC mux. selection register. Selection of Vref, ADC channel (single
channel or differential, channel gain), Left adjust result
2. ADCSRA – ADC control and status register. ADC enable, start conversion,
Auto-trigger enable, interrupt flag, interrupt enable, prescaler bits
2. Set ADC frequency – Set the appropriate scaling factor in ADCSRA. The ADC
frequency is (clock frequency)/(scaling factor).
4. Trigger – If auto trigger is desired, select in ADCSRA, and configure the trigger
source in SFIOR
6. 8/10 bit – If 8 bit resolution is desired, set ADLAR bit in ADMUX. 2 LSBs are
discarded and the 8 MSBs are shifted to ADCH.
2. Do not connect anything at AREF that draws current from AREF. Only connect a
capacitor (recommended) or a high-impedance element at AREF.
4. If left-adjustment is not done i.e. in 10-bit mode, read ADCL first, then ADCH.
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/iom16.h>
#include <stdlib.h>
#include <stdio.h>
int main(void){
sei();
SREG |= S_REG; //Global Interrupt enable
DDRC = 0xFF; //Configure Port C pins as output
ADMUX |= A_REF; //set voltage source for ADC
ADCSRA |= A_DPS; //set ADC frequency
ADCSRA |= A_DEN; //ADC enable
ADCSRA |= A_DIE; //ADC interrupt enable
ADMUX |= A_LAR; //left adjust result
ADMUX |= 00; //Selec ADC0 as input
ADCSRA |= A_DSC; //Start conversion
while(1){
}
}
ISR(ADC_vect){
PORTC = ADCH; //Read ADC value and write it to Port C
SREG |= S_REG; //Re-enable interrupts
ADCSRA |= A_DIE; //ADC interrupt enable
ADCSRA |= A_DSC; //Start conversion
}