	
	
	;CODE PUZZLE for PIC12F629    1-10-2007


	list	p=12F629				; list directive to define processor
	
	include	"p12f629.inc"			; processor specific variable definitions
	
	errorlevel  -302                ; suppress message 302 from list file
									; (Register in operand not in bank 0.)
	
	
	__CONFIG   _CP_off & _CPD_off & _BODEN_offF & _MCLRE_off & _WDT_off & _PWRTE_ON & _INTRC_OSC_NOCLKOUT ;Internal osc.

		
			; globals

fileA	equ		20h
fileB	equ		21h	
fileC	equ		22h
fileD	equ		23h
fileE	equ		24h
swflag	equ		25h	
swflag2 equ     26h

status		equ	03h
option_reg	equ	81h


				;bits
				
rp0		equ	5	
		

Start	org	0x00		;program starts at location 000
		nop
		nop
		nop
		nop		;NOPs to get past reset vector address
		nop
		nop


SetUp	bsf		status,rp0 	;Bank 1	
		movlw	b'10000110'	;Turn off T0CKI, prescaler for TMR0 = 1:128
		movwf	option_reg    
		bcf		status,rp0	;bank 0  
		movlw   07h         ;Set up W to turn off Comparator ports
		movwf   CMCON       ;must be placed in bank 0   
		clrf 	GPIO       	;Clear GPIO of junk		
		goto 	Main


		
LED0	bsf		status,rp0  	;Bank 1	
		movlw	b'00111111'		;make all input
		movwf	TRISIO	    	;turn off all LEDs
		bcf		status,rp0		;bank 0 
		retlw	00		
		
		
LED1	bsf		status,rp0  	;Bank 1	
		movlw	b'00111001'		;make GP1,2 output
		movwf	TRISIO	    	;enable outputs	
		bcf		status,rp0		;bank 0 
		movlw	02h				;make GP1 HIGH, GP2 LOW
		movwf	GPIO 			;turn on LED1
		retlw	00
		
		
LED2	bsf		status,rp0 	  	;Bank 1	
		movlw	b'00111001'		;make GP1,2 output
		movwf	TRISIO	    	;enable outputs	
		bcf		status,rp0		;bank 0 
		movlw	04h				;make GP1 LOW, GP1 HIGH
		movwf	GPIO 			;turn on LED2
		retlw	00
		
			
LED3	bsf		status,rp0 		;Bank 1	
		movlw	b'00011011'		;make GP2,5 output
		movwf	TRISIO	    	;enable outputs	
		bcf		status,rp0		;bank 0 
		movlw	04h				;make  GP2 HIGH  GP5 LOW,
		movwf	GPIO 			;turn on LED3
		retlw	00
		
					
LED4	bsf		status,rp0 		;Bank 1	
		movlw	b'00011011'		;make GP2,5 output
		movwf	TRISIO	    	;enable outputs	
		bcf		status,rp0		;bank 0 
		movlw	20h				;make  GP2 LOW  GP5 HIGH
		movwf	GPIO 			;turn on LED4
		retlw	00
		
							
LED5	bsf		status,rp0 		;Bank 1	
		movlw	b'00101011'		;make GP2,4 output
		movwf	TRISIO	    	;enable outputs	
		bcf		status,rp0		;bank 0 
		movlw	04h				;make  GP2 HIGH  GP4 LOW
		movwf	GPIO 			;turn on LED5
		retlw	00
		
		
	
LED6	bsf		status,rp0 		;Bank 1	
		movlw	b'00001111'		;make GP4,5 output
		movwf	TRISIO	    	;enable outputs	
		bcf		status,rp0		;bank 0 
		movlw	10h				;make  GP4 HIGH  GP5 LOW
		movwf	GPIO 			;turn on LED6
		retlw	00
		
	
LED7	bsf		status,rp0 		;Bank 1	
		movlw	b'00001111'		;make GP4,5 output
		movwf	TRISIO	    	;enable outputs	
		bcf		status,rp0		;bank 0 
		movlw	20h				;make  GP4 LOW  GP5  HIGH
		movwf	GPIO 			;turn on LED7
		retlw	00
		
	
	
LED8	bsf		status,rp0 		;Bank 1	
		movlw	b'00011101'		;make GP1,5 output
		movwf	TRISIO	    	;enable outputs	
		bcf		status,rp0		;bank 0 
		movlw	02h				;make  GP1 HIGH   GP5   LOW
		movwf	GPIO 			;turn on LED8
		retlw	00
		

LED9	bsf		status,rp0 		;Bank 1	
		movlw	b'00101101'		;make GP1,4 output
		movwf	TRISIO	    	;enable outputs	
		bcf		status,rp0		;bank 0 
		movlw	02h				;make  GP1 HIGH   GP4   LOW
		movwf	GPIO 			;turn on LED9
		retlw	00
		
	
LED10	bsf		status,rp0 		;Bank 1	
		movlw	b'00101101'		;make GP1,4 output
		movwf	TRISIO	    	;enable outputs	
		bcf		status,rp0		;bank 0 
		movlw	10h				;make  GP1 LOW  GP4    HIGH
		movwf	GPIO 			;turn on LED10
		retlw	00
		
			
	
			
	
	;Delay 0.5 sec

Del_1	movlw 	02h
		movwf 	fileC	
DelX	nop
		decfsz 	fileA,1		; ,1 denotes the result of the decrement 
		goto 	DelX
		decfsz 	fileB,1		;     is placed in the file	
		goto 	DelX
		decfsz 	fileC,1
		goto 	DelX
		retlw 	00
		
		
							;1mS delay
		
_1mS	decfsz	fileA,1			 
		goto 	_1mS	 	
		retlw	00
		
							;100mS delay
							
_100mS	movlw	60h	
		movwf	fileB
AA		call	_1mS
		decfsz 	fileB,1
		goto	AA	
		retlw	00
		
		
		
		
	
Switch	bsf		status,rp0 		;Bank 1	
		bCf		TRISIO,0	   	;make GP0 output	
		bcf		status,rp0		;bank 0
		bcf		GPIO,0			;make switch-line LOW				
		call	_1mS			; short delay while 100n discharges
		bsf		status,rp0 		;Bank 1	
		bsf		TRISIO,0	   	;make GP0 input	
		bcf		status,rp0		;bank 0
		call	_1mS
		call	_1mS			; delay while 100n charges via switch
		call	_1mS		
		btfss	GPIO,0			;is switch-line HIGH?
		retlw	00				;no switch pressed		
								;yes - switch A or B pressed
		
		bsf		status,rp0 		;Bank 1	
		bCf		TRISIO,0	   	;make GP0 output	
		bcf		status,rp0		;bank 0
		bcf		GPIO,0			;make switch-line LOW	
		call	_1mS			; delay while 100n charges via switch
		bsf		status,rp0 		;Bank 1	
		bsf		TRISIO,0	   	;make GP0 input	
		bcf		status,rp0		;bank 0
		call	_1mS
		call	_1mS			; delay while 100n charges via switch
		btfsc	GPIO,0			;is switch-line HIGH?
		retlw 	01				;yes - switch A  pressed
		call	_1mS
		call	_1mS			; delay while 100n charges via switch
		btfsc	GPIO,0			;is switch-line HIGH?
		retlw 	02				;yes - switch B pressed
		retlw	00				;no switch pressed		

		
		

		
			
							
							
							;LED 1 flashes at 1Hz. Program looks for button A 
							;pressed 8 times when the blinking LED is OFF,
							;before turning on LED 1 fully, to go
							;to next stage of flashing next LED.	
							;If button B pressed, go to Main and start again
							;If button A is pressed when LED is ON, start again	
							
							;10 LEDs and 10 codes to get through 
							;The first is "Code1"
							
							;swflag gets loaded from W via the Sw sub-routine


Main	nop					;The Main sub-routine is called "Code"

Code1	clrf	swflag		;clear switch flag
		clrf	swflag2		;clear switch flag2 - bit 3 will be set after 8 presses of "A"
Code1G	movlw	0D0h		;224 loops to create 0.5sec for LED1 ON
		movwf	fileD		;loop file is file D
Code1A	call  	LED1		;turn on LED 1
		call	Switch		;if button B is pressed - clear swflag2	
		movwf	swflag		;store switch value  bit1 HIGH=B
		btfss	swflag,1	;has B been pushed?		
		goto	Code1B		;no button pressed				
		clrf	swflag2		;removes the button presses of A
Code1B	decfsz	fileD,1
		goto	Code1A
		
		movlw	0D0h		;224 loops to create 0.5sec for LED1 OFF
		movwf	fileD		;loop file is file D
Code1C	call  	LED0		;turn off LED 1
		call	Switch		;see if button A or B is pressed - if button B go to Code1D		
		movwf	swflag		;store switch value - use bit0=A, bit1=B
		btfsc	swflag,1	;has B been pushed?
		goto	Code1D		;bit 1 = high = B = go to Code1D
		btfss	swflag,0	;has A been pushed?		
		goto	Code1E		;no button pressed
		btfsc	swflag2,7	;first time pressed?
		goto	Code1F		
		bsf		swflag2,7	;set the debounce flag		
		incf	swflag2		;increment the flag for 8 presses of "A"
		btfsc	swflag2,3	;bit 3 will be HIGH after 8 presses of "A"
		goto	Out1
		goto	Code1F	
Code1D	clrf	swflag2	
Code1E	bcf		swflag2,7
Code1F	decfsz	fileD,1
		goto	Code1C		;micro loops constantly, flashing LED1 at 1Hz
		goto	Code1G		
		
		
Out1	call  	LED1		;turn on LED 1 for 2 seconds	
		call	Del_1
		call	Del_1
		call	Del_1		
		call	Del_1
		call  	LED0
				
		
							;LED2 flashes at 1Hz due to Del_1
							;hold button B down for 8 flashes then release
							;sub-routine detects button A and cancels count
							;count cancelled if button B not released after 8 flashes
							
			

Code2	clrf	swflag2		;clear switch flag2 - bit 3 will be set after 8 passes
Code2a	call  	LED2
		call	Del_1
		call	Switch		;see if button A or B is pressed 		
		movwf	swflag		;store switch value - use bit0=A, bit1=B
		btfss	swflag,1	;has B been pushed?
		clrf	swflag2		;Button B has been released
		btfsc	swflag,0	;has A been pushed?	
		clrf	swflag2
		call  	LED0
		call	Del_1
		call	Switch		;see if button A or B is pressed 		
		movwf	swflag		;store switch value - use bit0=A, bit1=B
		btfss	swflag,1	;has B been pushed?
		clrf	swflag2		;Button B has been released
		btfsc	swflag,0	;has A been pushed?	
		goto	Code2
		incf	swflag2,1		
		movlw	08			;look to see if B is released afer 8 passes
		xorwf	swflag2,0	;bit 3 will be HIGH after 8 passes
		btfss	03,2		;test the zero bit for equality of the two numbers
		goto	Code2a
		call  	LED2		;
		call	Del_1
		call  	LED0		;
		call	Del_1		
		call	Switch		;see if button A or B is pressed 		
		movwf	swflag		;store switch value - use bit0=A, bit1=B
		btfsc	swflag,1	;has B been pushed?
		goto	Code2		;B still pushed
							;micro goes to Out2
		
Out2	call  	LED2		;turn on LED 2 for 2 seconds	
		call	Del_1
		call	Del_1
		call	Del_1		
		call	Del_1
		call  	LED0	
		

							;ROUTINE FOR LED 3
							
							;All LEDs off
							;push button A and LEDs 1&3 come on
							;push button B three times then button A and 
							;LED 4 flashes at 1Hz




Code3	call	Switch		;see if button A or B is pressed 		
		movwf	swflag		;store switch value - use bit0=A, bit1=B
		btfss	swflag,0	;has A been pushed?
		goto	Code3
Code3a	call  	LED1		;A pushed. Turn on LEDs 1&3			
		call	_1mS
		call	_1mS
		call	_1mS
		call	_1mS
		call	_1mS
		call	_1mS			
		call  	LED3					
		call	Switch		;see if button B is pressed 		
		movwf	swflag		;store switch value 
		btfss	swflag,1	;has B been pushed?					
		goto	Code3b
		btfsc	swflag2,7	;first time pressed?
		goto	Code3a					
		bsf		swflag2,7	;set the debounce flag						
		incf	swflag2		;increment the flag 
		btfsc	swflag2,2	;bit 2 will be HIGH after 4 presses of "B"		
		goto	Code3c		;B has been pushed 4 times
		goto	Code3a
		
Code3b	bcf		swflag2,7	;clear the debounce flag
		goto	Code3a
	
		
Code3c	movlw	0ffh	
		movwf	fileD		;create 1 second delay with LEDs 1&3 ON
Code3d	call  	LED1
		call	_1mS
		call	_1mS		
		call  	LED3		
		call	_1mS
		call	_1mS
		decfsz	fileD,1			 
		goto 	Code3d	 	
		
Code3e	call	Switch		;see if button B is pressed 		
		movwf	swflag		;store switch value 
		btfsc	swflag,1	;has B been pushed?					
		goto	Code3
		btfss	swflag,0	;has A been pushed?					
		goto	Code3e
		
		

		
							;ROUTINE FOR LED 4
							
							;LED4 flashes at 1Hz. 
							;Button A, B, A, B must be pushed to get past LED 4.
							
							
							;Look for button A:
		
		
Code4	movlw	0D0h		;224 loops to create 0.5sec for LED4 ON
		movwf	fileD		;loop file is file D
Code4A	call  	LED4		;turn on LED 4
		call	Switch		;
		movwf	swflag		;store switch value A = bit1 
		btfsc	swflag,0	;has A been pushed?		
		goto	Code4C		;A pressed				
		decfsz	fileD,1
		goto	Code4A
		
		movlw	0D0h		;224 loops to create 0.5sec for LED4 OFF
		movwf	fileD		;loop file is file D
Code4B	call  	LED0		;turn off LED 4
		call	Switch		;see if button A is pressed 
		movwf	swflag		;store switch value 
		btfsc	swflag,0	;has A been pushed?
		goto	Code4C		;no button pressed
		decfsz	fileD,1
		goto	Code4B		;micro loops constantly, flashing LED4 at 1Hz
		goto	Code4		
		
	
							;Look for button B:
	
							
							
Code4C	movlw	0D0h		;224 loops to create 0.5sec for LED4 ON
		movwf	fileD		;loop file is file D
Code4D	call  	LED4		;turn on LED 4
		call	Switch		;	
		movwf	swflag		;store switch value B = bit1 
		btfsc	swflag,1	;has B been pushed?		
		goto	Code4F		;B pressed				
		decfsz	fileD,1
		goto	Code4D
		
		movlw	0D0h		;224 loops to create 0.5sec for LED4 OFF
		movwf	fileD		;loop file is file D
Code4E	call  	LED0		;turn off LED 4
		call	Switch		;see if button B is pressed 	
		movwf	swflag		;store switch value - bit1=B
		btfsc	swflag,1	;has B been pushed?
		goto	Code4F		;no button pressed
		decfsz	fileD,1
		goto	Code4E		;micro loops constantly, flashing LED4 at 1Hz
		goto	Code4		
		
		
				
							;Look for button A:
		
		
Code4F	movlw	0D0h		;224 loops to create 0.5sec for LED4 ON
		movwf	fileD		;loop file is file D
Code4G	call  	LED4		;turn on LED 4
		call	Switch		;
		movwf	swflag		;store switch value A = bit0 
		btfsc	swflag,0	;has A been pushed?		
		goto	Code4I		;A pressed				
		decfsz	fileD,1
		goto	Code4G
		
		movlw	0D0h		;224 loops to create 0.5sec for LED4 OFF
		movwf	fileD		;loop file is file D
Code4H	call  	LED0		;turn off LED 4
		call	Switch		;see if button A is pressed 
		movwf	swflag		;store switch value 
		btfsc	swflag,0	;has A been pushed?		
		goto	Code4I		;no button pressed
		decfsz	fileD,1
		goto	Code4H		;micro loops constantly, flashing LED4 at 1Hz
		goto	Code4F		
		
	
							;Look for button B:
	
							
							
Code4I	movlw	0D0h		;224 loops to create 0.5sec for LED4 ON
		movwf	fileD		;loop file is file D
Code4J	call  	LED4		;turn on LED 4
		call	Switch		;	
		movwf	swflag		;store switch value B= bit1 
		btfsc	swflag,1	;has B been pushed?		
		goto	Code5		;B pressed				
		decfsz	fileD,1
		goto	Code4J
		
		movlw	0D0h		;224 loops to create 0.5sec for LED4 OFF
		movwf	fileD		;loop file is file D
Code4K	call  	LED0		;turn off LED 4
		call	Switch		;see if button B is pressed 	
		movwf	swflag		;store switch value - bit1=B
		btfsc	swflag,1	;has B been pushed?
		goto	Code5		;no button pressed
		decfsz	fileD,1
		goto	Code4K		;micro loops constantly, flashing LED4 at 1Hz
		goto	Code4I		
		
		
											

							;ROUTINE FOR LED 5
							
							;LED5 illuminates 
							;Button B must be pushed 4 or more seconds after LED 5
							;comes on. If A or B is pushed before 4 seconds, time starts again
							
Code5	movlw	08h		
		movwf	fileD									
		call  	LED5					
Code5A	call	Del_1					
		decfsz	fileD,1
		goto	Code5A					
Code5B	call	Switch		;	
		movwf	swflag		;store switch value B= bit1 
		btfsc	swflag,1	;has B been pushed?		
		goto	Out5		;B pressed	
		btfsc	swflag,0	;has A been pushed?							
		goto	Code5				
		goto	Code5B
		
		
							
							
							;Routines 6,7,8,9,10 are available for constructors
							
		
Out5	call  	LED6		;All LEDs will scan back-and-forth
		call	_100mS
		call  	LED7
		call	_100mS
		call  	LED8
		call	_100mS
		call  	LED9		
		call	_100mS
		call  	LED10
		call	_100mS
		call  	LED9
		call	_100mS
		call  	LED8
		call	_100mS
		call  	LED7
		call	_100mS
		call  	LED6
		call	_100mS
		call  	LED5
		call	_100mS
		call  	LED4
		call	_100mS
		call  	LED3
		call	_100mS	
		call  	LED2
		call	_100mS
		call  	LED1
		call	_100mS
		call  	LED2
		call	_100mS
		call  	LED3
		call	_100mS
		call  	LED4
		call	_100mS
		call  	LED5
		call	_100mS	
		goto	Out5		
		
		
			
			

	end
		
	
	