Analog to digital conversion in AVR Microcontrollers

Microprocessors, Lecture 10:
Analog to digital conversion in AVR
Microcontrollers
(Chapter 13 of the text book)
Contents
• ADC units of ATmega32
• ADC programming in C
University of Tehran 2
ADC in AVR
University of Tehran 3
ADC
• Typical usage of ADC (analog to digital
convertors)
University of Tehran 4
ADC
• A typical ADC
• Vref= the maximum allowable volatge
University of Tehran 5
ADC resolution
• Step size: the difference between two
consecutive output nembers
• For a 8-bit output and Vref=5v,
– step size= 5/246
University of Tehran 6
ADC internal structure
• Successive approximation method
– See page 468 of the book for algorithm details
• n steps for an n-bit output ADC
• Requires n cycles to calculate digital output
University of Tehran 7
ADC in ATmega32
• 7 analog input
channels
– Just one ADC
multiplexed by
input channels
• 10-bit output
– Kept in ADCL and
ADCH registers
– 6 unused bits, can
be set the upper or
lower 6 bits
University of Tehran 8
AVR ADC Registers
• ADCL and ADCH to keep digital data
• ADCSRA to control ADCs
• ADMux to select one input channel for
conversion
• SPIO, special function register
University of Tehran 9
ADMUX register
University of Tehran 10
ADMUX register
• Select the reference voltage (the maximum
acceptable input channel voltage)
University of Tehran 11
ADMUX register
• Which bits of ADCH and ADCL are unused
University of Tehran 12
ADMUX register
• Which channel is selected to the ADC
– Can also work in differential mode
University of Tehran 13
ADCSRA register
• ADC control and status register
– Control and monitor the ADC
University of Tehran 14
ADCSRA register
• ADATE bit
• Sets the operation mode of ADC
• 0= single conversion
– Converts the input just one time every time ADSC
becomes 1
• 1= free running
– Continuously converts the input to digital values
with some frequency
– For ADATE=1 we have more ADC options, Take a
look at AVR documents for more details if you are
interested!
University of Tehran 15
ADCSRA register
• Selecting the ADC clock
University of Tehran 16
ADC clock
• Determines the speed of sampling the input
data
– Each conversion takes around 13 ADC clocks
• Larger frequency has smaller accuracy
• In AVR, frequency have to be less than 200
kHz
• Set it to smaller frequency (ADPS[0..2]=111) if
speed is not critical
University of Tehran 17
ADC programming in C
University of Tehran 18
ADC programming in C
A program that coverts the analog voltage of ADC0 and copies it to
portD and portB
--polling method
•The ADSC bit
should be 1 for
the ADC to start
conversion
•Returns to 0
automatically
once the
conversion is
completed
University of Tehran 19
ADC programming in C
A program that coverts the analog voltage of ADC0 and copies it to
portD and portB
--interrupt method
University of Tehran 20
Connecting sensors to AVR
• Transducer (sensor): convert
environment parameters
(temperature, pressure,
velocity,…) to electrical
quantities (voltage, current,
capacitance, resistance)
• Signal conditioning:
converting electrical
quantities to voltage
University of Tehran 21
Connecting sensors to AVRexample
University of Tehran 22
Connecting LM35 to AVR
• In AVR:
– Vref=2.56v
– 10 bit output, 1024 values Î step size=2.56/1024= 2.5
mv
• In LM34:
– for 1 degree increase in temperature, we have 10mv
increase in output voltage
ÎIf the temperature increases by one degree:
– 10mv increase in sensor output
– 10/2.5=4 increase in ADC output
• Divide the ADC result by 4 to get actual
temperature (shift right 2 times)
University of Tehran 23
Connecting LM35 to AVR
•
•
Example: temp= 20 degrees, sensor output= 200mv (10mv for every
degree), this voltage is converted to 0x1010000=80
(200mv/2.5mv=80) by the ADC
Divide by 4 to get actual temperature by ADC
University of Tehran 24
ADC programming in C in
CodeVision
• Set different values in the project setup wizard
University of Tehran 25