Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Adc Dac and Sensor Interfacing - Part 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

ADC DAC and

Sensor Interfacing
- Part 2
LECTURE# 20
MICROPROCESSOR SYSTEMS AND INTERFACING

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 1


Last Lecture
Analog to Digital and Digital to Analog Conversion
ADC Characteristics
1
◦ Resolution, Conversion Time ≈ Conversion Rate
, and Reference Voltage
◦ Effect of ADC Resolution and Sample Rate
◦ Steps (Quantization Levels) and Step Size
𝑉𝑖𝑛 𝑉𝑖𝑛
ADC Digital Output 𝐷𝑜𝑢𝑡 = × 2𝑟𝑒𝑠𝑜𝑙𝑢𝑡𝑖𝑜𝑛 =
𝑉𝑟𝑒𝑓 𝑆𝑡𝑒𝑝 𝑆𝑖𝑧𝑒

ADC Operation (Multiple Channels, Start of Conv, and End of Conv.)


ADC Programming in AVR
◦ Reference Selection, Channel Selection, and Clock Pre-scalar Selection,
◦ Auto-Trigger, Digital Input Disable

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 2


AVR ADC Block Diagram

Reference Selection

SAR ADC

Analog input channel selection

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 3


ADC Power Connections
Additionally, in case of VREF
◦ VREF = AVCC or Internal 1.1V
◦ Connect a capacitor to AREF pin
◦ AVREF = External AREF
◦ connect a stable reference voltage
source to AREF

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 4


ADC Steps for polling based
programming
1. Make the pin for the selected ADC channel an input pin.
2. Turn on the ADC module of the AVR because it is disabled upon power-on reset to
save power.
3. Select the conversion speed. We use registers ADPS2:0 to select the conversion
speed.
4. Select voltage reference and ADC input channels. We use:
◦ REFS0 and REFS1 bits in the ADMUX register to select voltage reference, and,
◦ MUX3:0 bits in ADMUX register to select the ADC input channel.
5. Activate the start conversion bit by writing a one to the ADSC bit of ADCSRA reg..
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.
◦ You have to read ADCL before ADCH; otherwise, the result will not be valid.
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.

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 5


ADC Programming in AVR
Summary
ADMUX REFS1 REFS0 ADLAR - MUX3 MUX2 MUX1 MUX0 ADPS2:0 ADC Clock
a 0 0 0 Fosc/2
ADCSRA ADEN ADSC ADATE ADIF ADIE APDS2 ADPS1 ADPS0
0 0 1 Fosc/2
ADCSRB - ACME - - - ADTS2 ADTS1 ADTS0
0 1 0 Fosc/4
DIDR0 - - ADC5D ADC4D ADC3D ADC2D ADC1D ADC0D 0 1 1 Fosc/8
MUX3:0 ADC Channel ADTS2:0 Auto Trigger Source 1 0 0 Fosc/16
0 0 0 Free Running mode 1 0 1 Fosc/32
0000 ADC0
1 1 0 Fosc/64
0001 ADC1 0 0 1 Analog Comparator
1 1 1 Fosc/128
⋮ ⋮ 0 1 0 External Interrupt Request 0
0 1 1 Timer/Counter0 Compare Match A REFS1:0 Vref
0111 ADC7
1 0 0 Timer/Counter0 Overflow 0 0 AREF pin, external
1000 ADC8 (Temp. Sensor)
0 1 AVCC pin (VCC)
1110 1.1 V (VBG) 1 0 1 Timer/Counter1 Compare Match B
1 0 Reserved
1111 0 V (GND) 1 1 0 Timer/Counter1 Overflow
1 1 Internal 1.1 V
1 1 1 Timer/Counter1 Capture Event

ADCH ADCL
ADLAR = 0 x x x x x x D9 D8 D7 D6 D5 D4 D3 D2 D1 D0
ADLAR = 1 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0 x x x x x x

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 6


Assembly Example
Program 13-1
Takes input from ADC channel 0 and display on PORTB and PORTD
.INCLUDE "M32DEF.INC"
LDI R16, 0xFF
OUT DDRB, R16 ;make Port B an output
OUT DDRD, R16 ;make Port D an output
LDI R16, 0
OUT DDRA, R16 ;make Port A an input for ADC
LDI R16, 0x87 ;R16 = 0b 1000 0111
OUT ADCSRA, R16 ;enable ADC and select ck/128
LDI R16, 0xC0 ;R16 = 0b 1100 0000
OUT ADMUX, R16 ;2.56V Vref, ADCO single ended input, right-justified data
READ_ADC: SBI ADCSRA, 6 ;start conversion
KEEP_POLING: SBIS ADCSRA, 4 ;skip next instruction, if ADIF is set ;wait for end of conversion
RJMP KEEP_POLING ;keep polling ADIF flag
SBI ADCSRA, 4 ;write 1 to clear ADIF flag
IN R16, ADCL ;read ADCL first
OUT PORTD,R16 ;give the low byte to PORTD
IN R16, ADCH ;read ADCH after ADCL
OUT PORTB,R16 ;give the high byte to PORTB
RJMP READ_ADC ;keep repeating it

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 7


C Example
Program 13-1C
#include <avrio.h> //standard AVR header
int main (void)
{
DDRB = OxFF; //make Port B an output
DDRD = OxFF; //make Port D an output
DDRA = 0; //make Port A an input for ADC input
ADCSRA= 0x87; //make ADC enable and select ck/128
ADMUX= OxCO; //2.56V Vref, ADCO single ended input
//data will be right-justified
while (1)
{
ADCSRA|=(1<<6); //start conversion
while((ADCSRA&(1<<4))==0); //wait for conversion to finish
PORTD = ADCL; //give the low byte to PORTD
PORTB = ADCH; //give the high byte to PORTS
}
return 0;
}

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 8


Using Interrupt
ADC conversion complete ROM interrupt vector address 0x20
For C language use function ISR(ADC_vect);

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 9


Sensor Interfacing
LM35
LM35 Converts Temperature in to electrical signal
◦ Three pin (VCC, VOUT and GND)
◦ 10 mV/C
◦ Each degree increases the output of the sensor by 10mV and vice versa

What is the most suitable choice for 𝑉𝑟𝑒𝑓 ?


Write and program to display temperature value on PORTD
◦ In assembly and C

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 10


LM35
Voltage Reference (5 V)
If we use 𝑉𝑟𝑒𝑓 = 5 V Dout Vin (mV) Temperature (°C)
1 4.88 0.488
◦ Step size = 4.88 mV (for 10-bit ADC of 2 9.76 0.976
AVR) 3 14.64 1.464
◦ 10mV/°𝐶 is not a multiple of step size 4 19.52 1.952
5 24.4 2.44
◦ So it will be difficult for calculating 8 39.04 3.904
temperature value 10 48.8 4.88
◦ Consider Dout = 50 20 97.6 9.76
30 146.4 14.64
◦ 𝑉𝑖𝑛 = 50 × 4.88 mV = 244.14 mV 40 195.2 19.52
244.14 𝑚𝑉
◦ Then Temperature will be = 50 244 24.4
10 𝑚𝑉/𝐶
80 390.4 39.04
24.41 °𝐶, which is not a proper value
100 488 48.8
200 976 97.6
Look at the table and fractional values of 400 1952 195.2
Temperature it results 800 3904 390.4
1000 4880 488
1024 4997.12 499.712

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 11


LM35
Voltage Reference (2.56 V)
If we use 𝑉𝑟𝑒𝑓 = 2.56 V Dout Vin (mV) Temperature (°C)
1 2.5 0.25
◦ Step size = 2.5 mV (for 10-bit ADC) 2 5 0.5
◦ 10mV/°𝐶 is a multiple of step size (2.5 mV) 3 7.5 0.75
4 10 1
◦ Consider Dout = 50 5 12.5 1.25
◦ 𝑉𝑖𝑛 = 50 × 2.5 mV = 125 mV 8 20 2
125 𝑚𝑉 10 25 2.5
◦ Temperature will be = 12.5 °𝐶
10 𝑚𝑉/𝐶 20 50 5
30 75 7.5
The temperature value result is proper 40 100 10
50 125 12.5
If we want to have Dout = temperature 80 200 20
◦ Divide the result by 4 100 250 25
200 500 50
◦ Use ADLAR = 1 and ignore the LSB two bits 400 1000 100
in ADCL 800 2000 200
◦ Read only ADCH (which is temperature) 1000 2500 250
1024 2560 256

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 12


LM35
Voltage Reference (1.1 V)
If we use 𝑉𝑟𝑒𝑓 = 1.1 V Dout 𝐕𝐢𝐧 (mV) Temperature (°C)
1 1.07 0.1
◦ Step size = 1.074 mV (for 10-bit ADC) 2 2.15 0.2
◦ 10mV/°𝐶 is not a multiple of step size 3 3.22 0.3
4 4.30 0.4
◦ So, it won’t be straight-forward calculation 5 5.37 0.5
◦ Consider Dout = 50 8 8.59 0.9
◦ 𝑉𝑖𝑛 = 50 × 1.074mV = 53.71 mV 10 10.74 1.1
20 21.48 2.1
53.71 mV
◦ Then Temperature will be = 5.4 °𝐶, 30 32.23 3.2
10 mV/𝐶
which is not a proper value 40 42.97 4.3
50 53.71 5.4
80 85.94 8.6
Look at the table and fractional values of 100 107.4 10.7
Temperature it results 200 214.8 21.5
400 429.7 43.0
800 859.4 85.9
1000 1074 107.4
1024 1100 110.0

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 13


DAC

Saad Arslan COMSATS UNIVERSITY ISLAMABAD 14

You might also like