You are on page 1of 25

1

Microprocessors and Microcontrollers


Analog-to-Digital Conversion
EE3954
ADC.1
by
Maarten Uijt de Haag, Tim Bambeck
PORTE
Timer0 Timer1 Timer2 ADC
8
Data
Bus
RE0
RE1
RE2
RF3
RE4
RE5
RE6
RE7
PORTA
CCPs Comparators
Synchronous
Serial Port
USARTs
Other
Modules
Parallel
Slave Port
LCD
Drivers
ADC.2
RA0
RA1
RA2
RA3
RA4
RA5
RA6
RA7
Voltage
Reference
Data
EEPROM
Figure 4-2 in the Reference Manual
2
Analog-to-Digital Conversion (ADC)
Example
Microcontroller
PIC16F877
RA1/AN1
(ADC)
Electric
Circuit
Microcontroller
Transducer Signal Conditioner
ADC.3
t
x(t)
Analog Signal
Analog-to-Digital
Conversion
v(t)
Analog Signal
t
x[k]
Digital Signal
ADC.4
k
x [k]
3
Digital-to-Analog Conversion (DAC)
Example
Microcontroller
PIC16F877
DAC
Transducer
Analog Signal
Digital Signal
ADC.5
t
x(t)
Analog Signal
k
x[k]
g g
PIC Microcontroller does NOT have a built-in DAC!!!
ADC
Comparator
Basic Component:
+
_
Output, Vout
Input signal, Vin
Reference Voltage, Vref

ADC.6
If Vin > Vref , Vout = 5VDC (Logic 1)
If Vin Vref , Vout = 0VDC (Logic 0)
4
ADC
1-bit Example
5V
2.5V
1
0
Vin > 2.5V: Signal is 1
Vin 2.5V: Signal is 0
ADC.7
0V
1-bit => 2
1
= 2 levels => 1 threshold
ADC
2-bit Example
5 v / 4 levels = 1.25 each increment
(thresholds)
2-bit => 2
2
= 4 levels => 3 thresholds
5.00 v
3.75 v
2.50 v
2.50 < Vin 3.75 V : Signal is 10
3.75 < Vin 5.00V : Signal is 11
( )
ADC.8
1.25 v
0.00 v
1.25 < Vin 2.50 V : Signal is 01
0.00 V Vin 1.25 V : Signal is 00
5
ADC
3-bit Example
001
010
011
100
101
110
111

1.250 V
1.875 V
2.500 V
3.125 V
3.750 V
4.375 V
5.00 V
0.625V < Vin 1.250V: Signal is 001
1.250V < Vin 1.875V: Signal is 010
1.875V < Vin 2.500V: Signal is 011
2.500V < Vin 3.125V: Signal is 100
3.125V < Vin 3.750V: Signal is 101
3.750V < Vin 4.375V: Signal is 110
4.375V < Vin 5.00V: Signal is 111
A
/
D

C
o
n
v
e
r
t
e
r
B
i
n
a
r
y

O
u
t
p
u
t
I
n
p
u
t

V
o
l
t
a
g
e

T
o

A
/
D

C
o
n
v
e
r
t
e
r
ADC.9
3-bits => 2
3
= 8 levels => 7 thresholds (5v/8 = 0.625)
0.000 V
0.000V Vin 0.625V: Signal is 000
000
001
0.625 V
g
T
ADC
3-bit Example
Examples: Examples: Examples:
Analog Input Voltage:
2.8756V
-0.234V
4.9876V
Examples:
Digital Output:
100
000
111
ADC.10
1.1V
3.2V
001
101
6
ADC
The PIC16F877 Microcontroller
10 bits: 10-bits:
2
10
= 1024 levels => 1023 thresholds
Resolution = 5V/1024 = 0.0048828125 V
Thresholds:
0.0048828125 V
ADC.11
0.009765625 V
0.0146484375 V
Etc.
ADC
Implementation: Parallel-Encoded or Flash ADC
Vref = 5V
Vin
Priority
En d
Digital Value out
I
7
I
6
I
5
I
4
O
0
O
1
Example:
3-bit ADC
4.375V
3.125V
2.500V
3.750V
ADC.12
Encoder
I
3
I
2
I
1
I
0
O
2
1.875V
0.625V
1.250V
7
ADC
Implementation: Parallel-Encoded or Flash ADC
Vref = 5V
Vin
Priority
En d
Digital Value Out
I
7
I
6
I
5
I
4
O
0
O
1
Example:
3-bit ADC
4.375V
3.125V
2.500V
3.750V
ADC.13
Encoder
I
3
I
2
I
1
I
0
O
2
Major drawback:
An n-bit ADC Requires 2
n
-1 comparators,
thus expensive
Advantage:
Highest speed ADC available
1.875V
0.625V
1.250V
ADC in 16F877
- Successive Approximation ADC
Clock
Vin
Clock
Oscillator
Successive
Approximation
Register
D
7
D
6
D
5
D
4
D
3
D
2
D
Q
7
Q
6
Q
5
Q
4
Q
3
Q
2
Q
D-
Flip
Flops
(Latche)s
Clear
Start
Digital
Output
Data ready End of conversion
ADC.14
8-bit
D/A converter
D
1
D
0
Q
1
Q
0
(Latche)s
V
Note: for an n-bit ADC it will take n clock-cycles to find an output.
8
Example: 8-bit A/D
Voltage input range: 0V 5V
Number of bits: 8 => 2
8
= 256 levels (= 255 thresholds) Number of bits: 8 => 2 = 256 levels (= 255 thresholds)
Resolution = 5V / 256 = 0.01953125 V
Thresholds at : 0.01953125 V
2x0.01953125 V = 0.0390625 V
3x0.01953125 V = 0.05859375 V

ADC.15
If 4.3 volts is input to A/D converter,
What is the binary output from the A/D converter?
4.3V / resolution = 220.16 => 220 = binary value = 1101 1100
Example: 10-bit A/D
Voltage input range: 0V 5V
Number of bits: 10 => 2
10
= 1024 levels (= 1023 thresholds) Number of bits: 10 => 2 = 1024 levels (= 1023 thresholds)
Resolution = 5V / 1024 = 0.004882813 V
Thresholds at : 0.004882813 V
2x 0.004882813 V = 0.009765625 V
3x 0.004882813 V = 0.014648438 V

ADC.16
If 4.3 volts is input to A/D converter,
What is the binary output from the A/D converter?
4.3V / resolution = 880.64 => 880 = binary value = 11 0111 0000
9
ADC
As part of the microcontroller
Reference Manual:
Section 21 & 22 (8-bit ADC),
Section 23 (10-bit ADC)
Datasheet:
ADC.17
Section 11 (10-bit ADC)
ADC
As part of the microcontroller
Timer0 Timer1 Timer2 ADC*
Data
PORTF
RF0
RF1
RF2
RF3
RF4
RF5
RF6
RF7
CCPs Comparators
Synchronous
Serial Port
USARTs
Other
Modules
Parallel
Slave Port
LCD
Drivers
8
Bus
ADC.18
PORTG
RG0
RG1
RG2
RG3
RG4
RG5
RG6
RG7
Voltage
Reference
Data
EEPROM
*or A/D
10
ADC
The configuration
ADC.19
ADC.20
11
ADC Register (SFR) settings
ADC.21
ADC Register (SFR) settings
ADC.22
12
Digital input:
the standard
input as we
used it before!!
ADC.23
used it before!!
ADC
The result
ADC.24
13
ADC
The result
ADC.25
ADC
Outside & Inside
ADC.26
14
ADC
Timing
ADC.27
Sample/Hold Acquisition Time
Tacq = Amplifier Settling Time (Tamp) q p g ( p)
+ Holding Capacitor Charging Time (Tc)
+ Temperature Coefficient Time (Tcoff)
Tacq = Tamp + Tc + Tcoff
Tamp = 2 s for PIC 16F877
ADC.28
p
Next few slides find Tc & Tcoff:
15
Calculations for Tc:
Capacitor Charges through Series Resistances:
Rs = External signals source resistance (use 10,000 in lab)
Ric = Interconnect Resistance (Ric 1 000 ohm) Ric = Interconnect Resistance (Ric 1,000 ohm)
Rss = Sample Switch Resistance (function of Vdd)
(for Vdd = 5v, Rss = 7,000 ohm)
CHOLD = Capacitor = 120 * 10
-12
farads
ADC.29
So Time for capacitor to charge:
Tc = - CHOLD*( Rs + Ric + Rss) * ln (1/2047)
Calculations for Tc (continued):
Tc = - CHOLD( Rs + Ric + Rss) * ln (1/2047)
Tc = - (120 * 10
-12
)( 10,000 + 1,000 + 7,000) * ln (1/2047)
Tc = - (120 * 10
-12
)*(18,000)*(-7.624)
ADC.30
Tc = 16.5 s Therefore:
16
Finding Time for Temperature Coefficient Term (Tcoff) :
Tcoff = (Operating Temp in C - 25C)*(0.05s/C
Lets assume worst case Operating Temperature is 50C (= 122F)
Tcoff = (50C - 25C)*(0.05s/C)
ADC.31
Therefore: Tcoff = 1.25 s
So total Acquisition time Tacq =
Tacq = Tamp + Tc + Tcoff q p
Tacq = 2 s + 16.5 s + 1.25 s
Tacq = 19.75 s
ADC.32
With our 4 MHz PICs in lab (1 s instruction cycle)
We would delay 20 s before we start conversion (GO is set)
17
ADC
Timing
19.75 s
(example)
ADC.33
(example)
ADC Conversion Time for
Successive Approx. Converter
Successive Approximation
T must be 1 6s
Takes 11.5 TAD for conversion
ADC.34
T
AD
must be 1.6s
18
Selecting T
AD
Clock Source Value
Using PICs in Lab:
Minimum T
AD
must be 1.6 s
PICs in Lab use 4 MHz oscillator, so Tosc = 0.250 s
We need to set ADCS1, ADCS0 bits in ADCON0 to select T
AD
:
ADCS1,ADCS0 Operation T
AD
Time
0 0 2 * Tosc 0.50 s
0 1 8 * T 2 00
ADC.35
0 1 8 * Tosc 2.00 s
1 0 32 * Tosc 8.00 s
1 1 use internal RC ( 2 6 s )
Select ADCS1, ADCS0 to 0,1 since this gives 2.0 s time for T
AD
.
So A/D conversion Time = 11.5 * 2.0 s = 23 s
Time for A/D Conversion - Summary
19.75 s
(example)
11.5 * TAD
= 23 s
ADC.36
Note You must wait at least 2 * TAD before next acquisition is started.
19
ADC
The configuration
ADC.37
ADC
Use Flow Chart
Configure A/D Module
Configure analog
pins/voltage reference
and digital I/O
(ADCON1)
Configure A/D Interrupt*
Wait for T
ACQ
Start Conversion (set GO)
W i f i
(ADCON1)
Select ADC input
channel (ADCON0)
Select ADC conversion
clock (ADCON0)
Turn on ADC module
(ADCON0)
ADC.38
*if desired
Wait for conversion
to complete
Read A/D Result
Wait for at least 2T
AD
before next acquisition
20
ADC
Use Flow Chart
Configure A/D Module
Clear ADIF bit
Configure A/D Interrupt*
Wait for T
ACQ
Start Conversion (set GO)
W i f i
Clear ADIF bit
Set ADIE bit
Set PEIE bit
Set GIE bit
ADC.39
*if desired
Wait for conversion
to complete
Read A/D Result
Wait for at least 2T
AD
before next acquisition
ADC
The interrupt
ADC.40
21
ADC
Use Flow Chart
Configure A/D Module
Configure A/D Interrupt*
Wait for T
ACQ
Start Conversion (set GO)
W i f i
Use POLLING:
Keep checking the DO/DONE
bit, if it is cleared the
conversion is done.
Use INTERRUPTS:
Wait for the ADC interrupt
ADC.41
*if desired
Wait for conversion
to complete
Read A/D Result
Wait for at least 2T
AD
before next acquisition
ADC
An Example
PIC16F877
RA1/AN1
Write a program that makes
continuous measurements of
the output voltage of the microphone
and writes the results to TEMPH (0x20)
and TEMPL (0x21)
Assume that the Oscillator
ADC.42
frequency clock is 4MHz
22
Example
Configure A/D Module
Configure A/D Interrupt* Configure A/D Interrupt
Wait for T
ACQ
Start Conversion (set GO)
Wait for conversion
ADC.43
f
to complete
Read A/D Result
Wait for at least 2T
AD
before next acquisition
Example
Configure ADC Module
0 1 0 0 1 0 0 1
RA1/AN1 thus
CHS2:CHS0 = 001
Conversion clock
4MHz < 5MHz thus
ADCS1:ADCS0 = 01
Turn on Module:
ADON = 1
ADC.44
RA1/AN1 thus
PCFG3:PCFG0 = 0000
1 0 0 0 0 0 0 0
Result is Right Justified
thus ADFM = 1
23
Code
Configure ADC Module
ADCON0 equ 0x1F ADCON0 equ 0x1F
ADCON1 equ 0x1F
org 0x000
INIT_ADC: movlw B01001001
movwf ADCON0 ; Configure ADC via ADCON0
bsf STATUS, RP0 ; Access bank 1
movlw B10000000
movwf ADCON1 ; Configure ADC via ADCON1
bcf STATUS, RP0 ; Switch back to bank 0
ADC.45

Configure A/D Module


Configure A/D Interrupt*
Example
Configure A/D Interrupt
Wait for T
ACQ
Start Conversion (set GO)
Wait for conversion
ADC.46
f
to complete
Read A/D Result
Wait for at least 2T
AD
before next acquisition
24
Example
Wait for T
ACQ
Need a delay for about/at least 20seconds: Need a delay for about/at least 20seconds:
DELAY: movlw 0x05 ; 1
movwf COUNT ; 1
NXT: decfsz COUNT ; 1/2
goto NXT ; 2
return ; 2
ADC.47
DELAY duration including call and return: 5 + 3*COUNT
5 + 3*COUNT = 20 => 3*COUNT = 15 => COUNT = 5
Code
Main Loop

MAIN: call DELAY ; Wait for T


ACQ
MAIN: call DELAY ; Wait for T
ACQ
bsf ADCON0,2 ; Start conversion (set GO)
CHECK: btfsc ADCON0,2 ; Wait for conversion to complete*
goto CHECK ; use POLLING
movf ADRESH,W ; Read A/D result (high byte)
movwf TEMPH ; Store high result
bsf STATUS,RP0 ; change to BANK1
movf ADRESL,W ; Read A/D result (low byte)
bcf STATUS,RP0 ; change to BANK0
movwf TEMPL ; Store low result
ADC.48
*GO/DONE = 0
25
Code
Main Loop - Sample Period

MAIN: call DELAY ; 20 MAIN: call DELAY ; 20


bsf ADCON0,2 ; 1
CHECK: btfsc ADCON0,2 ; ~12 T
ad
= 24
goto CHECK ;
movf ADRESH,W ; 1
movwf TEMPH ; 1
bsf STATUS,RP0 ; 1
movf ADRESL,W ; 1
movwf TEMPL ; 1
bcf STATUS,RP0 ; 1
ADC.49
nop ; 1
nop ; 1
goto MAIN ; 2
Sample Period = T
s
20 + 1 + 24 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 2 = 55T
CY
= 55sec
Sample Frequency = 1/T
s
1/55sec = 18,182Hz 18kHz

You might also like