0% found this document useful (0 votes)
13 views21 pages

IT343 Lecture 06

The document provides an overview of the Analog to Digital Converter (ADC) in AVR microcontrollers, specifically focusing on the ATmega32 features and programming steps. It explains the necessity of converting analog signals from sensors into digital signals for processing, detailing the successive approximation method used in ADCs. Additionally, it includes a sample program for reading data from an ADC channel and displaying the results on output ports.

Uploaded by

nevrixai
Copyright
© © All Rights Reserved
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)
13 views21 pages

IT343 Lecture 06

The document provides an overview of the Analog to Digital Converter (ADC) in AVR microcontrollers, specifically focusing on the ATmega32 features and programming steps. It explains the necessity of converting analog signals from sensors into digital signals for processing, detailing the successive approximation method used in ADCs. Additionally, it includes a sample program for reading data from an ADC channel and displaying the results on output ports.

Uploaded by

nevrixai
Copyright
© © All Rights Reserved
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/ 21

Analog to Digital Converter in AVR

Lecture 6
ADC in AVR
28 pin
(PCINT14/RESET) PC6 1 28 PC5 (ADC5/SCL/PCINT13)
(PCINT16/RXD) PD0 2 27 PC4 (ADC4/SDA/PCINT12)
(PCINT17/TXD) PD1 3 26 PC3 (ADC3/PCINT11)
(PCINT18/INT0) PD2 4 MEGA328 25 PC2 (ADC2/PCINT10)
(PCINT19/OC2B/INT1) PD3 5 24 PC1 (ADC1/PCINT9)
(PCINT20/XCK/T0) PD4 6 23 PC0 (ADC0/PCINT8)
VCC 7 22 GND
GND 8 21 AREF
(PCINT6/XTAL1/TOSC1) PB6 9 20 AVCC
(PCINT7/XTAL2/TOSC2) PB7 10 19 PB5 (SCK/PCINT5)
(PCINT21/OC0B) PD5 11 18 PB4 (MISO/PCINT4)
(PCINT22/OC0A/AIN0) PD6 12 17 PB3 (MOSI/OC2A/PCINT3)
(PCINT23/AIN1) PD7 13 16 PB2 (SS/OC1B/PCINT2)
(PCINT0/CLKO/ICP1) PB0 14 15 PB1 (OC1A/PCINT1)
ADC
◼ To understand real-world data like temperature,
humidity, pressure, position, we need
transducers, all of those measure certain
parameters and give us an electrical signal back
in the form of voltage and current.
◼ Since the majority of our devices nowadays are
digital, it becomes necessary to convert those
signals into digital signals. That is where the
ADC comes in, though there are many different
types of ADCs, the most used ADC types are
known as the successive approximation ADC.
Analog vs. Digital Signals
◼ Analog Signals: are used in many systems to produce
signals to carry information. These signals are continuous
in both values and time. In short, to understand analog
signals – all signals that are natural or come naturally
are analog signals ( Human voice, natural sound, analog
electronic devices).
◼ Digital Signals: Unlike analog signals, digital signals are
not continuous, but signals are discrete in value and
time. These signals are represented by binary numbers
(Signals used for transmission in a computer are the digital signal).
Sensors
◼ Sensor: Converts a physical signal (e.g.
light, temperature, humidity, etc.) to an
electrical signal (e.g. resistance, voltage,
current, capacitance, etc)
Signal conditioning
◼ The output of some sensors is in form of
resistance
◼ Some humidity sensor provide the result in form
of Capacitance
◼ We need to convert these signals to voltage,
however, in order to send input to an ADC. This
conversion is called signal conditioning.
ADC

DOUT

11

10

01

00 Vin
Vref(-)

Vref(+)
ADC Resolution

Vref n
stepSize = Vin ADC Output
no.ofsteps (binary number)

 Vin 
output =   Vref
 stepSize 
Successive Approximation ADC
◼ The successive approximation register is initialized so that the most
significant bit (MSB) is equal to a digital 1. This code is fed into the
DAC, which then supplies the analog equivalent of this digital code
into the comparator circuit for comparison with the sampled input
voltage.
◼ If this analog voltage exceeds Vin the comparator causes the SAR to reset this bit;
otherwise, the bit is left a 1.
◼ Then the next bit is set to 1 and the same test is done, continuing this binary
search until every bit in the SAR has been tested. The resulting code is the digital
approximation of the sampled input voltage and is finally output by the SAR at the
end of the conversion (EOC).
Successive Approximation ADC
ATmega32 ADC features
◼ It is a 10-bit ADC.

◼ It has 8 analog input channels.

◼ The converted output binary data is held by


two special function registers called ADCL
(A/D Result Low) and ADCH (A/D Result
High).
ATmega32 ADC features
◼ Because the ADCH:ADCL registers give us 16 bits
and the ADC data out is only 10 bits wide, 6 bits
of the 16 are unused.
◼ You have three options for Vref. Vref can be
connected to
1. AVCC (Analog Vcc),
2. internal 2.56 V reference, or
3. external AREF pin.
◼ The conversion time is dictated by the crystal
frequency connected to the XTAL pins (Fosc) and
ADPS0:2 bits.
ADC in AVR
System clock

ADPS
Prescaler

ADCSRA
3
ADC0 0

ADC1 1 clock
5
ADC2 2
ADC3 3
ADC4 4 10
Input ADC ADCH ADCL
ADC5 5 MUX VIN
ADC6
6
ADC7 7
VREF
Temperature sensor 8
1.1V
GND REFS0
REFS1
MUX3
MUX0
MUX1
MUX2

3 2 1 0

0 ADMUX 3 6 7
Internal

AVCC

AREF
1.1V
ADMUX
D7 D0
REFS1 REFS0 ADLAR - MUX3 MUX2 MUX1 MUX0

MUX0-MUX3: input select


ADC0 0
◼ ADC1 1

ADLAR:
ADC2 2
◼ ADC3 3
ADC4

0: right adjust the result


4
Input
◼ ADC5 5 MUX
ADC6

1: left adjust the result


6
◼ ADC7 7

REFS1-REFS0:
Temperature sensor 8
◼ 1.1V
GND
Vref selection

MUX3
MUX0
MUX1
MUX2
0 ADMUX 3 6 7
ADMUX
D7 D0
REFS1 REFS0 ADLAR - MUX3 MUX2 MUX1 MUX0

◼ MUX0-MUX3: input select


◼ ADLAR:
◼ 0: right adjust the result REFS0

1: left adjust the result


REFS1
3 2 1 0

REFS1-REFS0:

Internal

AVCC

AREF
1.1V
Vref selection
ADMUX
D7 D0
REFS1 REFS0 ADLAR - MUX3 MUX2 MUX1 MUX0

◼ MUX0-MUX3: input select


ADLAR =0
◼ ADLAR: ADCH ADCL

- ◼
- 0: right adjust the result
- - - - ADC9 ADC8 ADC7 ADC6 ADC5 ADC4 ADC3 ADC2 ADC1 ADC0

◼ 1: left adjust the result


ADLAR =1
◼ REFS1-REFS0: ADCH ADCL

ADC9 Vref selection


ADC8 ADC7 ADC6 ADC5 ADC4 ADC3 ADC2 ADC1 ADC0 - - - - - -
ADMUX
ADCSAR
ADC Prescaler
◼ PreScaler Bits let us change the clock frequency of ADC
◼ The frequency of ADC should not be more than 200 KHz
◼ Conversion time is longer in the first conversion
Steps in programming ADC
1. Make the pin for the selected ADC channel an input pin.
2. Turn on the ADC module
3. Select the conversion speed
4. Select voltage reference and ADC input channels.
5. Activate the start conversion bit by writing a one to the
ADSC bit of ADCSRA.
6. Wait for the conversion to be completed by polling the
ADIF bit in the ADCSRA register.
7. After the ADIF bit has gone HIGH, read the 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.
A program with ADC
◼ This program gets data from channel 0 (ADC0) of ;ADC
and displays the result on Port B and Port D.
int main (void)
{
DDRB = 0xFF;//make Port B an output
DDRD = 0xFF; //make Port D an output

ADCSRA= 0x87;//make ADC enable and select ck/128


ADMUX= 0xC8;//1.1V Vref, temp. sensor, right-justified

while(1)
{
ADCSRA |= (1<<ADSC);//start conversion

while((ADCSRA&(1<<ADIF))==0);//wait for conversion to finish

ADCSRA |= (1<<ADIF);

PORTD = ADCL;//give the low byte to PORTD


PORTB = ADCH;//give the high byte to PORTB
_delay_ms(100);
}

You might also like