Class4_ADC_0693a6b07b7fc054c7e4bc2a9c5b6937
Class4_ADC_0693a6b07b7fc054c7e4bc2a9c5b6937
Microcontrollers and
Embedded Systems
Week 6: ADC, DAC, and Sensor Interfacing
1
Learning Objectives
2
ATmega328 Pin-out
3
Analog to Digital Conversion
4
Analog to Digital Conversion
• Factors:
- Resolution: No. bits
(quantization)
8, 10, 12, 16, 24
5
Analog to Digital Conversion
• Factors:
- Range/Resolution (No. bits)
- Speed
6
ADC Output
• Ratios
𝑉𝑖𝑛
- 𝐷𝑜𝑢𝑡 =
𝑠𝑡𝑒𝑝 𝑠𝑖𝑧𝑒
• Channels
• Parallel vs serial
7
ADC Types
• Flash ADC
Source: https://www.digikey.com/en/articles/match-the-right-adc-to-the-application
8
ADC Types
Source: https://www.maximintegrated.com/en/design/technical-documents/tutorials/1/1080.html
9
ADC: ATMega328P
• 10-bit resolution • Temperature sensor input channel
• 0.5 LSB integral non-linearity • Optional left adjustment for ADC
• ±2 LSB absolute accuracy result readout
• 13 - 260μs conversion time • 0 - VCC ADC input voltage range
• Up to 76.9ksps (up to 15ksps at • Selectable 1.1V ADC reference
maximum resolution) voltage
• Six multiplexed single ended input • Free running or single conversion
channels mode
• Two additional multiplexed single • Interrupt on ADC conversion
ended input channels (TQFP and complete
VFQFN package only) • Sleep mode noise canceler
10
11
ADC Registers
ADMUX REFS1 REFS0 ADLAR MUX[3:0]
ADCL ADCL[7:0]
ADCH ADCH[7:0]
DIDR0 ADCxD[7:0]
12
ADMUX Register
ADMUX REFS1 REFS0 ADLAR MUX[3:0]
13
ADMUX Register
ADMUX REFS1 REFS0 ADLAR MUX[3:0]
15
ADCSRA Register
ADCSRA ADEN ADSC ADATE ADIF ADIE ADPSS0[2:0]
16
ADCSRA Register
ADCSRA ADEN ADSC ADATE ADIF ADIE ADPSS0[2:0]
19
ADC Timing
Condition Sample and Hold Time (Cycles) Total Conversion Time (Cycles)
First Conversion 14.5 25
Normal Conversion 1.5 13
Auto trigger conversion 1.5/2.5 13/14 20
ADC: ATMega328P
21
ADC Data Conversion Steps
1. Select analog voltage reference REFS[1:0] in ADMUX register.
2. ADLAR for left/right adjustment (8- or 10-bit)
3. Select the analog channel from MUX[3:0]
4. Select the conversion speed ADPS[2:0]
5. ADEN=1 to enable AD (Reg. ADCSRA)
(if interrupt is required, enable before this step by setting ADIE)
6. Set ADSC=1 to start conversion
7. Wait until the conversion is done
1. Polling: ADSC==0 or ADIF==1 (Clear the flag afterwards)
2. ISR
8. Read ADC (or ADCH for 8-bit)
22
Example
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);
}
}
23
ADC in EE470avr.h
1. adc_init():
- enable AD, ck/128, AVCC=Vref. Do not forget to connect pin 20 (AVCC) to pin 7 (VCC)
2. adc_channel(ch):
- ch=[0,5]
- select analog ch. Configure the corresponding pin as an input pin.
3. v1=adc_read():
- read from channel ch the sample value as an integer (0,1023) that corresponds to
voltages (0,+5V)
24
Digital-to-Analog Converter
26
DAC
27
DC – R-2R Ladder
28
DAC
29
DAC
To scope
OUT –
VOUT = 0 to 10V
+ 1k
Comp
0.1uF
100 pF
PD7 D7
VEE -12V
GND
30
DAC – Example
• R=5k Ohms and Iref=2mA, calculate Vout for the following binary inputs:
- 0b10011001
- 0b11001000
31
Analog Comparator
32
Analog Voltage Comparator
33
Overview
34
AC Registers
35
ADCSRB
36
ACSR
37
ACSR
40
Sensors
41
Thermistor
• Nonlinear
• R(T):
1 1
𝐵 𝑇−𝑇
- 𝑅 = 𝑅0 𝑒 0
42
Temperature Sensor
• LM35:
• LM34A:
- -50F to +300F, accuracy +2.0F with output 10mV/F
• LM35A:
- -55C to +150C, accuracy +1.0C with output 10mV/C
• LM34 & LM35 requires NO external calibration
- they are INTERNALLY calibrated.
43
AVR Example
/* Read the temperature using LM35 then display it on the PC screen.
Use channel 0 (ADC0= pin PC0)
*********************************************/
#include "ee470avr.h"
int main(void) {
unsigned int v1, v0 = 0;
char xline[80];
serial_init(103); // 9600 bps, 16MHz.
bitclr(DDRC, PC0); // input pin PC0=ADC0
adc_init(); // enable AD, ck/128, AVCC=Vref
adc_channel(0); // select analog ch 0.
while (1) {
v1 = adc_read();
if (v1 != v0) {
v0 = v1;
sprintf(xline, "v1=%d,v in volts=%g,T=%g degrees\r\n",
v1,v1*5./1023.,(v1*5./1023.)/.01);
putstr(xline);
}
}
}
44