Sie sind auf Seite 1von 58

Analog-to-Digital Conversion

ADC.1
Data
Bus

8
Timer0 Timer1 Timer2 ADC

PORTE
Synchronous RE0
CCPs Comparators USARTs
Serial Port RE1
RE2
RF3
RE4
RE5
RE6
Other Parallel LCD RE7
Modules Slave Port Drivers

PORTA

Voltage Data RA0


Reference EEPROM RA1
RA2
RA3
RA4
RA5
RA6
RA7

ADC.2
Figure 4-2 in the Reference Manual
Analog-to-Digital Conversion (ADC)
Example

Microcontroller

Electric
Circuit RA1/AN1
(ADC)

Transducer Signal Conditioner


PIC16F877

Analog Signal
x(t)

t
ADC.3
Analog-to-Digital
Conversion
Analog Signal Discrete-Time Signal
v(t) x[k]

t
k
Discrete-Amplitude Signal Digital Signal
v’(t) x’[k]

t k ADC.4
Digital-to-Analog Conversion (DAC)
Example
Microcontroller

DAC

Transducer
PIC16F877

Analog Signal Digital Signal


x(t) x’[k]

t k

PIC Microcontroller does NOT have a built-in DAC!!! ADC.5


ADC
Comparator

Basic Component:

Input signal, Vin +


∞ Output, Vout
Reference Voltage, Vref _

If Vin > Vref , Vout = 5VDC (Logic ‘1’)

If Vin ≤ Vref , Vout = 0VDC (Logic ‘0’)

ADC.6
ADC
1-bit Example

5V

Vin > 2.5V: Signal is ‘1’ ‘1’

2.5V

Vin ≤ 2.5V: Signal is ‘0’ ‘0’

0V

1-bit => 21 = 2 levels => 1 threshold


ADC.7
ADC
2-bit Example
2-bit => 22 = 4 levels => 3 thresholds
5 v / 4 levels = 1.25 each increment
(thresholds)
5.00 v

3.75 < Vin ‹ 5.00V : Signal is ’11’’


3.75 v
2.50 < Vin ≤ 3.75 V : Signal is ’10’
2.50 v

1.25 < Vin ≤ 2.50 V : Signal is ’01’


1.25 v
0.00 V ‹ Vin ≤ 1.25 V : Signal is ’00’
0.00 v
ADC.8
ADC
3-bit Example

5.00 V
4.375V < Vin ≤ 5.00V: Signal is 111 ‘111’
4.375 V
To A/D Converter

3.750V < Vin ≤ 4.375V: Signal is 110 ‘110’

A/D Converter
Input Voltage

Binary Output
3.750 V
3.125V < Vin ≤ 3.750V: Signal is 101 ‘101’
3.125 V
2.500V < Vin ≤ 3.125V: Signal is 100 ‘100’
2.500 V
1.875V < Vin ≤ 2.500V: Signal is 011 ‘011’
1.875 V
1.250V < Vin ≤ 1.875V: Signal is 010 ‘010’
1.250 V
0.625V < Vin ≤ 1.250V: Signal is 001 ‘001’
0.625 V
0.000V ‹ Vin ≤ 0.625V: Signal is 000’ ‘000’
0.000 V

3-bits => 23 = 8 levels => 7 thresholds (5v/8 = 0.625)


ADC.9
ADC
3-bit Example
Examples: Examples:

Analog Input Voltage: Digital Output:

2.8756V 100

-0.234V 000

4.9876V 111

1.1V 001

3.2V 101

ADC.10
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.11
ADC
Implementation: Parallel-Encoded or Flash ADC

Vref = 5V
Vin

4.375V
I7

3.750V
I6
Example:
3.125V
3-bit ADC I5
O0
2.500V
I4 O1 Digital Value out
Priority
Encoder
1.875V O2
I3
1.250V
I2

0.625V I1

I0

ADC.12
Digital Logic – Revisited (?/)
Priority Encoder

I7 I6 I5 I4 I3 I2 I1 I0 O2 O1 O0
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

Note: if one uses active low, LLH = 110


ADC.13
ADC
Implementation: Parallel-Encoded or Flash ADC

Vref = 5V Vin

4.375V
I7

3.750V
I6
Example:
3.125V
3-bit ADC I5
O0
2.500V
I4 O1 Digital Value Out
Priority
Encoder
1.875V O2
I3
1.250V
I2 Major drawback:
An n-bit ADC Requires 2n-1 comparators,
0.625V I1 thus expensive

I0 Advantage:
Highest speed ADC available
ADC.14
ADC in 16F877
- Successive Approximation ADC
Clock
Oscillator

Clear
Vin Successive Start
Approximation Data ready – End of conversion
Register

D7 Q7
D6 Q6
D5 D- Q5
D4 Flip Q4 Digital
D3
Flops
Q3 Output
D2 Q2
D1 (Latche)s Q1
D0 Q0

V
8-bit
D/A converter

Note: for an n-bit ADC it will take n clock-cycles to find an output.


ADC.15
ADC
Successive Approximation – HW Flowchart
Set all bits
D7…D0 to ‘0’

Set D7 to ‘1’ Set D6 to ‘1’ Set D5 to ‘1’ Set D0 to ‘1’


and compare and compare and compare and compare
V to Vin V to Vin V to Vin V to Vin

Y Y Y
V < Vin V < Vin V < Vin V < Vin

N N N

Clear D7 Clear D6 Clear D5 Clear D0

ADC.16
Successive Approximation
Example – 4.3V – Step 1 – Bit 7 (D7)
5V

4.3V
D7 = 1 (1XXX XXXX)

Threshold = 2.5V

D7 = 0 (0XXX XXXX)

0V

D7 = Most Significant Bit Result: 1XXX XXXX ADC.17


Successive Approximation
Example – 4.3V – Step 2 – Bit 6 (D6)
5V

4.3V D6 = 1 (11XX XXXX)


Threshold = 3.75

D6 = 0 (10XX XXXX)

0V

D7 = Most Significant Bit Result: 11XX XXXX ADC.18


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.19
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.20
ADC
As part of the microcontroller …
Timer0 Timer1 Timer2 ADC*
Data
Bus

8 PORTF
Synchronous RF0
CCPs Comparators USARTs
Serial Port RF1
RF2
RF3
RF4
RF5
RF6
Other Parallel LCD RF7
Modules Slave Port Drivers

PORTG

Voltage Data RG0


Reference EEPROM RG1
RG2
RG3
RG4
RG5
RG6
*or A/D RG7
ADC.21
ADC
The configuration …

ADC.22
ADC.23
ADC Register (SFR) settings

ADC.24
ADC Register (SFR) settings

ADC.25
Digital input:
the standard
input as we used
it before!!

ADC.26
ADC
The result …

ADC.27
ADC
The result …

ADC.28
ADC
Outside & Inside

ADC.29
ADC
Timing …

ADC.30
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.31
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.32
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)

Therefore: Tc = 16.5 µs

ADC.33
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.34
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.35
ADC
Timing …

19.75 µs
(example)

ADC.36
ADC Conversion Time for
Successive Approx. Converter

Takes 11.5 TAD for conversion

Successive Approximation

TAD must be ≥ 1.6µs

ADC.37
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.38
Time for A/D Conversion - Summary

19.75 µs 11.5 * TAD


(example) = 23 µs

Note – You must wait at least 2 * TAD before next acquisition is started.

ADC.39
ADC
The configuration …

ADC.40
ADC
Use … Flow Chart
Configure A/D Module •  Configure analog pins/
voltage reference and
digital I/O (ADCON1)
•  Select ADC input
Configure A/D Interrupt*
channel (ADCON0)
•  Select ADC conversion
clock (ADCON0)
Wait for TACQ •  Turn on ADC module
(ADCON0)

Start Conversion (set GO)

Wait for conversion


to complete

Read A/D Result

Wait for at least 2TAD


before next acquisition
ADC.41
*if desired
ADC
Use … Flow Chart
Configure A/D Module

•  Clear ADIF bit


Configure A/D Interrupt*
•  Set ADIE bit
•  Set PEIE bit
•  Set GIE bit
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.42
*if desired
ADC
The interrupt …

ADC.43
ADC
Use … Flow Chart
Configure A/D Module

Configure A/D Interrupt*


Use POLLING:
Keep checking the DO/DONE
Wait for TACQ bit, if it is cleared the
conversion is done.
Start Conversion (set GO)
Use INTERRUPTS:
Wait for the ADC interrupt
Wait for conversion
to complete

Read A/D Result

Wait for at least 2TAD


before next acquisition
ADC.44
*if desired
ADC
An Example

Write a program that makes


continuous measurements of
RA1/AN1 the output voltage of the microphone
and writes the results to TEMPH (0x20)
and TEMPL (0x21)

PIC16F877
Assume that the Oscillator
frequency clock is 4MHz

ADC.45
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.46
Example
Configure ADC Module

0 1 0 0 1 0 0 1

Conversion clock RA1/AN1 thus Turn on Module:


4MHz < 5MHz thus CHS2:CHS0 = ‘001’ ADON = ‘1’
ADCS1:ADCS0 = ’01’

1 0 0 0 0 0 0 0

Result is Right Justified RA1/AN1 thus


thus ADFM = ‘1’ PCFG3:PCFG0 = ‘0000’ ADC.47
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.48
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.49
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.50
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

2TAD = 2*8TOSC = 2*8*0.25μs = 4μs = 4 instruction cycles


*GO/DONE = 0 ADC.51
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.52
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

Signal
Sample bandwidth
frequency

ADC.53
Nyquist Criterion
Example

Range of human hearing:


20 to 20,000 Hz
RA1/AN1

PIC16F877 Must sample at


fs > 2*(20,000 – 20) = 39,960Hz ≈ 40kHz

For example: CD – 44.1kHz sampling rate


DVD – up to 96 kHz/192kHz
Blu-ray – 192kHz
And
CD – 16 bits per sample
ADC.54
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.55
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.56
ADC
An Example … with Push-button
VDD

RA5/AN4

RA1/AN1

Vss PIC16F877

NOTE that in this case RA5 is a DIGITAL input and RA1 is an ANALOG input

ADC.57
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
ADC.58

Das könnte Ihnen auch gefallen