Sie sind auf Seite 1von 58

Microprocessors and Microcontrollers

Analog-to-Digital Conversion
EE3954

by
Maarten Uijt de Haag, Tim Bambeck, Harsha Chenji

ADC.1

Data
Bus
8
Timer0

Timer1

Timer2

ADC

PORTE
CCPs

Comparators

Synchronous
Serial Port

USARTs

Other
Modules

Parallel
Slave Port

LCD
Drivers

RE0
RE1
RE2
RF3
RE4
RE5
RE6
RE7

PORTA
Voltage
Reference

Figure 4-2 in the Reference Manual

Data
EEPROM

RA0
RA1
RA2
RA3
RA4
RA5
RA6
RA7

ADC.2

Analog-to-Digital Conversion (ADC)


Example

Microcontroller
Electric
Circuit

Transducer

RA1/AN1
(ADC)

Signal Conditioner
PIC16F877

x(t)

Analog Signal

ADC.3

Analog-to-Digital
Conversion

v(t)

Analog Signal

x[k]

Discrete-Time Signal

t
v(t)

Discrete-Amplitude Signal

x[k]

Digital Signal

ADC.4

Digital-to-Analog Conversion (DAC)


Example

Microcontroller
DAC

Transducer
PIC16F877

x(t)

Analog Signal

x[k]

Digital Signal

PIC Microcontroller does NOT have a built-in DAC!!!

ADC.5

ADC

Comparator
Basic Component:
Input signal, Vin
Reference Voltage, Vref

+
_

Output, Vout

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

Vin 2.5V: Signal is 0

2.5V

0V

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

ADC.7

ADC

2-bit Example

2-bit => 22 = 4 levels => 3 thresholds

5.00 v

5 v / 4 levels = 1.25 each increment


(thresholds)
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

5.00 V
4.375 V
3.750 V
3.125 V
2.500 V
1.875 V
1.250 V
0.625 V
0.000 V

4.375V < Vin 5.00V: Signal is 111


3.750V < Vin 4.375V: Signal is 110
3.125V < Vin 3.750V: Signal is 101
2.500V < Vin 3.125V: Signal is 100
1.875V < Vin 2.500V: Signal is 011
1.250V < Vin 1.875V: Signal is 010
0.625V < Vin 1.250V: Signal is 001
0.000V Vin 0.625V: Signal is 000

111
110
101
100
011
010
001
000

A/D Converter
Binary Output

Input Voltage
To A/D Converter

3-bit Example

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
4.375V
3.750V

Example:
3-bit ADC

Vin
I7
I6

3.125V
I5

O0

2.500V
I4
1.875V
I3
1.250V
0.625V

Priority
Encoder

O1

Digital Value out

O2

I2
I1
I0

ADC.12

Digital Logic Revisited (?/)


Priority Encoder

I7

I6

I5

I4

I3

I2

I1

I0

O2

O1

O0

Note: if one uses active low, LLH = 110

ADC.13

ADC

Implementation: Parallel-Encoded or Flash ADC


Vref = 5V
4.375V
3.750V

Example:
3-bit ADC

Vin
I7
I6

3.125V
I5

O0

2.500V
I4
1.875V
I3
1.250V
0.625V

I2
I1
I0

Priority
Encoder

O1

Digital Value Out

O2
Major drawback:
An n-bit ADC Requires 2n-1 comparators,
thus expensive
Advantage:
Highest speed ADC available

ADC.14

ADC in 16F877

- Successive Approximation ADC


Clock
Oscillator

Vin

Successive
Approximation
Register

Clear

Data ready End of conversion


D7
D6
D5
D4
D3
D2
D1
D0

Start

DFlip
Flops
(Latche)s

Q7
Q6
Q5
Q4
Q3
Q2
Q1
Q0

Digital
Output

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
D7D0 to 0

Set D7 to 1
and compare
V to Vin

V < Vin
N
Clear D7

Set D6 to 1
and compare
V to Vin

V < Vin
N
Clear D6

Set D5 to 1
and compare
V to Vin

Set D0 to 1
and compare
V to Vin

V < Vin

V < Vin

N
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)

D6 = 0

(10XX XXXX)

Threshold = 3.75

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

CCPs

Comparators

Synchronous
Serial Port

USARTs

Other
Modules

Parallel
Slave Port

LCD
Drivers

PORTF
RF0
RF1
RF2
RF3
RF4
RF5
RF6
RF7

PORTG
Voltage
Reference

*or A/D

Data
EEPROM

RG0
RG1
RG2
RG3
RG4
RG5
RG6
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 - 25C)*(0.05s/C)


Lets assume worst case Operating Temperature is 50C (= 122F)

Tcoff = (50C - 25C)*(0.05s/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 PICs 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.6s

ADC.37

Selecting TAD Clock Source Value


Using PICs in Lab:
Minimum TAD 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 TAD:
ADCS1,ADCS0
0
0
0
1
1
0
1
1

Operation
2 * Tosc
8 * Tosc
32 * Tosc
use internal RC

TAD Time
0.50 s
2.00 s
8.00 s
( 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
(example)

11.5 * TAD
= 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 A/D Interrupt*

Wait for TACQ

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)

Start Conversion (set GO)


Wait for conversion
to complete
Read A/D Result
Wait for at least 2TAD
before next acquisition

*if desired

ADC.41

ADC

Use Flow Chart


Configure A/D Module
Configure A/D Interrupt*
Wait for TACQ

Clear ADIF bit


Set ADIE bit
Set PEIE bit
Set GIE bit

Start Conversion (set GO)


Wait for conversion
to complete
Read A/D Result
Wait for at least 2TAD
before next acquisition

*if desired

ADC.42

ADC

The interrupt

ADC.43

ADC

Use Flow Chart


Configure A/D Module
Configure A/D Interrupt*
Wait for TACQ
Start Conversion (set GO)
Wait for conversion
to complete

Use POLLING:
Keep checking the DO/DONE
bit, if it is cleared the
conversion is done.
Use INTERRUPTS:
Wait for the ADC interrupt

Read A/D Result


Wait for at least 2TAD
before next acquisition

*if desired

ADC.44

ADC

An Example

RA1/AN1

PIC16F877

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.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

Conversion clock
4MHz < 5MHz thus
ADCS1:ADCS0 = 01

Result is Right Justified


thus ADFM = 1

Turn on Module:
ADON = 1

RA1/AN1 thus
CHS2:CHS0 = 001

RA1/AN1 thus
PCFG3:PCFG0 = 0000

ADC.47

Code

Configure ADC Module


ADCON0
ADCON1
INIT_ADC:

equ
equ
org
movlw
movwf
bsf
movlw
movwf
bcf

0x1F
0x1F
0x000
B01001001
ADCON0
STATUS, RP0
B10000000
ADCON1
STATUS, RP0

; Configure ADC via ADCON0


; Access bank 1
; Configure ADC via ADCON1
; 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 20seconds:

DELAY:
NXT:

movlw
movwf
decfsz
goto
return

0x05
COUNT
COUNT
NXT

;1
;1
; 1/2
;2
;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:
CHECK:

*GO/DONE = 0

call
DELAY
bsf
ADCON0,2
btfsc
ADCON0,2
goto
CHECK
movf
ADRESH,W
movwf
TEMPH
bsf
STATUS,RP0
movf
ADRESL,W
bcf
STATUS,RP0
movwf
TEMPL
nop
nop
goto MAIN

; Wait for TACQ


; Start conversion (set GO)
; Wait for conversion to complete*
; use POLLING
; Read A/D result (high byte)
; Write to TEMPH
; Read A/D result (low byte)
; Write to TEMPL
; 1 Wait for 4 instruction cycles
;1
;2

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


ADC.51

Code

Main Loop - Sample Period


MAIN:
CHECK:

call
DELAY
bsf
ADCON0,2
btfsc
ADCON0,2
goto
CHECK
movf
ADRESH,W
movwf
TEMPH
bsf
STATUS,RP0
movf
ADRESL,W
movwf
TEMPL
bcf
STATUS,RP0
nop
nop
goto MAIN

; 20
;1
; ~12 Tad = 24
;
;1
;1
;1
;1
;1
;1
;1
;1
;2

Sample Period = Ts 20 + 1 + 24 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 2 = 55TCY= 55sec


Sample Frequency = 1/Ts 1/55sec = 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
Sample
frequency

Signal
bandwidth

ADC.53

Nyquist Criterion
Example

RA1/AN1

PIC16F877

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/192kHz
Blu-ray 192kHz

And
CD 16 bits per sample

ADC.54

ADC

Example Using Interrupts


ADCON0 equ
ADCON1 equ

INIT_ADC:

INIT_IRQ:

0x1F
0x1F
org
goto
org
goto
movlw
movwf
bsf
movlw
movwf
bcf
bcf
bsf
bsf
bcf
bsf
bsf

0x000
INIT_ADC
0x004
AD_ISR
B01001001
ADCON0 ; Configure ADC via ADCON0
STATUS, RP0
; Access bank 1
B10000000
ADCON1 ; Configure ADC via ADCON1
STATUS, RP1
PIR1, ADIF
; Clear the ADIF flag (bit 6)
STATUS,RP0
; Access bank 1
PIE1, ADIE
; Enable AADC interrupt
STATUS, RP0
; Access bank 0
INTCON, PEIE
; Enable peripheral interrupts
INTCON, GIE
; Enable global interrupts
ADC.55

Code

Main Loop & Interrupt Service Routine

MAIN
AD_ISR:

*GO/DONE = 0

call
bsf
goto

movf
movwf
movf
movwf
call
bsf
bcf
retfie

DELAY
ADCON0,2
MAIN

; Wait for TACQ


; Start conversion (set GO)

ADRESH,W
TEMPH
ADRESL,W
TEMPL
DELAY
ADCON0,2
PIR1, ADIF

; Read A/D result (high byte)


; Write to TEMPH
; Read A/D result (low byte)
; Write to TEMPL
; Wait for TACQ
; Start conversion (set GO)
; Clear the interrupt

ADC.56

ADC

VDD

An Example with Push-button

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
movwf
bsf
movlw
movwf
movlw
movwf
bcf

B01001001
ADCON0 ; Configure ADC via ADCON0
STATUS, RP0
; Access bank 1
B10000100
ADCON1 ; Configure ADC via ADCON1
B11111111
; Configure RA5 as an input pin
TRISA
STATUS, RP1

ADC.58

Das könnte Ihnen auch gefallen