0% found this document useful (0 votes)
61 views

Plugin ADCs

This document discusses configuring and using the analog to digital converter on an Atmega16 microcontroller. It describes the relevant registers, steps to configure the ADC including selecting a reference voltage, channel, and trigger mode. It also provides code to read ADC values and output them on a port with interrupts.

Uploaded by

Praveen Puskar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Plugin ADCs

This document discusses configuring and using the analog to digital converter on an Atmega16 microcontroller. It describes the relevant registers, steps to configure the ADC including selecting a reference voltage, channel, and trigger mode. It also provides code to read ADC values and output them on a port with interrupts.

Uploaded by

Praveen Puskar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Using ADC on an Atmega16

Baby-EDL 2009
Tutorial 2, October 2
Features
1. Adjustable 8-bit/10-bit resolution.

2. Multiplexed with pins of port A.

3. 8 multiplexed single channels, 7 multiplexed differential channels including 2 with


optional gain.

4. Auto-triggering mode

5. Sleep mode for noise canceling

6. Separate power supply at AVCC


Relevant registers

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

3. ADCL, ADCH – ADC low and high registers

4. SFIOR – Special function IO register; select auto-trigger source.


Steps for configuring ADC
1. Select Vref – Select AVCC, AREF or internal 2.56V reference (see precautions)

2. Set ADC frequency – Set the appropriate scaling factor in ADCSRA. The ADC
frequency is (clock frequency)/(scaling factor).

3. Select input channel – Channel single/differential can be selected in ADCSRA

4. Trigger – If auto trigger is desired, select in ADCSRA, and configure the trigger
source in SFIOR

5. Interrupt – If an interrupt is desired on completion of conversion, enable global


interrupts and adc interrupt. However, ADC interrupt flag is always set
upon completion of conversion.

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.

7. Enable ADC – Set ADEN bit in ADCSRA

8. Start conversion – Set ADSC bit in ADCSRA.


Precautions
1. If using AVCC or internal 2.56V reference, do not connect any other source at
AREF. This pin is directly connected to AVCC when AVCC is enabled.

2. Do not connect anything at AREF that draws current from AREF. Only connect a
capacitor (recommended) or a high-impedance element at AREF.

3. Discard the first reading, especially if input is differential.

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>

#define A_DEN 0x80 //ADC Enable, in ADCSRA register


#define A_DSC 0X40 //ADC Start Conversion, in ADCSRA register
#define A_DIF 0x10 //ADC Interrupt Flag, in ADCSRA register
#define A_DIE 0x08 //ADC Interrupt Enable, in ADCSRA register
#define A_DPS 0x06 //Scaling Factor = 64 between XTAL frequency and ADC input frequency, in ADCSRA register
#define S_REG 0x80 //Global interrupt enable, in SREG register
#define A_REF 0x00 //Select Aref as the reference voltage input, in ADMUX register
#define A_LAR 0x20 //ADC left adjust result, gives 8 MSB in ADCH, in ADMUX register
//to select channel, do an OR operation on ADMUX with 0x00 to 0x03

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
}

You might also like