;Switch-3 Program for 5x7 Video Screen
;PIC16F84 and only F84 chip
;Detects Buttons A, B and C (with debounce) 
;and flashes LEDs


Start   ORG 0x00
	BSF 03,5
	MOVLW 1Ch	;Make RA2, RA3 and RA4 inputs
	MOVWF 05h
	MOVLW 00h	;Put 00 into W	
	MOVWF 06h	;Make all RB lines output
	BCF 03,5	;Go to Page0 for programming

	MOVWF 06h	;Put 00 into output port to turn off all LEDs
	MOVWF 1Dh	;zero the debounce file
	MOVWF 1Eh	;Zero the flag file

Scan1	MOVLW 0FFh
	MOVWF 1Ah
	MOVLW 080h
	MOVWF 1Bh
Scan2	BTFSS 05,2	;Test if Button A pressed. (Not pressed, line = HIGH)
	GOTO AA		;Button A is pressed
	BTFSS 1E,5	;A not pressed. Check to see if flag bit 5 is SET
	GOTO Scan3	;Not SET
	MOVF 1Dh,0	;Bit 5  SET. Copy 1D into W
	XORLW 00
	BTFSC 03,2	;Is file 1D = 0? 
	GOTO Scan5	;Debounce file is zero
	DECFSZ 1Dh,1	;Decrement debounce file
	GOTO Scan5
	BCF 1E,5
Scan3	BTFSS 05,3	;Test if button B pressed
	GOTO BB		;B pressed
	BTFSS 1E,6	;B not pressed.  Check to see if flag bit 6 is SET
	GOTO Scan4	;Not SET
	MOVF 1Dh,0	;Bit 6 SET. Copy 1D into W
	XORLW 00
	BTFSC 03,2	;Is file 1D = 0? 
	GOTO Scan5	;Debounce file is zero
	DECFSZ 1Dh,1	;Decrement debounce file
	GOTO Scan5
	BCF 1E,6
Scan4	BTFSS 05,4	;Test if button C pressed.
	GOTO CC		;C pressed
	BTFSS 1E,7	;C not pressed.  Check to see if flag bit 7 is SET
	GOTO Scan5	;Not SET
	MOVF 1Dh,0	;Bit 7 SET. Copy 1D into W
	XORLW 00
	BTFSC 03,2	;Is file 1D = 0? 
	GOTO Scan5	;Debounce file is zero
	DECFSZ 1Dh,1	;Decrement debounce file
	GOTO Scan5
	BCF 1E,7
Scan5	NOP
Del1	DECFSZ 1Ah,1	;Delay section
	GOTO Del1
	DECFSZ 1Bh,1
	GOTO Scan2
	BTFSS 1E,2	;Test for LED A active
	GOTO Scan8
	MOVLW 01
	XORWF 06h,1	;Toggle LED A
Scan6	BTFSS 1E,3	;Test for LED B active
	GOTO Scan9
	MOVLW 08
	XORWF 06h,1	;Toggle LED B
Scan7	BTFSS 1Eh,4	;Test for LED C active
	GOTO Scan10
	MOVLW 40h
	XORWF 06h,1	;Toggle LED C
	GOTO Scan1

Scan8	BCF 06h,0
	GOTO Scan6
Scan9	BCF 06h,3
	GOTO Scan7
Scan10	BCF 06h,6
	GOTO Scan1
	



AA	MOVLW 00
	XORWF 1D,0	;Is 1D = 0?
	BTFSS 03,2
	GOTO Scan5	;1D is not zero
	BSF 1E,5	;1D is zero. Put a flag in 1E for button A
	MOVLW 04h	;Toggle LED A ON/OFF
	XORWF 1E,1	
	MOVLW 10h	;Put 40h into debounce file 1D
	MOVWF 1Dh
	GOTO Scan5


BB	MOVLW 00
	XORWF 1D,0	;Is 1D = 0?
	BTFSS 03,2
	GOTO Scan5	;1D is not zero
	BSF 1E,6	;1D is zero. Put a flag in 1E for button B
	MOVLW 08h	;Toggle LED B ON/OFF
	XORWF 1E,1	
	MOVLW 10h	;Put 40h into debounce file 1D
	MOVWF 1Dh
	GOTO Scan5

CC	MOVLW 00
	XORWF 1D,0	;Is 1D = 0?
	BTFSS 03,2
	GOTO Scan5	;1D is not zero
	BSF 1E,7	;1D is zero. Put a flag in 1E for button C
	MOVLW 10h	;Toggle LED C ON/OFF
	XORWF 1E,1	
	MOVLW 10h	;Put 40h into debounce file 1D
	MOVWF 1Dh
	GOTO Scan5

	
	END
		