PIC16F882/883/884/886/887
9.2.6
A/D CONVERSION PROCEDURE
EXAMPLE 9-1:
A/D CONVERSION
;This code block configures the ADC
;for polling, Vdd and Vss as reference, Frc
clock and AN0 input.
;
This is an example procedure for using the ADC to
perform an Analog-to-Digital conversion:
1. Configure Port:
• Disable pin output driver (See TRIS register)
• Configure pin as analog
;Conversion start & polling for completion
; are included.
;
2. Configure the ADC module:
• Select ADC conversion clock
• Configure voltage reference
• Select ADC input channel
• Select result format
BANKSEL
MOVLW
MOVWF
BANKSEL
BSF
BANKSEL
BSF
BANKSEL
MOVLW
MOVWF
CALL
BSF
BTFSC
GOTO
BANKSEL
MOVF
MOVWF
BANKSEL
MOVF
ADCON1
;
B’10000000’ ;right justify
ADCON1
TRISA
TRISA,0
ANSEL
ANSEL,0
ADCON0
B’11000001’ ;ADC Frc clock,
ADCON0
SampleTime
ADCON0,GO
ADCON0,GO
$-1
;Vdd and Vss as Vref
;
;Set RA0 to input
;
;Set RA0 to analog
;
• Turn on ADC module
3. Configure ADC interrupt (optional):
• Clear ADC interrupt flag
;AN0, On
• Enable ADC interrupt
;Acquisiton delay
;Start conversion
;Is conversion done?
;No, test again
;
;Read upper 2 bits
;store in GPR space
;
• Enable peripheral interrupt
• Enable global interrupt(1)
4. Wait the required acquisition time(2)
.
ADRESH
ADRESH,W
RESULTHI
ADRESL
ADRESL,W
RESULTLO
5. Start conversion by setting the GO/DONE bit.
6. Wait for ADC conversion to complete by one of
the following:
;Read lower 8 bits
;Store in GPR space
• Polling the GO/DONE bit
MOVWF
• Waiting for the ADC interrupt (interrupts
enabled)
7. Read ADC Result
8. Clear the ADC interrupt flag (required if interrupt
is enabled).
Note 1: The global interrupt can be disabled if the
user is attempting to wake-up from Sleep
and resume in-line code execution.
2: See Section 9.3 “A/D Acquisition
Requirements”.
© 2007 Microchip Technology Inc.
Preliminary
DS41291D-page 103