  	       ;Experiment-14 for "5x7 Video Screen" Project      
  	        ;PIC16F84 and only F84 chip         
      		    ;  0-9 Up Counter
	
Start   ORG 0x00
        BSF 03,5        ;Go to page1 for setting-up the ports	 
	MOVLW 0Ch       ;Put 0C into W     
	MOVWF 05h       ;  to make RA2 & RA3 input 
	MOVLW 00h	;Put 00 into W   
        MOVWF 06h       ;to make all RB lines output
        BCF 03,5        ;Go to Page0 for programming
	GOTO Main




Table1	ADDWF 02h,1	;Add W to Program Counter
	RETLW 3Eh	;0
	RETLW 41h	
	RETLW 41h
	RETLW 3Eh		
	RETLW 00h	;1
	RETLW 20h
	RETLW 7Fh	
	RETLW 00h
	RETLW 27h	;2
	RETLW 49h
	RETLW 49h
	RETLW 31h
	RETLW 22h	;3
	RETLW 49h	
	RETLW 49h
	RETLW 36h	
	RETLW 1Ch	;4
	RETLW 24h
	RETLW 7Fh
	RETLW 04h	
	RETLW 7Ah	;5
	RETLW 49h
	RETLW 49h
	RETLW 46h
	RETLW 3Eh	;6
	RETLW 49h
	RETLW 49h	
	RETLW 26h
	RETLW 40h	;7
	RETLW 47h
	RETLW 48h
	RETLW 70h	
	RETLW 36h	;8
	RETLW 49h
	RETLW 49h
	RETLW 36h
	RETLW 32h	;9
	RETLW 49h
	RETLW 49h	
	RETLW 3Eh
	RETLW 00h
	

    ;"LHD" Moves 4 bytes of data from Table1 plus a BLANK 
	; to output to produce Left-hand digit
	;  It scans the Video Screen ONCE

LHD	BSF 05,1	;Reset 4017
	NOP
	BCF 05,1

	MOVF 17h,0	;Copy 17h to W 
	MOVWF 23h	;17h copied to 23h for temp incrementing
	MOVLW 04
	MOVWF 18h	;4 cycles of Loading in count file 18h
LHD2	MOVF 23h,0	;Copy 23h to W
	CALL Table1
	MOVWF 06h
	CALL Del
	INCF 23h,1
	DECFSZ 18h,1
	GOTO LHD2
	MOVLW 00	;Last column is blank as numbers are squashed.
	MOVWF 15h
	CALL Del
	RETURN


RHD	BSF 05,1	;Reset 4017
	NOP
	BCF 05,1

	MOVLW 00	;First column is blank as numbers are squashed.
	MOVWF 06h
	CALL Del
	MOVF 19h,0	;Copy 19h to W 
	MOVWF 24h	;19h copied to 24h for temp incrementing
	MOVLW 04
	MOVWF 18h	;4 cycles of Loading in count file 18h
RHD2	MOVF 24h,0	;Copy 23h to W
	CALL Table1
	MOVWF 06h
	CALL Del
	INCF 24h,1
	DECFSZ 18h,1
	GOTO RHD2	
	RETURN

Del	DECFSZ 1Bh,1
	GOTO Del
	MOVLW 00h
	MOVWF 06
Clk	BCF 05,0	;Clock the 4017 to the
	NOP	 	;   next output
	BSF 05,0
	RETURN
	

Delay	MOVLW 04
	MOVWF 1B
Delay1	DECFSZ 1Ah,1	;Delay for main program for scanning
	GOTO Delay1
	DECFSZ 1B,1
	GOTO Delay1	
	RETURN
	

 ;00 to 99 COUNTER

OOto99	INCF 22h,1
	INCF 19h,1
	INCF 19h,1
	INCF 19h,1
	INCF 19h,1
	BTFSS 22h,1	;Bits 1 & 3 make up the number 10!
	RETURN
	BTFSS 22h,3
	RETURN		;File 22h is not 10!
	MOVLW 00
	MOVWF 22h	;Zero the digits file
	MOVWF 19h	;Zero the pointer for RHD     
        INCF 21h,1
	INCF 17h,1
	INCF 17h,1
	INCF 17h,1
	INCF 17h,1
	BTFSS 21h,1
	RETURN
	BTFSS 21h,3
	RETURN		;File 21h is not 10!
	MOVLW 00
	MOVWF 21h	;Zero file 21h
	MOVWF 17h	;Zero the pointer for LHD
	RETURN

SwA	BTFSS 05h,2	;Test for buttonA
	GOTO SwA1	;Pushed	
	GOTO SwA2	;NOT pushed
SwA1	BTFSC 1E,5	;First time for press A?
	RETURN	
	BSF 1E,5	;Set the button flag 
	CALL OOto99	;Increment count
	RETURN
SwA2	BCF 1E,5
	RETURN

	
	;MAIN ROUTINE


Main	MOVLW 00
	MOVWF 21h	;21h is units file. Start=0
	MOVWF 22h	;22h is 10's file. Start=0
	MOVWF 17h	;1's pointer in table1
	MOVWF 19h	;10's pointer in table1
Main1	MOVLW 60h	;Delay to show LHD
	MOVWF 1Ch
Main2	CALL LHD
	CALL SwA
	DECFSZ 1Ch,1
	GOTO Main2
	MOVLW 10h	;Delay after first digit is displayed
	MOVWF 1Ch
Main3	CALL Delay
	CALL SwA
	DECFSZ 1Ch,1
	GOTO Main3
	MOVLW 60h	;Delay to show RHD
	MOVWF 1Ch
Main4	CALL RHD
	CALL SwA
	DECFSZ 1Ch,1
	GOTO Main4
	MOVLW 0A0h	;Delay after RHD
	MOVWF 1Ch
Main5	CALL Delay
	CALL SwA
	DECFSZ 1Ch,1
	GOTO Main5	
	GOTO Main1	;Repeat routine

        END




