0% found this document useful (0 votes)
9 views

Class4_ADC_0693a6b07b7fc054c7e4bc2a9c5b6937

Uploaded by

a7madafefa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Class4_ADC_0693a6b07b7fc054c7e4bc2a9c5b6937

Uploaded by

a7madafefa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

EE470

Microcontrollers and
Embedded Systems
Week 6: ADC, DAC, and Sensor Interfacing

Dr. Ahmad Bashaireh


Jordan University of Science and Technology
Spring 2024

1
Learning Objectives

• Understand the function and need of DACs and ADCs


• Describe analog-to-digital converter (ADC) chips.
• Know how to program ADC in ATMega328P

2
ATmega328 Pin-out

3
Analog to Digital Conversion

• One of the most widely used devices for data acquisition


- MCU: digital
- Real world: analog (e.g. temperature, pressure, humidity, velocity)
• Transducer (aka sensor): converts a physical quantity to an electrical
(voltage, current) signals

4
Analog to Digital Conversion

• Factors:
- Resolution: No. bits
(quantization)
8, 10, 12, 16, 24

- Speed: clock source


(sampling rate/throughput)

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

• Successive Approximation ADC

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]

ADCSRA ADEN ADSC ADATE ADIF ADIE ADPSS0[2:0]

ADCSRB ACME ADTS[2:0]

ADCL ADCL[7:0]

ADCH ADCH[7:0]

DIDR0 ADCxD[7:0]

ACSR ACD ACBG ACO ACI ACIE ACIC ACIS[1:0]

12
ADMUX Register
ADMUX REFS1 REFS0 ADLAR MUX[3:0]

• REFS: Reference Selection


- The change will not go in effect until this conversion is complete
- The internal voltage reference options may not be used if an external reference voltage is being applied to the AREF pin.

REFS[1:0] Voltage Reference Selection


00 AREF, Internal Vref turned off
01 AVCC with external capacitor at AREF pin
10 Reserved
11 Internal 1.1V Voltage Reference with external capacitor at AREF pin

13
ADMUX Register
ADMUX REFS1 REFS0 ADLAR MUX[3:0]

• MUX: Analog Channel Selection


- If these bits are changed during a conversion, the MUX[3:0] Single Ended Input
change will not go in effect until this conversion is
0000 ADC0
complete
0001 ADC1
0010 ADC2
0011 ADC3
0100 ADC4
0101 ADC5
0110 ADC6
0111 ADC7
1000 Temperature sensor
1001 - 1101 Reserved
1110 1.1V (VBG)
1111 0V (GND) 14
ADMUX Register
ADMUX REFS1 REFS0 ADLAR MUX[3:0]

• ADLAR: ADC Left Adjust Result


- Write one to ADLAR to left adjust the result. Otherwise, the result is right adjusted.
- ADCL must be read first, then ADCH. Once ADCL is read, ADC access to Data Registers is blocked until ADCH is read.
- Changing the ADLAR bit will affect the ADC Data Register immediately, regardless of any ongoing conversions.

Right Adjusted (ADLAR=0)


ADCH ADC9 ADC8
ADCL ADC7 ADC6 ADC5 ADC4 ADC3 ADC2 ADC1 ADC0

Left Adjusted (ADLAR=1)


ADCH ADC9 ADC8 ADC7 ADC6 ADC5 ADC4 ADC3 ADC2
ADCL ADC1 ADC0

15
ADCSRA Register
ADCSRA ADEN ADSC ADATE ADIF ADIE ADPSS0[2:0]

• ADC Control and Status Register A


• ADEN: ADC Enable
- Writing this bit to one enables the ADC
- Turning the ADC off while a conversion is in progress, will terminate this conversion.
• ADSC: ADC Start Conversion
- Write this bit to one to start each conversion
- One: conversion is in progress.
- Zero: conversion is complete.
• ADATE: ADC Auto Trigger Enable
- When this bit is written to one, Auto Triggering of the ADC is enabled
- The ADC will start a conversion on a positive edge of the selected trigger signal. (ADTS in ADCSRB)

16
ADCSRA Register
ADCSRA ADEN ADSC ADATE ADIF ADIE ADPSS0[2:0]

• ADIF: ADC Interrupt Flag


- This bit is set when an ADC conversion completes and the Data Registers are updated

• ADIE: ADC Interrupt Enable


• ADPS: ADC Prescaler Select ADPS[3:0] Division Factor
- Determine the division factor between the system clock frequency 000 2
and the input clock to the ADC
001 2
010 4
011 8
100 16
101 32
110 64
111 128
17
ADCRSB Register
ADCSRB ACME ADTS[2:0]

• ADC Control and Status Register B


• ACME: Analog Comparator Multiplexer Enable
- When this bit is written logic one and the ADC is switched off (ADEN in ADCSRA is zero)

• ADTS: ADC Auto Trigger Source


- Selects which source will trigger an ADC conversion (positive edge)
ADPS[3:0] Division Factor
000 Free Running mode
001 Analog Comparator
010 External Interrupt Request 0
011 Timer/Counter0 Compare Match A
100 Timer/Counter0 Overflow
101 Timer/Counter1 Compare Match A
110 Timer/Counter1 Overflow
111 Timer/Counter1 Capture Event 18
ADC Timing

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

• Minimize EMI from digital side:


- Keep analog signal paths as short as possible.
- Make sure analog tracks run over the analog ground
plane and keep them well away from high-speed
switching digital tracks.
- Separate analog and digital grounds
- If any ADC [3:0] port pins are used as digital outputs, it
is essential that these do not switch while a conversion
is in progress.
- using the 2-wire Interface (ADC4 and ADC5) will only
affect the conversion on ADC4 and ADC5 and not the
other ADC channels

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

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

• Resolution: no. bits


- Common: 8, 10, 12 bits (16 available)
• Implementation
- Resistor ladder
• R-2R
- PWM + LPF

27
DC – R-2R Ladder

28
DAC

• MC1408L8 or DAC0808: 8-bit. D7 MSB


− Digital i/p is converted to current (Iout)
− A reference current Iref (typically 2mA) is provided via a +5V.
𝐷7 𝐷6 𝐷5 𝐷4 𝐷3 𝐷2 𝐷1 𝐷0
− 𝐼𝑜𝑢𝑡 = 𝐼𝑟𝑒𝑓 + + + + + + +
2 4 8 16 32 64 128 256
− Iout ranges between 0 and 1.99mA.
− For 5K resistor can produce 0 to +10V o/p.

29
DAC

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

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

• AIN0 > AIN1?


• Can trigger two types of interrupts:
- Separate exclusive interrupt
- Timer/Counter1 Input Capture function
• ADC multiplexer: ADC[7:0] can replace AIN1
- ADC must be switched off
- ADCSRB.ACME=1 and ADCSRA.ADEN=0
- Power Reduction Register (PRR.PRADC) must be written to '0’ in order to be able
to use the ADC input MUX

34
AC Registers

ADCSRB ACME ADTS[2:0]

ACSR ACD ACBG ACO ACI ACIE ACIC ACIS[1:0]

DIDR1 AIN1D AIN0D

35
ADCSRB

ADCSRB ACME ADTS[2:0]

• ACME: Analog Comparator Multiplexer Enable


- One: negative input = MUX ACME ADEN ADTS[2:0] Division Factor
0 X XXX AIN1
- Zero: negative input = AIN1
1 1 XXX AIN1
• ADTS: see ADC 1 0 000 ADC0
1 0 001 ADC1
1 0 010 ADC2
1 0 011 ADC3
1 0 100 ADC4
1 0 101 ADC5
1 0 110 ADC6
1 0 111 ADC7

36
ACSR

ACSR ACD ACBG ACO ACI ACIE ACIC ACIS[1:0]

• ACD: Analog Comparator Disable


- One: no power
- Zero: AC is ON
- When changing the ACD bit, the Analog Comparator Interrupt must be disabled
• ACBG: Analog Comparator Bandgap Select
- One: positive input is BG ref
- Zero: AIN0
• ACO: Analog Comparator Output
- synchronization introduces a delay of 1 - 2 clock cycles
• ACIE: Analog Comparator Interrupt Enable

37
ACSR

ACSR ACD ACBG ACO ACI ACIE ACIC ACIS[1:0]

• ACIC: Analog Comparator Input Capture Enable


- One: input capture function in Timer/Counter1 is triggered by AC
• The comparator output is in this case directly connected to the input capture front-end logic
• To make the comparator trigger the Timer/Counter1 Input Capture interrupt, the ICIE1 bit in
the Timer Interrupt Mask Register (TIMSK1) must be set

• ACIS: Analog Comparator Interrupt Mode Select


- Events to trigger the Analog Comparator interrupt.
ACIS1 ACIS0 ADPS[3:0]
0 0 Comparator Interrupt on Output Toggle.
0 1 Reserved
1 0 Comparator Interrupt on Falling Output Edge.
1 1 Comparator Interrupt on Rising Output Edge. 38
C Example
// built-in voltage comparator: v1 to PD6 (AIN0 +ve), v2 to PD7 (AIN1 -ve).
// Light one LED to indicate v1>v2 RED, and v1<v2 BLUE.
// Connect the LEDS to PC2, and PC4.
#include "ee470avr.h“
int main() {
// v1 to PD6 (AIN0 +ve), v2 to PD7 (AIN1 -ve).
bitclr(DDRD, PD6); bitclr(DDRD, PD7); // analog i/p
bitset(DDRC, PC2); bitset(DDRC, PC4); // o/p digital
bitclr(ACSR, ACD); bitclr(ACSR, ACBG); // AC enable, AIN0=PD6
while (1) {
if (bittst(ACSR, ACO) == 0) {
bitset(PORTC, PC2);
bitclr(PORTC, PC4);
}
else {
bitclr(PORTC, PC2);
bitset(PORTC, PC4);
}
}
}
39
Sensors

40
Sensors

• The sensor output might be current, resistance, or capacitance


• Signal Conditioning:
- (I/R/C)-to-voltage conversion and/or
- Voltage conversion and/or
- Signal amplification and/or
- DC shift since we can deal with voltages in the range (0,+5V).

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

You might also like