	
	;Blank12F629.asm
	;CODE PUZZLE 


	list	p=12F629
	
	include	"p12f629.inc"

	__CONFIG	_MCLRE_OFF & _CP_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT  ;Internal osc.

	
			; globals
aaaa	equ 	20h		;this is the first available file
swflag	equ 	21h		;switch flag file - use bits 0,1
ccc		equ 	22h
ddd		equ 	23h
eee		equ 	24h
fff		equ		25h

fileA	equ		26h
fileB	equ		27h	
fileC	equ		28h
fileD	equ		29h
fileE	equ		2Ah
	

filezz	equ		5Fh	;this is the last available file

status		equ	03h
option_reg	equ 	81h


				; bits on GPIO
				
pin7		equ	0	;GP0  
pin6	 	equ	1	;GP1
pin5		equ	2	;GP2
pin4		equ	3	;GP3 input only
pin3		equ	4	;GP4 
pin2		equ	5	;GP5 


				;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, prescale for TMR0 = 1:128
		movwf	option_reg
        movlw   07h         ;Set up W to turn off Comparator ports
        movwf   CMCON    
		movlw	b'00001000'	;Set GP0,1,2,4,5, as output
		movwf	TRISIO	    ;enable outputs	
		bcf	status,rp0		;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	10h				;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	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
		
	
Switch	bsf		status,rp0 		;Bank 1	
		bCf		TRISIO,0	   	;make GP0 output	
		bcf		status,rp0		;bank 0
		bcf		GPIO,0			;make switch-line LOW
DelSw1	decfsz 	fileA,1			; short delay while 100n discharges
		goto 	DelSw1	 
		bsf		status,rp0 		;Bank 1	
		bsf		TRISIO,0	   	;make GP0 input	
		bcf		status,rp0		;bank 0
		movlw	03
		movwf	fileB
DelSw2	decfsz 	fileA,1			; delay while 100n charges via switch
		goto 	DelSw2	
		decfsz 	fileB,1
		goto 	DelSw2		
		btfsc	GPIO,0			;is switch-line HIGH?
		retlw 	01				;yes - switch A pressed
DelSw3	decfsz 	fileA,1			; delay while 100n charges via switch
		goto 	DelSw3
DelSw4	decfsz 	fileA,1			; delay while 100n charges via switch
		goto 	DelSw4		
		btfsc	GPIO,0			;is switch-line HIGH?
		retlw 	02				;yes - switch B pressed
		retlw	00				;no switch pressed
		
		
		
							;LED 1 flashing at 1Hz. Program looks for button A 
							;pressed 3 times within 1 second and then no presses 
							;for 2 seconds before turning on LED 1 fully, to go
							;to next stage of flashing next LED.	
							;If button B pressed, go to Main	
	
	
Main	call	LED1
		call	Del_1
		call	LED0
		call	Del_1
		
		goto 	Main

			;oscillator calibration


		call 	03ffh
		movwf	OSCCAL	
		

	end
		

	
	