list p=16F84,t=ON,c=132,n=80,st=off
__CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _HS_OSC
include "P16F84.INC"
cblock 0x0C
count
t1,t2
error_code
total_lo,total_hi
count_lo,count_hi
endc
; /-----<A2D_OUT_BIT
; R
; R Rf
; R
; Rin |
;Vin ----RRRRR---.------>A2D_IN_BIT
; |
; -
; -
; |
; Gnd
; Resistor "Rin" [10K] from unknown voltage to comparator input.
; Resistor "Rf" [10k] from Feedback pin to comparator pin.
; Capacitor Cint from comparator pin to ground.0.56uF mylar film
A2D_PORT EQU PORTA
A2D_IN_BIT EQU 0
A2D_OUT_BIT EQU 1
ERROR_STUCK_HI EQU 1
ERROR_STUCK_LO EQU 2
ORG 0 ;Reset Vector
GOTO Main
ORG 4 ;Interrupt Vector
Main
bsf STATUS,RP0
clrf PORTB ;Make portb i/o pins outputs
movlw 1
movwf PORTA
bcf STATUS,RP0
clrf error_code
start:
loop
clrf count_lo
clrf count_hi
call a2d_convert2
movf count_lo,W
movwf PORTB
goto loop
;--------------------------------------------------------
; a2d_convert
;
; 2I/O pins
a2d_convert:
call precharge
a2d_loop
RLF A2D_PORT,W
xorlw 0xff
movwf A2D_PORT
clrc
movlw 1
btfss A2D_PORT,A2D_OUT_BIT
addwf count_lo,f
skpnc
addwf count_hi,f
addwf total_lo,f
skpnc
addwf total_hi,f
; clrf t1
; decfsz t1,f
; goto $-1
btfss total_hi,4
goto a2d_loop
return
;--------------------------------------------------------
; a2d_convert
;
; I/O pin
a2d_convert2:
;Put the A2D_PORT's tris into the FSR so that we can
;access it with out having to do those damn bank selects
movlw TRISA
movwf FSR
movlw 1
clrf count_lo
clrf count_hi
clrf total_lo
clrf total_hi
clrf t1
; Step 1 is the pre-charging cycle where the capacitor
;is made to pass through the low-to-high threshold and
;then again through the high-to-low threshold.
; This ensures that the voltage on the capacitor is the
;same for each time we pass through the algorithm.
;Make the capacitor go through the low to high threshold
bsf A2D_PORT,A2D_OUT_BIT
bcf INDF,A2D_OUT_BIT
a2d2_l1
call delay16
call delay16
call delay16
call delay16
bsf INDF,A2D_OUT_BIT
call delay16
btfsc A2D_PORT,A2D_OUT_BIT
goto a2d2_l2
bsf A2D_PORT,A2D_OUT_BIT
bcf INDF,A2D_OUT_BIT
decfsz t1,f
goto a2d2_l1
; movlw ERROR_STUCK_LO
; movwf error_code
; goto display_error
;now pass through the high to low threshold
a2d2_l2
clrf t1
bcf A2D_PORT,A2D_OUT_BIT
bcf INDF,A2D_OUT_BIT
a2d2_l3
call delay16
call delay16
call delay16
call delay16
bsf INDF,A2D_OUT_BIT
call delay16
btfss A2D_PORT,A2D_OUT_BIT
goto a2d2_l4
bcf A2D_PORT,A2D_OUT_BIT
bcf INDF,A2D_OUT_BIT
decfsz t1,f
goto a2d2_l3
; movlw ERROR_STUCK_HI
; movwf error_code
; goto display_error
a2d2_l4
;If we get then we've successfully precharged the capacitor
;Also, the capacitor voltage is just below the high-to-low
;threshold (Vil) so let's begin by ramping it positive
bsf A2D_PORT,A2D_OUT_BIT
bcf INDF,A2D_OUT_BIT
a2d2_l5
addwf total_lo,f
skpnc
addwf total_hi,f
btfsc total_hi,0
return
call delay16
call delay16
call delay16
call delay16
bsf INDF,A2D_OUT_BIT
call delay4
btfss A2D_PORT,A2D_OUT_BIT
goto a2d2_low
addwf count_lo,F
;The cap voltage is too high
bcf A2D_PORT,A2D_OUT_BIT
bcf INDF,A2D_OUT_BIT
skpnc
addwf count_hi,F
goto a2d2_l5
a2d2_low
;The cap voltage is too high
bsf A2D_PORT,A2D_OUT_BIT
bcf INDF,A2D_OUT_BIT
goto $+1
goto a2d2_l5
return
;--------------------------------------------------------
; precharge
precharge:
movlw 1
clrf total_lo
clrf total_hi
bcf A2D_PORT,A2D_OUT_BIT
; Make the capacitor voltage go below the trip threshold
pc_low_loop
btfss A2D_PORT,A2D_IN_BIT
goto pc_high
addwf total_lo,f
skpnc
addwf total_hi,f
skpc
goto pc_low_loop
movlw ERROR_STUCK_HI
movwf error_code
goto display_error
; Now make the capacitor voltage go above the trip threshold
pc_high
BSF A2D_PORT,A2D_OUT_BIT
clrf total_lo
clrf total_hi
pc_high_loop
btfsc A2D_PORT,A2D_IN_BIT
goto precharged
addwf total_lo,f
skpnc
addwf total_hi,f
skpc
goto pc_high_loop
movlw ERROR_STUCK_LO
movwf error_code
goto display_error
precharged:
clrf total_lo
clrf total_hi
bcf A2D_PORT,A2D_OUT_BIT
return
;--------------------------------------------------------
;
; error - write an error code to portb. if the error code is
; zero then toggle portb between 0 and 0xff
display_error:
bsf STATUS,RP0
clrf PORTB ;Make portb i/o pins outputs
bcf STATUS,RP0
error_loop:
movf error_code,W
movwf PORTB
skpz
goto error_loop
;If the error code is zero, then flash the LEDs
call big_delay
comf PORTB,f
call big_delay
goto error_loop
big_delay:
clrf t1
clrf t2
decfsz t1,f
goto $-1
decfsz t2,f
goto $-3
return
delay16 NOP
delay15 NOP
delay14 NOP
delay13 NOP
delay12 NOP
delay11 NOP
delay10 NOP
delay9 NOP
delay8 NOP
delay7 NOP
delay6 NOP
delay5 NOP
delay4 RETURN
END
file: /Techref/microchip/a2d.src, 5KB, , updated: 1999/7/19 10:09, local time: 2025/5/3 15:43,
|
| ©2025 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://www.linistepper.com/Techref/microchip/a2d.src"> microchip a2d</A> |
Did you find what you needed?
|