;MAIN PROGRAM ************************
start
bcf GPIO,0 ;ensure port is off
;wait for switch on
btfsc GPIO,1
goto start ;bit is set - input is high - switch is off
;switch is on - start with reference number = 0
clrf ref ;start ramp at zero
;initial 2sec delay
bcf flags,running ;allow normal timer int
call timer2sec
bsf GPIO,2 ;##### test LED ##########
twosecdelay
btfss flags,tim
goto twosecdelay ;loop until end of delay
;end of 2sec delay
bcf flags,newstart
bsf flags,running ;show interrupt that prog is in normal running mode
;set clock counter to 13
movlw .13
movwf count
;-----------program loops here between interrupts--------
mainloop ;wait for interrupt which will switch on port
btfss flags,newstart ;flag to send prog back to give 2sec time delay again
goto mainloop ;normal measure in interrupt routine
goto prestart ;meter has gone to zero - new start required
measure ;'''''''''call to here from interrupt program''''''''''''''
;pulse width ramps up form zero to a ratio of 231 in 256, in approx 6secs
;ref = turn-off time. ramps up to maxref
bcf GPIO,4 ;####### test LED ###########
bcf GPIO,5 ;####### test LED ###########
bsf GPIO,0 ;turn on port
;measure 'on' period (port is now on)
;look for tmr0 = ref to turn port off
int2
movwf ref ;get ref number
subwf TMR0,0
btfss STATUS,Z
goto int2
bcf GPIO,0 ;TMR0 = ref. Turn off port
;port-on period ended
decfsz count,1 ;1 in 13 ramp rate
return ;loop until time to step up/down ref register (13 x 2ms)
;reload counter after 13 x 2.048ms = 26.62ms
;(25.62ms x 231 = 6.15sec)
movlw .13
movwf count
;test switch input
btfsc GPIO,1
goto s1 ;bit is set - input is high - switch is off
goto s2 ;input is low - switch is on
s1
bsf GPIO,4 ;####### test LED #########
movwf ref ;switch is off - test file for 0
btfss STATUS,Z
goto s1a ;not zero
bsf flags,newstart ;gauge has run back to zero. loop back to include 2sec delay
return
s1a
decf ref,1 ;not at 0, so decrement
return
s2
bsf GPIO,5 ;####### test LED ##########
movwf ref ;test if ref = maximum allowed
subwf maxref,0
btfsc STATUS,C
return ;ref = >maxref
incf ref,1 ;ref was <maxref
return
;end of 'measure' routine from timer interrupt
;end of main prog ********************
;SUBROUTINES ***************************
;Timer (prescaler = div 8. TMR0 clocks tim1 every 2.048ms.
;tim1 clocks tim2 every 524ms)
;set timer for 2sec count of t1 =209; t2 = 3
;(209 x 2.048ms + 3 x 524ms = 2secs)
timer2sec
movlw .209 ;count of 209 x tim1
movwf tim1 ;store
movlw 3 ;count of 3 x tim2
movwf tim2
bcf flags,tim ;clear flag
clrf TMR0 ;reset timer counter
bcf INTCON,2 ;T01F - Clear TMR0 flag bit to allow interrupt
return
;********end of timer subs**********