list p=12F509 ; list directive to define processor
#include <p12F509.inc> ; processor specific variable definitions
__config _MCLRE_ON & _CP_OFF & _WDT_OFF & _IntRC_OSC
; '__CONFIG' directive is used to embed configuration word within .asm file.
; The lables following the directive are located in the respective .inc file.
; See respective data sheet for additional information on configuration word.
cblock 0x07
;***** VARIABLE DEFINITIONS
dc1 ; Delay Loop Counters
dc2
dc3
cv1 ; Loop Counter Variables
cv2
cv3
shadow ; Shadow variable used in rain to keep all top leds on or for cycle to resest all bits to 0
delaytime ; Adjustable delay variable
endc
;************************************************* *********************
; Internal RC calibration value is placed at location 0x3FF by Microchip
; as a movlw k, where the k is a literal value.
org 0
MAIN
movwf OSCCAL ; update register with factory cal value
;******** Main Code
start
movlw b'101000' ; Configure only GP3 and GP5 as a input
tris GPIO
goto testbut
loop
waitup movlw .2
call delay10 ; Delay of 20ms to debounce EITHER switch
btfsc GPIO,3 ; Is switch 3 pressed?
goto waitup ; Yes, wait 20ms and test again
btfsc GPIO,5 ; Is switch 5 pressed?
goto waitup ; Yes, wait 20ms and test again
; At this point both switches are up
testbut btfss GPIO,3 ; skip if button NOT pressed on GP3 ( GP3 low )
goto got3
btfsc GPIO,5 ; skip if button IS pressed on GP5 ( GP5 low )
goto testbut ; Otherwise wait for either button
got5 call counter ; Calls counter Subroutine that loops the subroutine X number of times
goto waitup ; ensure button up on return from counter
got3 call counter2 ; Calls counter2 Subroutine that loops the subroutine X number of times
goto waitup ; ensure button up on return from counter2
goto loop
;******************* Loops the code X # of times to change the loop amout change .13 or # to amount of times you want looped
counter
movlw .13 ; Loops 13 times
movwf cv1 ; Writes 13 to cv1
cv5 call Rain ; Calls the Rain Subroutine
decfsz cv1,f ; Decrement counter is set to 13
goto cv5 ; If Decrement counter is not 0 then repeats loop
counter2
movlw .13 ; Loops 13 times
movwf cv3 ; Writes 13 to cv1
cv4 call Sam ; Calls the Sam Subroutine
decfsz cv3,f ; Decrement counter is set to 13
goto cv4 ; If Decrement counter is not 0 then repeats loop
;******** Main Subroutine's / Effects
Cycle
movlw .5
movwf delaytime
movlw b'000000'
movwf shadow
call lightledA
call lightledB
call lightledC
call lightledD
call lightledE
call lightledF
call lightledG
call lightledH
call lightledI
call lightledJ
call lightledK
call lightledL
goto Cycle
Rain
movlw b'000000'
movwf shadow
movlw .2
movwf delaytime
call lightledA
call lightledD
call lightledG
call lightledJ
movlw .9
call delay10
movlw .3
movwf delaytime
call lightledC
call lightledF
call lightledI
call lightledL
movlw .9
call delay10
movlw .9
call delay10
movlw .9
movwf delaytime
call lightledA
call lightledD
call lightledG
call lightledJ
movlw .9
call delay10
movlw .9
call delay10
movlw .9
call delay10
movlw .4
movlw delaytime
call lightledB
call lightledE
call lightledH
call lightledK
goto Rain
Sam
movlw b'000000'
movwf shadow
movlw .1
movwf delaytime
call lightledC
call lightledB
call lightledA
call lightledD
call lightledG
call lightledG
call lightledH
call lightledI
call lightledJ
call lightledK
call lightledL
movlw .9
call delay10
call lightledB
call lightledD
call lightledF
call lightledG
call lightledI
call lightledH
call lightledJ
call lightledL
movlw .9
call delay10
call lightledJ
call lightledG
call lightledD
call lightledA
call lightledE
call lightledC
call lightledF
call lightledI
call lightledL
goto Sam
;************* Subroutines
;****************** Delay Subroutine ( 10 ms )
delay10 ; Delay W x 10ms
movwf dc3 ; Delay = 1 + W x (3 + 10009 + 3) - 1 + 4 -> W x 10.015ms
dly2 movlw .13 ; Repeat inner loop 13 times
movwf dc2 ; -> 13 x (767 + 3) - 1 = 10009 cycles
clrf dc1 ; Inner loop = 256 x 3 - 1 = 767 cycles
dly1 decfsz dc1,f
goto dly1
decfsz dc2,f ; End middle loop
goto dly1
decfsz dc3,f ; End outer loop
goto dly2
retlw 0
;****************** LED Subroutines
lightledA
movlw b'111010' ; Configure GP4 and GP1 as input
tris GPIO
movlw b'000001' ; Change corresponding bit of GP0 to 1
movwf GPIO ; Writes 1 to GP0
movlw delaytime ; Get from dealytime variable
call delay10 ; Delay of 50 x 10ms = 500ms or .5s
movlw shadow ; Change corresponding bit of GP0 to 0
movwf GPIO
movlw b'101000' ; Configure GP0,GP1,GP2,GP4 to outputs
tris GPIO ; Write 0 to GP0
retlw 0
lightledB
movlw b'001110' ; Configure GP1 and GP2 as inputs
tris GPIO
movlw b'000001' ; Change corresponding bit of GP0 to 1
movwf GPIO ; Writes 1 to GP0
movlw delaytime ; Get from dealytime variable
call delay10 ; Delay of 50 x 10ms = 500ms or .5s
movlw shadow ; Change correspoding bit of GP1 to 0
movwf GPIO
movlw b'101000' ; Configure GP0,GP1,GP2,GP4 to outputs
tris GPIO
retlw 0
lightledC
movlw b'001110' ; Configure pins GP1 and GP2 to inputs
tris GPIO
movlw b'010000' ; Change corresponding bit of GP4 to 1
movwf GPIO ; Writes 1 to GP4
movlw delaytime ; Get from dealytime variable
call delay10 ; Delay of 50 x 10ms = 500ms or .5s
movlw shadow ; Change correspoding bit of GP4 to 0
movwf GPIO
movlw b'101000' ; Configure GP0,GP1,GP2,GP4 to outputs
tris GPIO
retlw 0
lightledD
movlw b'011001' ; Configure pins GP1 and GP4 to inputs
tris GPIO
movlw b'000100' ; Change corresponding bit of GP2 to 1
movwf GPIO ; Writes 1 to GP2
movlw delaytime ; Get from dealytime variable
call delay10 ; Delay of 50 x 10ms = 500ms or .5s
movlw shadow ; Change correspoding bit of GP2 to 0
movwf GPIO
movlw b'101000' ; Configure GP0,GP1,GP2,GP4 to outputs
tris GPIO
retlw 0
lightledE
movlw b'011001' ; Configure pins GP0 and GP4 to inputs
tris GPIO
movlw b'000010' ; Change corresponding bit of GP1 to 1
movwf GPIO ; Writes 1 to GP1
movlw delaytime ; Get from dealytime variable
call delay10 ; Delay of 50 x 10ms = 500ms or .5s
movlw shadow ; Change correspoding bit of GP2 to 0
movwf GPIO
movlw b'101000' ; Configure GP0,GP1,GP2,GP4 to outputs
tris GPIO
retlw 0
lightledF
movlw b'001101' ; Configure pins GP0 and GP2 to inputs
tris GPIO
movlw b'010000' ; Change corresponding bit of GP4 to 1
movwf GPIO ; Writes 1 to GP4
movlw delaytime ; Get from dealytime variable
call delay10 ; Delay of 50 x 10ms = 500ms or .5s
movlw shadow ; Change correspoding bit of GP4 to 0
movwf GPIO
movlw b'101000' ; Configure GP0,GP1,GP2,GP4 to outputs
tris GPIO
retlw 0
lightledG
movlw b'011100' ; Configure pins GP2 and GP4 to inputs
tris GPIO
movlw b'000010' ; Change corresponding bit of GP1 to 1
movwf GPIO ; Writes 1 to GP1
movlw delaytime ; Get from dealytime variable
call delay10 ; Delay of 50 x 10ms = 500ms or .5s
movlw shadow ; Change correspoding bit of GP0 to 0
movwf GPIO
movlw b'101000' ; Configure GP0,GP1,GP2,GP4 to outputs
tris GPIO
retlw 0
lightledH
movlw b'011001' ; Configure pins GP0 and GP4 to inputs
tris GPIO
movlw b'000100' ; Change corresponding bit of GP2 to 1
movwf GPIO ; Writes 1 to GP2
movlw delaytime ; Get from dealytime variable
call delay10 ; Delay of 50 x 10ms = 500ms or .5s
movlw shadow ; Change correspoding bit of GP2 to 0
movwf GPIO
movlw b'101000' ; Configure GP0,GP1,GP2,GP4 to outputs
tris GPIO
retlw 0
lightledI
movlw b'001011' ; Configure pins GP0 and GP1 to inputs
tris GPIO
movlw b'000110' ; Change corresponding bit of GP1 to 1
movwf GPIO ; Writes 1 to GP1
movlw delaytime ; Get from dealytime variable
call delay10 ; Delay of 50 x 10ms = 500ms or .5s
movlw shadow ; Change correspoding bit of GP1 to 0
movwf GPIO
movlw b'101000' ; Configure GP0,GP1,GP2,GP4 to outputs
tris GPIO
retlw 0
lightledJ
movlw b'011100' ; Configure pins GP2 and GP4 to inputs
tris GPIO
movlw b'001001' ; Change corresponding bit of GP0 to 1
movwf GPIO ; Writes 1 to GP0
movlw delaytime ; Get from dealytime variable
call delay10 ; Delay of 50 x 10ms = 500ms or .5s
movlw shadow ; Change correspoding bit of GP1 to 0
movwf GPIO
movlw b'101000' ; Configure GP0,GP1,GP2,GP4 to outputs
tris GPIO
retlw 0
lightledK
movlw b'011001' ; Configure pins GP0 and GP4 to inputs
tris GPIO
movlw b'000010' ; Change corresponding bit of GP1 to 1
movwf GPIO ; Writes 1 to GP1
movlw delaytime ; Get from dealytime variable
call delay10 ; Delay of 50 x 10ms = 500ms or .5s
movlw shadow ; Change correspoding bit of GP0 to 0
movwf GPIO
movlw b'101000' ; Configure GP0,GP1,GP2,GP4 to outputs
tris GPIO
retlw 0
lightledL
movlw b'001110' ; Configure pins GP0 and GP1 to inputs
tris GPIO
movlw b'010000' ; Change corresponding bit of GP4 to 1
movwf GPIO ; Writes 1 to GP4
movlw delaytime ; Get from dealytime variable
call delay10 ; Delay of 50 x 10ms = 500ms or .5s
movlw shadow ; Change correspoding bit of GP0 to 0
movwf GPIO
movlw b'101000' ; Configure GP0,GP1,GP2,GP4 to outputs
tris GPIO
retlw 0
end