Microprocessors and Microcontrollers

Analog-to-Digital Conversion
EE3954

by Maarten Uijt de Haag, Tim Bambeck

ADC.1

Data Bus 8 Timer0 Timer1 Timer2

ADC

PORTE
CCPs Comparators Synchronous Serial Port USARTs RE0 RE1 RE2 RF3 RE4 RE5 RE6 RE7

Other Modules

Parallel Slave Port

LCD Drivers

PORTA
Voltage Reference Data EEPROM RA0 RA1 RA2 RA3 RA4 RA5 RA6 RA7

Figure 4-2 in the Reference Manual

ADC.2

Analog-to-Digital Conversion (ADC)
Example
Microcontroller
Electric Circuit

RA1/AN1 (ADC)

Transducer

Signal Conditioner
PIC16F877

x(t)

Analog Signal

t

ADC.3

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

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

t

k

ADC.4

Digital-to-Analog Conversion (DAC)
Example
Microcontroller
DAC

Transducer
PIC16F877
Analog Signal

x(t)

x’[k]

Digital Signal

t

k

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

ADC.5

Vout = 0VDC (Logic ‘0’) ADC. Vin Reference Voltage. Vout If Vin > Vref .Comparator Basic Component: Input signal. Vout = 5VDC (Logic ‘1’) If Vin ≤ Vref .6 . Vref ADC ∞ + _ Output.

1-bit Example 5V ADC Vin > 2.5V: Signal is ‘0’ 0V ‘0’ 1-bit => 21 = 2 levels => 1 threshold ADC.5V ‘1’ Vin ≤ 2.7 .5V: Signal is ‘1’ 2.

00 V ‹ Vin ≤ 1.50 v 1.00 v ADC.25 each increment (thresholds) 3.00 v 5 v / 4 levels = 1.2-bit => 22 = 4 levels => 3 thresholds 2-bit Example ADC 5.25 v 0.50 V : Signal is ’01’ 1.25 V : Signal is ’00’ 0.75 V : Signal is ’10’ 2.25 < Vin ≤ 2.8 .00V : Signal is ’11’’ 3.75 < Vin ‹ 5.75 v 2.50 < Vin ≤ 3.

250V: Signal is 001 0.625 V 0.625V < Vin ≤ 1.125V < Vin ≤ 3.250V < Vin ≤ 1.625) ADC.375V: Signal is 110 3.3-bit Example 5.375V < Vin ≤ 5.750V: Signal is 101 2.875 V 1.875V: Signal is 010 0.500V < Vin ≤ 3.125 V 2.625V: Signal is 000’ ‘111’ ‘110’ ‘101’ ‘100’ ‘011’ ‘010’ ‘001’ ‘000’ 3-bits => 23 = 8 levels => 7 thresholds (5v/8 = 0.000V ‹ Vin ≤ 0.500V: Signal is 011 1.9 A/D Converter Binary Output .750V < Vin ≤ 4.000 V ADC Input Voltage To A/D Converter 4.500 V 1.750 V 3.375 V 3.250 V 0.00V: Signal is 111 3.875V < Vin ≤ 2.125V: Signal is 100 1.00 V 4.

3-bit Example Examples: Analog Input Voltage: 2.10 .234V 4.8756V -0.1V 3.2V Examples: Digital Output: 100 000 111 001 101 ADC ADC.9876V 1.

0048828125 V •  0.009765625 V •  0.ADC The PIC16F877 Microcontroller •  10-bits: –  210 = 1024 levels => 1023 thresholds –  Resolution = 5V/1024 = 0.11 .0048828125 V –  Thresholds: •  0.0146484375 V •  Etc. ADC.

Implementation: Parallel-Encoded or Flash ADC Vref = 5V 4.12 .625V I2 I1 I0 O1 O2 Digital Value out ADC.500V I4 1.375V 3.875V I3 1.750V ADC Vin I7 I6 I5 Example: 3-bit ADC 3.250V 0.125V O0 Priority Encoder 2.

LLH = 110 .Digital Logic – Revisited (?/) Priority Encoder I4 I3 I2 I1 I0 I7 I6 I5 O2 O1 O0 H H H H H H H L H H H H H H L X H H H H H L X X H H H H L X X X H H H L X X X X H H L X X X X X H L X X X X X X L X X X X X X X H H H H L L L L H H L L H H L L H L H L H L H L ADC.13 Note: if one uses active low.

375V 3.875V I3 1.500V I4 1.125V O0 Priority Encoder 2.250V 0. thus expensive Advantage: Highest speed ADC available ADC.625V I2 I1 I0 O1 O2 Digital Value Out Major drawback: An n-bit ADC Requires 2n-1 comparators.Implementation: Parallel-Encoded or Flash ADC Vref = 5V 4.14 .750V ADC Vin I7 I6 I5 Example: 3-bit ADC 3.

Successive Approximation ADC Clock Oscillator ADC in 16F877 Vin Successive Approximation Register Clear Start Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 Data ready – End of conversion D7 D6 D5 D4 D3 D2 D1 D0 DFlip Flops (Latche)s Digital Output V 8-bit D/A converter Note: for an n-bit ADC it will take n clock-cycles to find an output..15 . ADC.

16 .Successive Approximation – HW Flowchart Set all bits D7…D0 to ‘0’ ADC Set D7 to ‘1’ and compare V to Vin Set D6 to ‘1’ and compare V to Vin Set D5 to ‘1’ and compare V to Vin Set D0 to ‘1’ and compare V to Vin Y V < Vin N Clear D7 Y V < Vin N Clear D6 Y V < Vin N Clear D5 V < Vin Clear D0 ADC.

5V D7 = 0 0V D7 = Most Significant Bit (0XXX XXXX) Result: 1XXX XXXX ADC.3V D7 = 1 (1XXX XXXX) Threshold = 2.3V – Step 1 – Bit 7 (D7) 5V 4.Successive Approximation Example – 4.17 .

3V – Step 2 – Bit 6 (D6) 5V 4.18 .Successive Approximation Example – 4.75 0V D7 = Most Significant Bit Result: 11XX XXXX ADC.3V D6 = 1 D6 = 0 (11XX XXXX) (10XX XXXX) Threshold = 3.

0390625 V 3x0.19 .Example Voltage input range: 0V – 5V Number of bits: 8 => 28 = 256 levels (= 255 thresholds) Resolution = 5V / 256 = 0.05859375 V … … 4.01953125 V = 0.16 => 220 = binary value = 1101 1100 ADC.01953125 V = 0.01953125 V Thresholds at : 0.01953125 V 2x0.3V / resolution = 220.

20 .As part of the microcontroller … ADC •  Reference Manual: –  Section 21 & 22 (8-bit ADC). –  Section 23 (10-bit ADC) •  Datasheet: –  Section 11 (10-bit ADC) ADC.

21 .As part of the microcontroller … Timer0 Timer1 Timer2 ADC* Data Bus 8 CCPs Comparators Synchronous Serial Port USARTs ADC PORTF RF0 RF1 RF2 RF3 RF4 RF5 RF6 RF7 Other Modules Parallel Slave Port LCD Drivers PORTG Voltage Reference Data EEPROM RG0 RG1 RG2 RG3 RG4 RG5 RG6 RG7 *or A/D ADC.

The configuration … ADC ADC.22 .

ADC.23 .

24 .ADC Register (SFR) settings ADC.

25 .ADC Register (SFR) settings ADC.

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

27 .The result … ADC ADC.

28 .The result … ADC ADC.

Outside & Inside ADC ADC.29 .

Timing … ADC ADC.30 .

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.

CHOLD*( Rs + Ric + Rss) * ln (1/2047) ADC.000 ohm) Rss = Sample Switch Resistance (function of Vdd) (for Vdd = 5v. Rss = 7.Calculations for Tc: Capacitor Charges through Series Resistances: Rs = External signals source resistance (use 10.32 .000 ohm) CHOLD = Capacitor = 120 * 10-12 farads So Time for capacitor to charge: Tc = .000 in lab) Ric = Interconnect Resistance (Ric ≤ 1.

000)*(-7.(120 * 10 -12)*(18.000) * ln (1/2047) Tc = .CHOLD( Rs + Ric + Rss) * ln (1/2047) Tc = .Calculations for Tc (continued): Tc = .(120 * 10-12)( 10.33 .000 + 7.624) Therefore: Tc = 16.5 µs ADC.000 + 1.

25°C)*(0.Finding Time for Temperature Coefficient Term (Tcoff) : Tcoff = (Operating Temp in °C .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 .

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.5 µs + 1.35 .So total Acquisition time Tacq = Tacq = Tamp + Tc + Tcoff Tacq = 2 µs + 16.

75 µs (example) ADC.36 .Timing … ADC 19.

5 TAD for conversion Successive Approximation TAD must be ≥ 1. Converter Takes 11.ADC Conversion Time for Successive Approx.6µs ADC.37 .

ADCS0 to 0.5 * 2.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.50 µs 2.ADCS0 0 0 0 1 1 0 1 1 Operation 2 * Tosc 8 * Tosc 32 * Tosc use internal ‘RC’ TAD Time 0.0 µs time for TAD. so Tosc = 0.00 µs ( ≈ 2 – 6 µs ) Select ADCS1. So A/D conversion Time = 11.38 .250 µs We need to set ADCS1.00 µs 8.0 µs = 23 µs ADC.1 since this gives 2. ADCS0 bits in ADCON0 to select TAD: ADCS1.

75 µs (example) 11.39 . ADC.5 * TAD = 23 µs Note – You must wait at least 2 * TAD before next acquisition is started.Summary 19.Time for A/D Conversion .

The configuration … ADC ADC.40 .

41 .Use … Flow Chart 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 •  •  •  •  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 *if desired ADC.

Use … Flow Chart 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 •  •  •  •  Clear ADIF bit Set ADIE bit Set PEIE bit Set GIE bit ADC *if desired ADC.42 .

43 .The interrupt … ADC ADC.

if it is cleared the conversion is done. Use INTERRUPTS: Wait for the ADC interrupt *if desired ADC.Use … Flow Chart 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 Use POLLING: Keep checking the DO/DONE bit.44 .

45 .An Example 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 RA1/AN1 PIC16F877 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 .Configure ADC Module Example 0 1 0 0 1 0 0 1 Conversion clock 4MHz < 5MHz thus ADCS1:ADCS0 = ’01’ RA1/AN1 thus CHS2:CHS0 = ‘001’ Turn on Module: ADON = ‘1’ 1 0 0 0 0 0 0 0 Result is Right Justified thus ADFM = ‘1’ RA1/AN1 thus PCFG3:PCFG0 = ‘0000’ ADC.

Configure ADC via ADCON0 . Configure ADC via ADCON1 . Access bank 1 . Switch back to bank 0 ADC.48 .Configure ADC Module ADCON0 ADCON1 INIT_ADC: equ equ org movlw movwf bsf movlw movwf bcf … … … … 0x1F 0x1F 0x000 B’01001001’ ADCON0 STATUS. RP0 Code . RP0 B’10000000’ ADCON1 STATUS.

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

1 .1 .Wait for TACQ Need a delay for about/at least 20μseconds: Example DELAY: NXT: movlw movwf decfsz goto return 0x05 COUNT COUNT NXT .2 DELAY duration including ‘call’ and return’: 5 + 3*COUNT 5 + 3*COUNT = 20 => 3*COUNT = 15 => COUNT = 5 ADC.2 .50 . 1/2 .

1 .2 goto CHECK movf ADRESH.2 Code *GO/DONE = 0 2TAD = 2*8TOSC = 2*8*0.51 . Wait for TACQ . Start conversion (set GO) . Read A/D result (high byte) .Main Loop MAIN: CHECK: … call DELAY bsf ADCON0.RP0 movwf TEMPL nop nop goto MAIN .2 btfsc ADCON0. Wait for conversion to complete* . Write to TEMPH . Write to TEMPL .W bcf STATUS. 1 Wait for 4 instruction cycles .W movwf TEMPH bsf STATUS.RP0 movf ADRESL. use POLLING .25μs = 4μs = 4 instruction cycles ADC. Read A/D result (low byte) .

.2 Code 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.1 .2 goto CHECK movf ADRESH.RP0 nop nop goto MAIN .52 .Main Loop .W movwf TEMPH bsf STATUS.182Hz ≈ 18kHz ADC.1 .Sample Period MAIN: CHECK: … call DELAY bsf ADCON0.1 .1 .1 .RP0 movf ADRESL.1 .1 . ~12 Tad = 24 .1 .2 btfsc ADCON0.1 . 20 .W movwf TEMPL bcf STATUS.

Why is this Sample Frequency Important? The Nyquist criterion states that.53 . in order to prevent undesired aliasing. one must sample a signal at a rate equal to at least twice its bandwidth. Nyquist Criterion fs > 2B Sample frequency Signal bandwidth ADC.

54 .000 – 20) = 39.960Hz ≈ 40kHz For example: CD – 44.000 Hz RA1/AN1 PIC16F877 Must sample at fs > 2*(20.Nyquist Criterion Example Range of human hearing: 20 to 20.1kHz sampling rate DVD – up to 96 kHz And CD – 16 bits per sample ADC.

RP1 PIR1. Clear the ADIF flag (bit 6) STATUS. GIE . Access bank 0 INTCON.Example … Using Interrupts ADCON0 equ ADCON1 equ 0x1F 0x1F org goto org goto movlw movwf bsf movlw movwf bcf bcf bsf bsf bcf bsf bsf … … … 0x000 INIT_ADC 0x004 AD_ISR B’01001001’ ADCON0 . Enable global interrupts ADC. RP0 . Enable peripheral interrupts INTCON. Access bank 1 B’10000000’ ADCON1 . Configure ADC via ADCON0 STATUS. RP0 . PEIE . ADIE . Configure ADC via ADCON1 STATUS.55 ADC INIT_ADC: INIT_IRQ: . ADIF . Enable AADC interrupt STATUS. Access bank 1 PIE1.RP0 .

W TEMPH ADRESL. Start conversion (set GO) Code MAIN AD_ISR: .W TEMPL DELAY ADCON0. Read A/D result (low byte) . Wait for TACQ . Write to TEMPL . Clear the interrupt *GO/DONE = 0 ADC.2 MAIN ADRESH. Write to TEMPH . Start conversion (set GO) .56 . ADIF .Main Loop & Interrupt Service Routine … call bsf goto … movf movwf movf movwf call bsf bcf retfie DELAY ADCON0.2 PIR1. Wait for TACQ . Read A/D result (high byte) .

57 .VDD An Example … with Push-button ADC RA5/AN4 RA1/AN1 Vss PIC16F877 NOTE that in this case RA5 is a DIGITAL input and RA1 is an ANALOG input ADC.

Access bank 1 B’10000100’ ADCON1 .58 . RP0 . RP1 ADC. Configure RA5 as an input pin TRISA STATUS. Configure ADC via ADCON1 B’11111111’ . Configure ADC via ADCON0 STATUS.INIT_ADC: movlw movwf bsf movlw movwf movlw movwf bcf B’01001001’ ADCON0 .

Sign up to vote on this title
UsefulNot useful