|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Lightbox seconds timer for 16F887 ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#include <p16F887.inc>
__CONFIG _CONFIG1, _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT
__CONFIG _CONFIG2, _WRT_OFF & _BOR21V
cblock 0x20
BUF_LO
BUF_HI
BUF2HI
BUF2LO
BUF3HI
BUF3LO
THOUS
HUNS
TENS
UNITS
TH_DISP
HU_DISP
TE_DISP
UN_DISP
CTR
CTR2
CTR3
FLAGS
S_FLAG
C_FLAG
F_FLAG
endc
ZERO equ B'10001000'
ONE equ B'11101011'
TWO equ B'01001100'
THREE equ B'01001001'
FOUR equ B'00101011'
FIVE equ B'00011001'
SIX equ B'00011000'
SEVEN equ B'11001011'
EIGHT equ B'00001000'
NINE equ B'00001011'
DECIMAL equ B'00000011'
org 0
start:
banksel TRISE
clrf TRISE ; Make PortE all output
bcf TRISE,0 ; Set RE0 to output
bsf TRISE,1 ; Set RE1 to input
bsf TRISE,2 ; Set RE2 to input
bcf STATUS,RP0 ; select Register Page 0
bcf STATUS,RP1 ; select Register Page 0
clrf PORTA
bsf STATUS,RP0 ; select Register Page 1
clrf TRISA ; Make PortA all output
clrf TRISB ; Make PortB all output
clrf TRISC ; Make PortC all output
clrf TRISD ; Make PortD all output
movlw 0x80 ; right justify, Vdd and Vss as Vref
movwf ADCON1
bsf TRISA,0 ; Set RA0 to input
bsf STATUS,RP1 ; select Register Page 3
clrf ANSEL
bcf STATUS,RP1 ; select Register Page 1
bcf STATUS,RP0 ; select Register Page 0
movlw 0xFF
movwf PORTB ; turn off all outputs
movwf PORTC
movwf PORTD
clrf BUF_LO
clrf BUF_HI
clrf BUF2LO
clrf BUF2HI
clrf BUF3LO
clrf BUF3HI
clrf FLAGS
clrf S_FLAG
mainloop:
btfsc C_FLAG,0 ; test the count flag
goto main2 ; execute count
btfsc PORTE,2 ; else test down bit
goto sw2
btfsc PORTE,1 ; test up bit
goto sw1
btfsc PORTA,0 ; test start bit
goto sw0
clrf FLAGS ; if no button pressed
clrf S_FLAG ; clear the short delay flag and counter
goto main2_done
sw2:
btfsc FLAGS,0 ; if short count flag set
goto main1 ; else count down
incf S_FLAG ; else count to 5
btfss S_FLAG,2 ; check for 4
goto main1 ; else count down
btfss S_FLAG,0 ; 4+1=5, so skip to set short count flag
goto main1 ; else count down
bsf FLAGS,0 ; set short count flag
goto main1 ; and count down
sw1:
btfsc FLAGS,0 ; same as down count, but up
goto main0
incf S_FLAG
btfss S_FLAG,2
goto main0
btfss S_FLAG,0
goto main0
bsf FLAGS,0
goto main0 ; and count up
sw0:
bsf C_FLAG,0 ; continuous count
bsf PORTE,0 ; turn on relay
movfw BUF2HI ; store count for future reference
movwf BUF3HI
movfw BUF2LO
movwf BUF3LO
goto main2 ; start count
main0:
movlw 0x01 ; upcount under button control
addwf BUF2LO,1 ; increment buffer memoryy
btfsc STATUS,Z
incf BUF2HI,1
btfss BUF2HI,1 ; check for 1000
goto main2_done
btfss BUF2HI,0
goto main2_done
btfss BUF2LO,7
goto main2_done
btfss BUF2LO,6
goto main2_done
btfss BUF2LO,5
goto main2_done
btfss BUF2LO,3 ; not 1000, continue
goto main2_done
clrf BUF2LO ; else set buffer to 0
clrf BUF2HI
goto main2_done
main1:
movlw 0x01 ; downcount under button control without switching on relay
subwf BUF2LO,1 ; decrement buffer memory
btfsc STATUS,C ; carry bit is UNSET on borrow when decrementing
goto main2_done
subwf BUF2HI,1
btfsc STATUS,C ; check for 000
goto main2_done
movlw 0x03
movwf BUF2HI
movlw 0xE7
movwf BUF2LO
goto main2_done
main2:
movlw 0x01 ; downcount continuously with relay turned on
subwf BUF2LO,1 ; decrement buffer memory
btfsc STATUS,C ; carry bit is UNSET on borrow when decrementing
goto main2_done
subwf BUF2HI,1
btfsc STATUS,C ; check for 000
goto main2_done
movfw BUF3HI
movwf BUF2HI
movfw BUF3LO
movwf BUF2LO
clrf C_FLAG ; discontinue count
bcf PORTE,0 ; turn off relay
main2_done:
movfw BUF2HI ; transfer counting buffer to main register
movwf BUF_HI
movfw BUF2LO
movwf BUF_LO
clrf TH_DISP ; clear display registers
clrf HU_DISP
clrf TE_DISP
clrf UN_DISP
clrf THOUS ; clear BCD registers
clrf HUNS
clrf TENS
clrf UNITS
btfss BUF_HI,1 ; test buffer bits and distribute values to BCD registers
goto bitnine
movlw 5
addwf HUNS,f
movlw 1
addwf TENS,f
movlw 2
addwf UNITS,f
bitnine:
btfss BUF_HI,0
goto biteight
movlw 2
addwf HUNS,f
movlw 5
addwf TENS,f
movlw 6
addwf UNITS,f
biteight:
btfss BUF_LO,7
goto bitseven
movlw 1
addwf HUNS,f
movlw 2
addwf TENS,f
movlw 8
addwf UNITS,f
bitseven:
btfss BUF_LO,6
goto bitsix
movlw 6
addwf TENS,f
movlw 4
addwf UNITS,f
bitsix:
btfss BUF_LO,5
goto bitfive
movlw 3
addwf TENS,f
movlw 2
addwf UNITS,f
bitfive:
btfss BUF_LO,4
goto bitfour
movlw 1
addwf TENS,f
movlw 6
addwf UNITS,f
bitfour:
btfss BUF_LO,3
goto bitthree
movlw 8
addwf UNITS,f
bitthree:
btfss BUF_LO,2
goto bittwo
movlw 4
addwf UNITS,f
bittwo:
btfss BUF_LO,1
goto bitone
movlw 2
addwf UNITS,f
bitone:
btfss BUF_LO,0
goto bitzero
movlw 1
addwf UNITS,f
bitzero:
movlw D'10' ; move any tens to tens register
subwf UNITS,f
btfsc STATUS,Z
goto units_zero
btfss STATUS,C
goto units_carry
incf TENS,f
goto bitzero
units_carry:
movlw D'10'
addwf UNITS,f
goto units_done
units_zero:
incf TENS,f
units_done:
movlw D'10' ; move any hundredss to hundreds register
subwf TENS,f
btfsc STATUS,Z
goto tens_zero
btfss STATUS,C
goto tens_carry
incf HUNS,f
goto units_done
tens_carry:
movlw D'10'
addwf TENS,f
goto tens_done
tens_zero:
incf HUNS,f
tens_done:
movlw D'10'
subwf HUNS,f
btfsc STATUS,Z
goto huns_zero
btfss STATUS,C
goto huns_carry
incf THOUS,f
goto tens_done
huns_carry:
movlw D'10' ; move any thous to thous register
addwf HUNS,f
goto huns_done
huns_zero:
incf THOUS,f
huns_done:
movlw 1 ; test value in BCD units and set display registers
subwf UNITS,f
btfsc STATUS,C
goto units_1
movlw ZERO ; move '0' to UN_DISP
movwf UN_DISP
goto tens_0
units_1:
movlw 1
subwf UNITS,f
btfsc STATUS,C
goto units_2
movlw ONE ; move '1' to UN_DISP
movwf UN_DISP
goto tens_0
units_2:
movlw 1
subwf UNITS,f
btfsc STATUS,C
goto units_3
movlw TWO ; move '2' to UN_DISP
movwf UN_DISP
goto tens_0
units_3:
movlw 1
subwf UNITS,f
btfsc STATUS,C
goto units_4
movlw THREE ; move '3' to UN_DISP
movwf UN_DISP
goto tens_0
units_4:
movlw 1
subwf UNITS,f
btfsc STATUS,C
goto units_5
movlw FOUR ; move '4' to UN_DISP
movwf UN_DISP
goto tens_0
units_5:
movlw 1
subwf UNITS,f
btfsc STATUS,C
goto units_6
movlw FIVE ; move '5' to UN_DISP
movwf UN_DISP
goto tens_0
units_6:
movlw 1
subwf UNITS,f
btfsc STATUS,C
goto units_7
movlw SIX ; move '6' to UN_DISP
movwf UN_DISP
goto tens_0
units_7:
movlw 1
subwf UNITS,f
btfsc STATUS,C
goto units_8
movlw SEVEN ; move '7' to UN_DISP
movwf UN_DISP
goto tens_0
units_8:
movlw 1
subwf UNITS,f
btfsc STATUS,C
goto units_9
movlw EIGHT ; move '8' to UN_DISP
movwf UN_DISP
goto tens_0
units_9:
movlw 1
subwf UNITS,f
btfsc STATUS,C
goto tens_0
movlw NINE ; move '9' to UN_DISP
movwf UN_DISP
tens_0:
movlw 1
subwf TENS,f
btfsc STATUS,C
goto tens_1
movlw ZERO ; move '0' to TE_DISP
movwf TE_DISP
goto huns_0
tens_1:
movlw 1
subwf TENS,f
btfsc STATUS,C
goto tens_2
movlw ONE ; move '1' to TE_DISP
movwf TE_DISP
goto huns_0
tens_2:
movlw 1
subwf TENS,f
btfsc STATUS,C
goto tens_3
movlw TWO ; move '2' to TE_DISP
movwf TE_DISP
goto huns_0
tens_3:
movlw 1
subwf TENS,f
btfsc STATUS,C
goto tens_4
movlw THREE ; move '3' to TE_DISP
movwf TE_DISP
goto huns_0
tens_4:
movlw 1
subwf TENS,f
btfsc STATUS,C
goto tens_5
movlw FOUR ; move 4 to TE_DISP
movwf TE_DISP
goto huns_0
tens_5:
movlw 1
subwf TENS,f
btfsc STATUS,C
goto tens_6
movlw FIVE ; move '5' to TE_DISP
movwf TE_DISP
goto huns_0
tens_6:
movlw 1
subwf TENS,f
btfsc STATUS,C
goto tens_7
movlw SIX ; move 6 to TE_DISP
movwf TE_DISP
goto huns_0
tens_7:
movlw 1
subwf TENS,f
btfsc STATUS,C
goto tens_8
movlw SEVEN ; move '7' to TE_DISP
movwf TE_DISP
goto huns_0
tens_8:
movlw 1
subwf TENS,f
btfsc STATUS,C
goto tens_9
movlw EIGHT ; move 8 to TE_DISP
movwf TE_DISP
goto huns_0
tens_9:
movlw 1
subwf TENS,f
btfsc STATUS,C
goto huns_0
movlw NINE ; move '9' to TE_DISP
movwf TE_DISP
huns_0:
movlw 1
subwf HUNS,f
btfsc STATUS,C
goto huns_1
movlw ZERO ; move '0' to HU_DISP
movwf HU_DISP
goto thous_0
huns_1:
movlw 1
subwf HUNS,f
btfsc STATUS,C
goto huns_2
movlw ONE ; move '1' to HU_DISP
movwf HU_DISP
goto thous_0
huns_2:
movlw 1
subwf HUNS,f
btfsc STATUS,C
goto huns_3
movlw TWO ; move '2' to HU_DISP
movwf HU_DISP
goto thous_0
huns_3:
movlw 1
subwf HUNS,f
btfsc STATUS,C
goto huns_4
movlw THREE ; move '3' to HU_DISP
movwf HU_DISP
goto thous_0
huns_4:
movlw 1
subwf HUNS,f
btfsc STATUS,C
goto huns_5
movlw FOUR ; move '4' to HU_DISP
movwf HU_DISP
goto thous_0
huns_5:
movlw 1
subwf HUNS,f
btfsc STATUS,C
goto huns_6
movlw FIVE ; move '5' to HU_DISP
movwf HU_DISP
goto thous_0
huns_6:
movlw 1
subwf HUNS,f
btfsc STATUS,C
goto huns_7
movlw SIX ; move '6' to HU_DISP
movwf HU_DISP
goto thous_0
huns_7:
movlw 1
subwf HUNS,f
btfsc STATUS,C
goto huns_8
movlw SEVEN ; move '7' to HU_DISP
movwf HU_DISP
goto thous_0
huns_8:
movlw 1
subwf HUNS,f
btfsc STATUS,C
goto huns_9
movlw EIGHT ; move '8' to HU_DISP
movwf HU_DISP
goto thous_0
huns_9:
movlw 1
subwf HUNS,f
btfsc STATUS,C
goto thous_0
movlw NINE ; move '9' to HU_DISP
movwf HU_DISP
thous_0:
movlw 1
subwf THOUS,f
btfsc STATUS,C
goto thous_1
movlw ZERO ; move '0' to TH_DISP
movwf TH_DISP
goto thous_2
thous_1:
movlw 1
subwf THOUS,f
btfsc STATUS,C
goto thous_2
movlw ONE ; move '1' to TH_DISP
movwf TH_DISP
thous_2:
movf UN_DISP,0 ; paint display registers onto 7-segment displays
movwf PORTC
movf TE_DISP,0
movwf PORTD
movf HU_DISP,0
movwf PORTB
btfsc FLAGS,0 ; fast/slow count switch
goto short2delay
call secdelay
goto mainloop
short2delay:
call shortdelay
goto mainloop
secdelay:
movlw 0x05
movwf CTR3
decfsz CTR,1
goto $-1
decfsz CTR2,1
goto $-3
decfsz CTR3,1
goto $-5
return
shortdelay:
movlw 0x3F
movwf CTR2
decfsz CTR,1
goto $-1
decfsz CTR2,1
goto $-3
return
end
|