PIC16F913/914/916/917/946
12.2.6
A/D CONVERSION PROCEDURE
EXAMPLE 12-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 (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’01110000’ ;ADC Frc clock
ADCON1
TRISA
TRISA,0
ANSEL
ANSEL,0
ADCON0
B’10000001’ ;Right justify,
ADCON0
SampleTime
ADCON0,GO
ADCON0,GO
$-1
;
;
;Set RA0 to input
;
;Set RA0 to analog
;
• Turn on ADC module
3. Configure ADC interrupt (optional):
• Clear ADC interrupt flag
;Vdd Vref, AN0, On
;Acquisiton delay
;Start conversion
;Is conversion done?
;No, test again
;
;Read upper 2 bits
;store in GPR space
;
• Enable ADC interrupt
• Enable peripheral interrupt
• Enable global interrupt(1)
4. Wait the required acquisition time(2)
.
ADRESH
5. Start conversion by setting the GO/DONE bit.
ADRESH,W
RESULTHI
ADRESL
ADRESL,W
RESULTLO
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)
12.2.7
ADC REGISTER DEFINITIONS
7. Read ADC Result
The following registers are used to control the opera-
tion of the ADC.
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 12.3 “A/D Acquisition
Requirements”.
© 2007 Microchip Technology Inc.
DS41250F-page 179