list p = 16f676
#include<p16f676.inc>
__config(0x314)
Tog equ 20 ; didn't use
Pulse equ 21 ; ADC result
OnPulse equ 22 ; High time
OfPulse equ 23 ; Low time
d1 equ 24 ; delay var
d2 equ 25 ; delay var
d3 equ 26 ; delay var
org 0
goto init ; bypass int vector
org 4
goto ISR ; Interrupt vector
init
banksel TRISC
clrf TRISC ; Outputs
movlw 0xFF
movwf TRISA ; Inputs
movlw 0x01
movwf ANSEL ; RA0 for pot
movlw 0x50
movwf ADCON1 ; Fosc/16
clrf OPTION_REG ; TMR0 pre-scaler 1:2
banksel PORTC
movlw 0x07
movwf CMCON ; No comparators
clrf ADCON0 ; ADC left justify, ADC off
clrf INTCON ; no int's
bsf INTCON,T0IE ; set TMR0 interrupt only
bsf INTCON,GIE ; set global int's
loop
call readadc ; get pulse width from RA0
movf Pulse,w
sublw .255 ;
movwf OfPulse ; the off pulse could easily
movf Pulse,w ; have been the comp of the pulse
movwf OnPulse
goto loop
ISR
btfsc PORTC,0 ; First we toggle RC0
goto t1
bsf PORTC,0 ; On
movf OnPulse,w ; (set tmr0 high time)
goto t2
t1 bcf PORTC,0 ; or off
movf OfPulse,w ; (set tmr0 low time)
t2 movwf TMR0
bcf INTCON,T0IF ; clear the interrupt
retfie ; all done
readadc
bsf ADCON0,ADON ; switch on
call delay40 ; have to wait for module
bsf ADCON0,GO ; start conversion
call delay40 ; TAD
btfsc ADCON0,GO ; wait for ADC
goto $-1 ;
movf ADRESH,w ; 8 bit result
movwf Pulse ; in pulse
return
delay40 ; about 38mS
clrf d1 ; 256 iterations
movlw 30
movwf d2 ; 30 iterations
decfsz d1
goto $ -1
decfsz d2
goto $ -3
return
end