PIC16F72X/PIC16LF72X
9.2.6
A/D CONVERSION PROCEDURE
EXAMPLE 9-1:
A/D CONVERSION
This is an example procedure for using the ADC to
perform an Analog-to-Digital conversion:
;This code block configures the ADC
;for polling, Vdd reference, Frc clock
;and AN0 input.
;
1. Configure Port:
• Disable pin output driver (Refer to the TRIS
register)
;Conversion start & polling for completion
; are included.
;
• Configure pin as analog (Refer to the ANSEL
register)
BANKSEL
MOVLW
ADCON1
;
B’01110000’;ADC Frc clock,
;VDD reference
2. Configure the ADC module:
• Select ADC conversion clock
• Configure voltage reference
• Select ADC input channel
• Turn on ADC module
MOVWF
BANKSEL
BSF
BANKSEL
BSF
BANKSEL
MOVLW
MOVWF
CALL
BSF
BTFSC
GOTO
BANKSEL
MOVF
MOVWF
ADCON1
TRISA
TRISA,0
ANSELA
ANSELA,0
ADCON0
B’00000001’;AN0, On
ADCON0
SampleTime ;Acquisiton delay
ADCON0,GO ;Start conversion
ADCON0,GO ;Is conversion done?
;
;
;Set RA0 to input
;
;Set RA0 to analog
;
3. Configure ADC interrupt (optional):
• Clear ADC interrupt flag
;
• Enable ADC interrupt
• Enable peripheral interrupt
• Enable global interrupt(1)
$-1
;No, test again
;
;Read result
;store in GPR space
4. Wait the required acquisition time(2)
.
ADRES
ADRES,W
RESULT
5. Start conversion by setting the GO/DONE bit.
6. Wait for ADC conversion to complete by one of
the following:
• Polling the GO/DONE bit
• 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: Refer to Section 9.3 “A/D Acquisition
Requirements”.
© 2009 Microchip Technology Inc.
DS41341E-page 103