0% found this document useful (0 votes)
55 views11 pages

Lecture 5 EPMN201

The document discusses analog to digital conversion in AVR microcontrollers. It describes the 10-bit ADC with 8 input channels in the ATmega32 microcontroller. The ADC converts the analog voltage to a 10-bit binary number based on the reference voltage. The conversion results are stored in the ADCL and ADCH registers. It also describes the ADC multiplexer selection register ADMUX and ADC control and status register ADCSRA which controls the conversion process.

Uploaded by

Engy Ahmed
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)
55 views11 pages

Lecture 5 EPMN201

The document discusses analog to digital conversion in AVR microcontrollers. It describes the 10-bit ADC with 8 input channels in the ATmega32 microcontroller. The ADC converts the analog voltage to a 10-bit binary number based on the reference voltage. The conversion results are stored in the ADCL and ADCH registers. It also describes the ADC multiplexer selection register ADMUX and ADC control and status register ADCSRA which controls the conversion process.

Uploaded by

Engy Ahmed
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/ 11

3/20/2022

Analog to Digital converter in AVR


Lecture 5

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)

1
3/20/2022

ADC Resolution

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

 Vin 
output    Vref
 stepSize 

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).

2
3/20/2022

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

3
3/20/2022

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

4
3/20/2022

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

5
3/20/2022

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

6
3/20/2022

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);
}
}

7
3/20/2022

A program with ADC


 This program gets data from the integrated Temperature sensor and
displays the result on Port B and Port D.
ADCSRA

#include <avr/io.h>
#define F_CPU 16000000UL
ADMUX
REFS0
int main (void) REFS1
{ 3 2 1 0
DDRB = 0xFF;//make Port B an output
DDRD = 0xFF; //make Port D an output

Internal
ADC0

AVCC
0

AREF
1.1V
ADCSRA= 0x87;//make ADC enable and select ck/128 ADC1 1
ADMUX= 0xC8;//1.1V Vref, temp. sensor, right-justified ADC2 2
ADC3 3
while(1)
{ ADC4 4
Input
ADCSRA |= (1<<ADSC);//start conversion ADC5 5 MUX
ADC6
6
while((ADCSRA&(1<<ADIF))==0);//wait for conversion to finish ADC7 7

Temperature sensor 8
ADCSRA |= (1<<ADIF);
1.1V

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


PORTB = ADCH;//give the high byte to PORTB

MUX3
MUX0
MUX1
MUX2
}
0 3 6 7
} ADMUX

Sensors
 Sensor: Converts a physical signal (e.g.
light, temperature, humidity, etc.) to an
electrical signal (e.g. resistance, voltage,
current, capacitance, etc)

8
3/20/2022

LM35 & LM34 (Temperature Sensors)


 LM35 and LM34:
 convert temp. to voltage
 10mV for each degree

Using LM35
//this program reads the sensor and displays it on Port B
#include <avr/io.h> //standard AVR header VCC

int main (void)


{ ADC0

DDRB = 0xFF; //make Port B an output LM3x


ATmega 328
DDRC = 0;//make Port C an input for ADC input
ADCSRA = 0x87;//make ADC enable and select ck/128
ADMUX = 0xC0;//1.1V Vref, ADC0, right-justified

while (1){
ADCSRA |= (1<<ADSC);//start conversion
while((ADCSRA&(1<<ADIF))==0); //wait for end of conversion
ADCSRA |= (1<<ADIF); //clear the ADIF flag
PORTB = (ADCL|(ADCH<<8))*10/93;//PORTB = adc value/9.3
}
}
Note: 1.1 Volt input ----> 1023 ADC value
110 degree ----> 1023 ADC value
===> T = ADC value /1023 * 110 = ADC value /93 *10`

9
3/20/2022

Thermistor (a temperature sensor)


 Converts temperature to resistance
 It is not linear

Signal conditioning
 The output of some sensors (e.g. PT100)
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.

10
3/20/2022

DAC
Vout

4
3
2

VREF 1
Step size = 0 input
Num of steps 00 01 10 11

VOUT = num × step size

Binary number n
(input) DAC VOUT

Vref

Connecting a DAC to the microcontroller

Micro. +5V +5V


DAC0808
5K
Vcc
Vref(+)
Vref(-)
PD0 D0 5K 5k

To scope
OUT –
VOUT = 0 to 10V
+ 1k
Comp
0.1uF
100 pF
PD7 D7
VEE -12V

GND

11

You might also like