Date post: | 14-Apr-2015 |
Category: |
Documents |
Upload: | david-james-castillo |
View: | 33 times |
Download: | 1 times |
ADC.1
Microprocessors and Microcontrollers
Analog-to-Digital Conversion
EE3954
by
Maarten Uijt de Haag, Tim Bambeck
ADC.2
PORTE
RE0 RE1 RE2 RF3 RE4 RE5 RE6 RE7
PORTA
RA0 RA1 RA2 RA3 RA4 RA5 RA6 RA7
Timer0 Timer1 Timer2 ADC
CCPs Comparators Synchronous Serial Port USARTs
Other Modules
Parallel Slave Port
LCD Drivers
Voltage Reference
Data EEPROM
8
Data Bus
Figure 4-2 in the Reference Manual
ADC.3
Analog-to-Digital Conversion (ADC) Example
PIC16F877
RA1/AN1 (ADC)
Electric Circuit
Microcontroller
Transducer Signal Conditioner
t
x(t)Analog Signal
ADC.4
Analog-to-Digital Conversion
t
v(t) Analog Signal
k
x’[k] Digital Signal
t
v’(t) Discrete-Amplitude Signal
k
x[k] Discrete-Time Signal
ADC.5
Digital-to-Analog Conversion (DAC) Example
PIC16F877
DAC
Microcontroller
Transducer
t
x(t)Analog Signal
k
x’[k]Digital Signal
PIC Microcontroller does NOT have a built-in DAC!!!
ADC.6
ADC Comparator
Basic Component:
+ _ Output, Vout
Input signal, Vin
Reference Voltage, Vref
If Vin > Vref , Vout = 5VDC (Logic ‘1’) If Vin ≤ Vref , Vout = 0VDC (Logic ‘0’)
∞
ADC.7
ADC 1-bit Example
0V
5V
2.5V
‘1’
‘0’
Vin > 2.5V: Signal is ‘1’
Vin ≤ 2.5V: Signal is ‘0’
1-bit => 21 = 2 levels => 1 threshold
ADC.8
ADC 2-bit Example
5.00 v
3.75 v
2.50 v
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’
2.50 < Vin ≤ 3.75 V : Signal is ’10’
3.75 < Vin ‹ 5.00V : Signal is ’11’’
5 v / 4 levels = 1.25 each increment (thresholds)
2-bit => 22 = 4 levels => 3 thresholds
ADC.9
ADC 3-bit Example
3-bits => 23 = 8 levels => 7 thresholds (5v/8 = 0.625)
0.000 V 0.000V ‹ Vin ≤ 0.625V: Signal is 000’ ‘000’
‘001’ ‘010’ ‘011’ ‘100’ ‘101’ ‘110’ ‘111’
0.625 V 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
Con
vert
er
Bina
ry O
utpu
t
Inpu
t Vo
ltag
e
To A
/D C
onve
rter
ADC.10
ADC 3-bit Example
Examples: Analog Input Voltage:
2.8756V -0.234V
4.9876V
1.1V
3.2V
Examples: Digital Output: 100
000 111 001 101
ADC.11
ADC The PIC16F877 Microcontroller • 10-bits:
– 210 = 1024 levels => 1023 thresholds – Resolution = 5V/1024 = 0.0048828125 V – Thresholds:
• 0.0048828125 V • 0.009765625 V • 0.0146484375 V • Etc.
ADC.12
ADC Implementation: Parallel-Encoded or Flash ADC
Vref = 5V Vin
Priority Encoder
Digital Value out
I7 I6 I5 I4 I3 I2 I1 I0
O0 O1 O2
Example: 3-bit ADC
4.375V
3.125V
2.500V
1.875V
0.625V
1.250V
3.750V
ADC.13
Digital Logic – Revisited (?/) Priority Encoder
H H H H H H H L H H H
H H H H H H L X H H L
H H H H H L X X H L H
H H H H L X X X H L L
H H H L X X X X L H H
H H L X X X X X L H L
H L X X X X X X L L H
L X X X X X X X L L L
7I 6I 5I 4I 3I 2I 1I 0I 2O 1O 0O
Note: if one uses active low, LLH = 110
ADC.14
ADC Implementation: Parallel-Encoded or Flash ADC
Vref = 5V Vin
Priority Encoder
Digital Value Out
I7 I6 I5 I4 I3 I2 I1 I0
O0 O1 O2
Major drawback: An n-bit ADC Requires 2n-1 comparators, thus expensive Advantage: Highest speed ADC available
Example: 3-bit ADC
4.375V
3.125V
2.500V
1.875V
0.625V
1.250V
3.750V
ADC.15
ADC in 16F877 - Successive Approximation ADC
Vin
Clock Oscillator
Successive Approximation
Register
8-bit D/A converter
D7 D6 D5 D4 D3 D2 D1 D0
Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0
D- Flip Flops
(Latche)s
Clear Start
V
Digital Output
Data ready – End of conversion
Note: for an n-bit ADC it will take n clock-cycles to find an output.
ADC.16
ADC Successive Approximation – HW Flowchart
V < Vin
Set all bits D7…D0 to ‘0’
Set D7 to ‘1’ and compare
V to Vin
Clear D7
V < Vin
Set D6 to ‘1’ and compare
V to Vin
Clear D6
V < Vin
Set D5 to ‘1’ and compare
V to Vin
Clear D5
V < Vin
Set D0 to ‘1’ and compare
V to Vin
Clear D0
N
Y Y
N
Y
N
ADC.17
Successive Approximation Example – 4.3V – Step 1 – Bit 7 (D7)
0V
5V
D7 = Most Significant Bit
4.3V
Threshold = 2.5V
D7 = 0 (0XXX XXXX)
D7 = 1 (1XXX XXXX)
Result: 1XXX XXXX
ADC.18
Successive Approximation Example – 4.3V – Step 2 – Bit 6 (D6)
0V
5V
D7 = Most Significant Bit
4.3V
D6 = 0 (10XX XXXX)
D6 = 1 (11XX XXXX)
Result: 11XX XXXX
Threshold = 3.75
ADC.19
Example Voltage input range: 0V – 5V
Number of bits: 8 => 28 = 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
… …
4.3V / resolution = 220.16 => 220 = binary value = 1101 1100
ADC.20
ADC As part of the microcontroller …
• Reference Manual: – Section 21 & 22 (8-bit ADC), – Section 23 (10-bit ADC)
• Datasheet: – Section 11 (10-bit ADC)
ADC.21
ADC As part of the microcontroller …
PORTF
RF0 RF1 RF2 RF3 RF4 RF5 RF6 RF7
PORTG
RG0 RG1 RG2 RG3 RG4 RG5 RG6 RG7
Timer0 Timer1 Timer2 ADC*
CCPs Comparators Synchronous Serial Port USARTs
Other Modules
Parallel Slave Port
LCD Drivers
Voltage Reference
Data EEPROM
8
Data Bus
*or A/D
ADC.22
ADC The configuration …
ADC.23
ADC.24
ADC Register (SFR) settings
ADC.25
ADC Register (SFR) settings
ADC.26
Digital input: the standard input as we used it before!!
ADC.27
ADC The result …
ADC.28
ADC The result …
ADC.29
ADC Outside & Inside
ADC.30
ADC Timing …
ADC.31
Sample/Hold Acquisition Time Tacq = Amplifier Settling Time (Tamp)
+ Holding Capacitor Charging Time (Tc) + Temperature Coefficient Time (Tcoff)
Tacq = Tamp + Tc + Tcoff
Tamp = 2 µs for PIC 16F877
Next few slides find Tc & Tcoff:
ADC.32
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) Rss = Sample Switch Resistance (function of Vdd) (for Vdd = 5v, Rss = 7,000 ohm)
CHOLD = Capacitor = 120 * 10-12 farads
So Time for capacitor to charge:
Tc = - CHOLD*( Rs + Ric + Rss) * ln (1/2047)
ADC.33
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)
Tc = 16.5 µs Therefore:
ADC.34
Finding Time for Temperature Coefficient Term (Tcoff) :
Tcoff = (Operating Temp in °C - 25°C)*(0.05µs/°C)
Lets assume worst case Operating Temperature is 50°C (= 122°F)
Tcoff = (50°C - 25°C)*(0.05µs/°C)
Therefore: Tcoff = 1.25 µs
ADC.35
So total Acquisition time Tacq =
Tacq = Tamp + Tc + Tcoff
Tacq = 2 µs + 16.5 µs + 1.25 µs
Tacq = 19.75 µs
With our 4 MHz PIC’s in lab – (1 µs instruction cycle) We would delay 20 µs before we start conversion (GO is set)
ADC.36
ADC Timing …
19.75 µs (example)
ADC.37
ADC Conversion Time for Successive Approx. Converter
Successive Approximation
TAD must be ≥ 1.6µs
Takes 11.5 TAD for conversion
ADC.38
Selecting TAD Clock Source Value Using PIC’s in Lab: Minimum TAD must be ≥ 1.6 µs
PIC’s in Lab use 4 MHz oscillator, so Tosc = 0.250 µs
We need to set ADCS1, ADCS0 bits in ADCON0 to select TAD:
ADCS1,ADCS0 Operation TAD Time 0 0 2 * Tosc 0.50 µs 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 TAD.
So A/D conversion Time = 11.5 * 2.0 µs = 23 µs
ADC.39
Time for A/D Conversion - Summary
19.75 µs (example)
11.5 * TAD = 23 µs
Note – You must wait at least 2 * TAD before next acquisition is started.
ADC.40
ADC The configuration …
ADC.41
ADC Use … Flow Chart
Configure A/D Module
Configure A/D Interrupt*
*if desired
Wait for TACQ
Start Conversion (set GO)
Wait for conversion to complete
Read A/D Result
Wait for at least 2TAD before next acquisition
• Configure analog pins/voltage reference and digital I/O (ADCON1)
• Select ADC input channel (ADCON0)
• Select ADC conversion clock (ADCON0)
• Turn on ADC module (ADCON0)
ADC.42
ADC Use … Flow Chart
Configure A/D Module
Configure A/D Interrupt*
*if desired
Wait for TACQ
Start Conversion (set GO)
Wait for conversion to complete
Read A/D Result
Wait for at least 2TAD before next acquisition
• Clear ADIF bit • Set ADIE bit • Set PEIE bit • Set GIE bit
ADC.43
ADC The interrupt …
ADC.44
ADC Use … Flow Chart
Configure A/D Module
Configure A/D Interrupt*
*if desired
Wait for TACQ
Start Conversion (set GO)
Wait for conversion to complete
Read A/D Result
Wait for at least 2TAD before next acquisition
Use POLLING: Keep checking the DO/DONE bit, if it is cleared the conversion is done. Use INTERRUPTS: Wait for the ADC interrupt
ADC.45
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 frequency clock is 4MHz
ADC.46
Example Configure A/D Module
Configure A/D Interrupt*
Wait for TACQ
Start Conversion (set GO)
Wait for conversion to complete
Read A/D Result
Wait for at least 2TAD before next acquisition
ADC.47
Example Configure ADC Module
0 1 0 0 1 0 0 1
RA1/AN1 thus PCFG3:PCFG0 = ‘0000’
RA1/AN1 thus CHS2:CHS0 = ‘001’
Conversion clock 4MHz < 5MHz thus
ADCS1:ADCS0 = ’01’
Turn on Module: ADON = ‘1’
1 0 0 0 0 0 0 0
Result is Right Justified thus ADFM = ‘1’
ADC.48
Code Configure ADC Module
ADCON0 equ 0x1F ADCON1 equ 0x1F
org 0x000 INIT_ADC: movlw B’01001001’
movwf ADCON0 ; Configure ADC via ADCON0 bsf STATUS, RP0 ; Access bank 1 movlw B’10000000’ movwf ADCON1 ; Configure ADC via ADCON1 bcf STATUS, RP0 ; Switch back to bank 0 … … … …
ADC.49
Configure A/D Module
Configure A/D Interrupt*
Wait for TACQ
Start Conversion (set GO)
Wait for conversion to complete
Read A/D Result
Wait for at least 2TAD before next acquisition
Example
ADC.50
Example Wait for TACQ
Need a delay for about/at least 20μseconds:
DELAY: movlw 0x05 ; 1 movwf COUNT ; 1
NXT: decfsz COUNT ; 1/2 goto NXT ; 2 return ; 2
DELAY duration including ‘call’ and return’: 5 + 3*COUNT 5 + 3*COUNT = 20 => 3*COUNT = 15 => COUNT = 5
ADC.51
Code Main Loop
… MAIN: call DELAY ; Wait for TACQ
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 ; Write to TEMPH bsf STATUS,RP0 movf ADRESL,W ; Read A/D result (low byte) bcf STATUS,RP0 movwf TEMPL ; Write to TEMPL nop ; 1 Wait for 4 instruction cycles nop ; 1 goto MAIN ; 2
*GO/DONE = 0 2TAD = 2*8TOSC = 2*8*0.25μs = 4μs = 4 instruction cycles
ADC.52
Code Main Loop - Sample Period
… MAIN: call DELAY ; 20
bsf ADCON0,2 ; 1 CHECK: btfsc ADCON0,2 ; ~12 Tad = 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 nop ; 1 nop ; 1 goto MAIN ; 2
Sample Period = Ts ≈ 20 + 1 + 24 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 2 = 55TCY= 55μsec Sample Frequency = 1/Ts ≈ 1/55μsec = 18,182Hz ≈ 18kHz
ADC.53
Nyquist Criterion Why is this Sample Frequency Important?
The Nyquist criterion states that, in order to prevent undesired aliasing, one must sample a signal at a rate equal to at least twice its bandwidth.
fs > 2B
Sample frequency
Signal bandwidth
ADC.54
Nyquist Criterion Example
PIC16F877
RA1/AN1
Range of human hearing: 20 to 20,000 Hz
Must sample at fs > 2*(20,000 – 20) = 39,960Hz ≈ 40kHz
For example: CD – 44.1kHz sampling rate DVD – up to 96 kHz
And CD – 16 bits per sample
ADC.55
ADC Example … Using Interrupts
ADCON0 equ 0x1F ADCON1 equ 0x1F
org 0x000 goto INIT_ADC org 0x004 goto AD_ISR
INIT_ADC: movlw B’01001001’ movwf ADCON0 ; Configure ADC via ADCON0 bsf STATUS, RP0 ; Access bank 1 movlw B’10000000’ movwf ADCON1 ; Configure ADC via ADCON1 bcf STATUS, RP1
INIT_IRQ: bcf PIR1, ADIF ; Clear the ADIF flag (bit 6) bsf STATUS,RP0 ; Access bank 1 bsf PIE1, ADIE ; Enable AADC interrupt bcf STATUS, RP0 ; Access bank 0 bsf INTCON, PEIE ; Enable peripheral interrupts bsf INTCON, GIE ; Enable global interrupts … … …
ADC.56
Code Main Loop & Interrupt Service Routine
… call DELAY ; Wait for TACQ bsf ADCON0,2 ; Start conversion (set GO)
MAIN goto MAIN …
AD_ISR: movf ADRESH,W ; Read A/D result (high byte)
movwf TEMPH ; Write to TEMPH movf ADRESL,W ; Read A/D result (low byte) movwf TEMPL ; Write to TEMPL call DELAY ; Wait for TACQ bsf ADCON0,2 ; Start conversion (set GO) bcf PIR1, ADIF ; Clear the interrupt retfie
*GO/DONE = 0
ADC.57
ADC An Example … with Push-button
PIC16F877
RA1/AN1
Vss
VDD
RA5/AN4
NOTE that in this case RA5 is a DIGITAL input and RA1 is an ANALOG input
ADC.58
INIT_ADC: movlw B’01001001’ movwf ADCON0 ; Configure ADC via ADCON0 bsf STATUS, RP0 ; Access bank 1 movlw B’10000100’ movwf ADCON1 ; Configure ADC via ADCON1 movlw B’11111111’ ; Configure RA5 as an input pin movwf TRISA bcf STATUS, RP1