Microchip Technology MA160014 Data Sheet

Page of 560
 2010-2012 Microchip Technology Inc.
DS41412F-page 303
PIC18(L)F2X/4XK22
17.2.10
A/D CONVERSION PROCEDURE
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
2.
Configure the ADC module:
• Select ADC conversion clock
• Configure voltage reference
• Select ADC input channel
• Select result format
• Select acquisition delay
• Turn on ADC module
3.
Configure ADC interrupt (optional):
• Clear ADC interrupt flag 
• Enable ADC interrupt
• Enable peripheral interrupt
• Enable global interrupt
(1)
4.
Wait the required acquisition time
(2)
.
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).
EXAMPLE 17-1:
A/D CONVERSION
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: Software delay required if ACQT bits are
set to zero delay. See 
;This code block configures the ADC
;for polling, Vdd and Vss as reference, Frc 
clock and AN0 input.
;
;Conversion start & polling for completion 
; are included.
;
MOVLW
B’10101111’
;right justify, Frc,
MOVWF
ADCON2
; & 12 TAD ACQ time
MOVLW
B’00000000’
;ADC ref = Vdd,Vss
MOVWF
ADCON1
;
BSF
TRISA,0
;Set RA0 to input
BSF
ANSEL,0
;Set RA0 to analog
MOVLW
B’00000001’
;AN0, ADC on
MOVWF
ADCON0
;
BSF
ADCON0,GO
;Start conversion
ADCPoll:
BTFSC
ADCON0,GO
;Is conversion done?
BRA
ADCPoll
;No, test again
; Result is complete - store 2 MSbits in
; RESULTHI and 8 LSbits in RESULTLO
MOVFF
ADRESH,RESULTHI
MOVFF
ADRESL,RESULTLO