4 AVR ADCs
4 AVR ADCs
4 AVR ADCs
AVR Lecture 4
Analog to Digital Converter
Some major characteristics of ADCs
◼ Resolution
❑ The ADC has n-bit resolution, where n can be
8,10,12,16 or even 24 bits
❑ Higher resolution ADCs have smaller step size,
where step size is the smallest change that can be
discerned by an ADC
2 / 74
Analog to Digital Converter
◼ Reference Voltage - Vref
❑ Vref is an input voltage used for reference voltage.
❑ The Reference voltage, along with the resolution of
the ADC defines the step size.
❑ For an n-bit ADC step size is given by
𝑉𝑟𝑒𝑓
Step size =
2𝑛
❑ For example if analog input voltage varies from 0 to
4 volts, Vref is connected to 4 volts
❑ If it is an 8 bit ADC than
step size = 4/256 = 15.62mV
3 / 74
Analog to Digital Converter
◼ Digital data output
❑ The digital output can be calculated as
𝑉𝑖𝑛
Dout =
𝑠𝑡𝑒𝑝 𝑠𝑖𝑧𝑒
𝑉𝑖𝑛
Dout = x2𝑛
𝑉𝑟𝑒𝑓
n= resolution of ADC (8,10,12,16,24 etc)
Dout= Digital Output in Decimal
4 / 74
Successive Approximation ADC
◼ If Vref =2.56 volt and 8-bit successive approximation
ADC is used, what will be the ADC output (Digital) if
input is 1 volt.
10 / 74
ATmega16 ADCs
◼ The ATmega16 features a 10-bit successive approximation
ADC(Successive Approximation ADC)
◼ The ADC converts an analog input voltage to a 10-bit
digital value through successive approximation
◼ The ADC is connected to an 8-channel Analog Multiplexer
which allows 8 single-ended voltage inputs constructed
from the pins of Port A
◼ The analog input channel are selected by writing to the
MUX bits in ADMUX
◼ The ADC contains a Sample and Hold circuit which
ensures that the input voltage to the ADC is held at a
constant level during conversion
13 / 74
Atmega16 ADC General Steps
◼ Starting/Powering up the ADC (Enabling)
◼ Selecting the triggering source
◼ Selecting the Input channel
◼ Selecting the ADC conversion speed i.e. ADC clock
◼ Selecting the reference voltage
◼ Enabling the interrupts if needed
◼ Reading the ADC conversion data
15 / 74
ATmega16 ADCs Operation
◼ The ADC is enabled by setting the ADC Enable bit,
ADEN in ADCSRA Voltage reference and input channel
selections will not go into effect until ADEN is set.
ADC Control and Status Register A – ADCSRA
16 / 74
Manual Triggering
◼A single conversion is started by writing a logical
one to the ADC Start Conversion bit, ADSC
ADC Control and Status Register A – ADCSRA
18 / 74
Auto Triggering Sources
◼ A conversion can be triggered automatically by
various sources
◼ Auto Triggering is enabled by setting the ADC Auto
Trigger Enable bit, ADATE in ADCSRA
ADC Control and Status Register A – ADCSRA
20 / 74
Setting the Trigger Source
◼ Triggering Sources
21 / 74
Setting the Trigger Source
◼ The trigger source is selected by setting the ADC
Trigger Select bits, ADTS in SFIOR
◼ The ADC will start a conversion on a positive edge
of the selected trigger signal.
◼ If another positive edge occurs on the trigger signal
during conversion, the edge will be ignored
22 / 74
ADC Free Running Mode
◼ In Free Running mode, the ADC constantly samples
and updates the ADC Data Register
◼ The first conversion must be started by writing a
logical one to the ADSC bit in ADCSRA
23 / 74
Prescaling and Conversion
◼ The ADC module contains a pre-scaler, which
generates an acceptable ADC clock frequency
◼ The pre-scaling is set by the ADPS bits in ADCSRA
24 / 74
Prescaling and Conversion
◼ Bits 2:0 – ADPS2:0: ADC Pre-scaler Select Bits
❑ These bits determine the division factor between the XTAL
frequency and the input clock to the ADC
25 / 74
Prescaling and Conversion
◼ The successive approximation circuitry requires an
input clock frequency between 50 kHz and 200 kHz
to get maximum resolution
26 / 74
ADC Conversion Result
◼ The ADC generates a 10-bit result which is presented in
the ADC Data Registers ADCH and ADCL
30 / 74
ADC Conversion Result
◼ The ADC has 10 BIT resolution, therefore its result
is saved in TWO registers, ADCH and ADCL.
◼ ADCL must be read first, then ADCH. When ADCL
is read ADC’s access to data registers is blocked.
This ensures that the Data obtained belongs to the
same conversion
◼ Once ADCL is read, and if the ADC completes a
conversion before ADCH is read, the registers are
not updated, hence the data from that conversion is
lost.
◼ When ADCH is read, ADC access to the ADCH and
ADCL Registers is re-enabled
31 / 74
ADC Conversion Result
◼ The ADC generates a 10-bit result which is presented in
the ADC Data Registers ADCH and ADCL
◼ By default, the result is presented right adjusted
32 / 74
ADC Conversion Result
◼ The ADC Data Register – ADCL and ADCH
33 / 74
ADC Conversion Result
◼ The ADC generates a 10-bit result which is presented in
the ADC Data Registers ADCH and ADCL
◼ By default, the result is presented right adjusted
34 / 74
ADC Conversion Result
ADC 8-bit output data
◼ If the result is left adjusted and no more
than 8-bit precision is required, it is
sufficient to read ADCH
36 / 74
ADC Voltage Reference
◼ADC reference voltage is selected by the bits in
ADMUX register
ADC Multiplexer Selection Register – ADMUX
37 / 74
Summary of Pin Description
PortA
PortB
PWM Channel
timer0
Pins Used for
Programming
Ref voltage for ADC
5V Ground
Voltage Supply for ADC
External Crystal
PortC
External interrupt0
PortD
External interrupt1
PWM channels
timer1 PWM Channel timer2
38 / 74
ADC Voltage Reference
◼ AVCC is the supply voltage pin for A/D convertor. It should
be externally connected to VCC, even if the ADC is not used
◼ AREF is the ref voltage pin for ADC
39 / 74
ADC Voltage Reference
◼ Voltage Reference Selections for ADC
REFS1 REFS0 Voltage Reference Selection
0 0 AVREF
0 1 AVCC
1 0 Reserved
1 1 Internal 2.56 V
40 / 74
ADC Voltage Reference
◼ The reference voltage for the ADC (VREF) indicates
the conversion range for the ADC
◼ VREF can be selected as either
❑ AVCC
❑ Internal 2.56V reference
❑ External AREF pin
41 / 74
Channel Selection
◼ Input channel for ADC conversion is selected
through the MUXn bits in the ADMUX register
44 / 74
ADC Interrupt
◼ ADC can generate an interrupt every time a
conversion is completed
◼ ADC interrupt is controlled by following bit
46 / 74
ADC Interrupt
◼ Bit 4 – ADIF: ADC Interrupt Flag
❑ This bit is set when an ADC conversion completes and the
Data Registers are updated
❑ ADIF is cleared when executing the corresponding interrupt
handling vector
❑ Alternatively, ADIF is cleared by writing a logical one to the
flag
47 / 74
Steps in programming ADC using polling
1. Make pin for the selected ADC channel as input pin
2. Turn on the ADC module of AVR by setting ADEN
bit in ADCSRA register
3. Select the conversion speed by setting values of
bits ADPS2:0 in ADCSRA register
4. Select voltage reference by setting bits REFS0 and
REFS1 in ADMUX register, and select the input
channel by setting bit MUX4:0 in ADMUX
5. Activate the start conversion bit by writing one to
ADSC bit of ADCSRA
6. Wait for the conversion to be completed by polling
ADIF bit in ADCSRA register
50 / 74
Steps in programming ADC using polling
7. After ADIF bit has gone high, Read ADCL and ADCH
registers to get the digital data output
8. If you want to read the selected channel again, go back
to step 5
9. If you want to select another Vref source or input
channel, go back to step 4.
51 / 74
Example
◼ Use the ADC with AVCC as the VREF, make use of
single ended conversions to check the value of an
analog voltage connected with ADC0 and display its
digital value on port B. (Use INTERRUPT and manual
conversion)
If we use internal oscillator at 8MHz than set the
prescaler such that the ADC clock has the frequency of
125kHz. [Use Channel Zero]
53 / 74
Solution Code
#include<avr/io.h>
#include<avr/interrupt.h>
54 / 74
Solution (Setting Value of ADSCRA)
◼ ADCSRA
55 / 74
Solution (Setting Value of ADSCRA)
◼ Now setting prescalar, 8MHz/125kHz=64
58 / 74
Solution (Setting Value of ADMUX)
◼ Now we select reference voltage by setting bits of
register ADMUX
◼ For VREF=AVCC
REFS1=0, REFS0=1
60 / 74
Solution (Setting Value of ADMUX)
◼ As we are interested in 8-bit result only so we
set ADLAR=1 so that result is left adjusted in
ADCH and ADCL register
ADLAR=1
◼ Mux[0] = 000
◼ Hence
ADMUX|=0b01100000
61 / 74
Solution Code
#include<avr/io.h>
#include<avr/interrupt.h>
63 / 74
Example
◼ Interrupt
◼ Polling
64 / 74
Solution Code (using interrupt)
#include<avr/io.h>
#include<avr/interrupt.h>
while(1)
{ ADC_Calc();
}
}
67 / 74
Using polling (Only) by checking Int Flag ADIF
void ADC_Calc()
{
ADMUX=0x60;//selecting channel 0
ADCSRA|=0x40;// starting conversion
while(!(ADCSRA&0x10)); //waiting for Interrupt flag ADIF to get set
PORTB=ADCH;
ADMUX=0x61;//selecting channel 1
ADCSRA|=0x40;// starting conversion
while(!(ADCSRA&0x10)); //waiting for Interrupt flag to get set
PORTC=ADCH;
ADMUX=0x62;//selecting channel 2
ADCSRA|=0x40;// starting conversion
while(!(ADCSRA&0x10)); //waiting for Interrupt flag to get set
PORTD=ADCH;
}
68 / 74
Using polling (Only) by checking ADSC Flag
void ADC_Calc()
{
ADMUX=0x60;//selecting channel 0
ADCSRA|=0x40;// starting conversion
while((ADCSRA&0x40));//waiting for the start conversion bit ADSC to get clear
PORTB=ADCH;
ADMUX=0x61;//selecting channel 1
ADCSRA|=0x40;// starting conversion
While((ADCSRA&0x40)); //waiting for the start conversion bit to get clear
PORTC=ADCH;
ADMUX=0x62;//selecting channel 2
ADCSRA|=0x40;// starting conversion
while((ADCSRA&0x40)); //waiting for the start conversion bit to get clear
PORTD=ADCH;
}
69 / 74