	;Expt4.asm
        ;Project: Counting on 7-segment display
	;List P = 16F84
	;#include <p16F84.inc>
	;__CONFIG 1Bh    ;_CP_OFF & _PWRTE_ON & _WDT_OFF & _RC_OSC


	ORG 0		;This is the start of memory for the program.
SetUp	BSF 03,5	;Go to Bank 1
	CLRF 06		;Make all port B output
	MOVLW 01	;Load W with 0000 0001
	MOVWF 05	;Make RA0 input
	BCF 03,5	;Go to Bank 0 - the program memory area.
	CLRF 1F		;Clear the button-press file
	GOTO Main

Table	ADDWF 02h,1	;Add W to the Program Counter to create a jump.
	RETLW 3Fh	;0    format= gfedcba
	RETLW 06h	;1    If any table value has a leading letter, it must be
	RETLW 5Bh	;2    preceded with a "0."   E.g: 0A3h, 0FFh, 0CCh
	RETLW 4Fh	;3
	RETLW 66h	;4
	RETLW 6Dh	;5
	RETLW 7Dh	;6
	RETLW 07h	;7
	RETLW 7Fh	;8
	RETLW 6Fh	;9 
		

Delay	NOP		;Create 1mS debounce delay
	DECFSZ 1A,1
	GOTO Delay
	RETURN

Main	MOVLW 0FFh	;File 1E holds the count-value
	MOVWF 1E	;Next increment will make file=0
Main1	BTFSS 05,0	;Test the input line on port A
	GOTO Main2	;Button not pressed
	CALL Delay	;Debounce the button
	BTFSC 1F,0	;Button pressed first time?
	GOTO Main1	;Button already pressed
	INCF 1E,1	;First time button pressed. Increment count.
	MOVLW 0A	;Has count reached ten?
	XORWF 1E,0	;Compare file 1E with ten
	BTFSC 03,2	;Check the zero flag in Status file
	GOTO Main	;Count has reached ten
	MOVF 1E,0	;Copy count into W
	CALL Table	;W will return with display-value
	MOVWF 06	;Output display value
	BSF 1F,0	;Set button-press flag
	GOTO Main1	;Loop Main1
Main2	CALL Delay	;Button not pressed. Call Delay
	BCF 1F,0	;Clear button-press flag
	GOTO Main1	;Loop Main1

	END		;Tells assembler end of program
