	
	;2Digit Counter   12F629.asm
	;  13-6-2009 


	list	p=12F629
	radix	dec
	include	"p12f629.inc"
	
		errorlevel	-302	; Dont complain about BANK 1 Registers during assembly

	__CONFIG	_MCLRE_OFF & _CP_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT  ;Internal osc.


temp1		equ 20h	;
temp2		equ 21h	;
temp3		equ 22h	;
SwUp		equ	23h	;
SwDwn		equ	24h	;
units		equ	25h	;
tens		equ	26h	;
Sw_Flag		equ	27h	;
FastCount	equ	28h	;
Count		equ	29h	; counting 00 to 99
File5mS		equ 2Ah ;
FileXmS		equ 2Bh ;

;****************************************************************
;Equates
;****************************************************************
status		equ	0x03
rp1			equ	0x06
rp0			equ	0x05
portA 		equ 0x05
portB 		equ 0x06
	
			



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			;bit 5 of the status register
		

Start	org	0x00		;program starts at location 000
		nop
		nop
		nop
		nop		;NOPs to get past reset vector address
		nop
		nop


								;set up to allow counting from external oscillator

SetUp	bsf		status, rp0 	;Bank 1	
		movlw	b'11110111'		;Turn on T0CKI, prescale for TMR0 = 1:256
		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
		clrf 	units		
		clrf 	tens
		clrf	Sw_Flag		
		goto 	Main			
	

Table1	addwf	02,1	;Add W to the Program Counter to create a jump
		goto	showX0	;the zero on the right-hand display	
		goto	show01
		goto	show02
		goto	show03
		goto	show04
		goto	show05
		goto	show06
		goto	show07
		goto	show08
		goto	show09
		
Table2	addwf	02,1	;Add W to the Program Counter to create a jump
		goto	show0X 	;the zero on the left-hand display	
		goto	show10
		goto	show20
		goto	show30
		goto	show40
		goto	show50
		goto	show60
		goto	show70
		goto	show80
		goto	show90
		
		;Delay 0.005 sec (5mS)

_5mS	movlw 	05h
		movwf 	File5mS	
Del5mS	nop
		decfsz 	FileXmS,1		; ,1 denotes the result of the decrement 
		goto 	Del5mS	
		decfsz 	File5mS,1		;     is placed in the file	
		goto 	Del5mS
		retlw 	00

FastUp	btfss	Sw_Flag,2	;First time through loop?
		goto	FU_2		;yes
		btfsc	Sw_Flag,7	;Has 5Hz bit been set?
		goto	FU_3
FU_1	incfsz	FastCount,1	;Increment FastCount
		movlw	d'50'		;determines the time before fast incrementing
		xorwf	FastCount,0
		btfss	status,2	;reached 50 loops?
		retlw	00
		clrf	FastCount
		bsf		Sw_Flag,7	;set bit for 5Hz incrementing					
		

FU_2	bsf		Sw_Flag,2	;Up button has been pressed				
		incf	units,1
		movlw	0Ah			;put 10 into w
		xorwf	units,0		;compare units file with 10
		btfss	status,2	;zero flag in status file. Will be set if units is 10
		retlw	00
		clrf	units
		incf	tens,1
		movlw	0Ah			;put 10 into w
		xorwf	tens,0		;compare units file with 10
		btfsc	status,2	;zero flag in status file. Will be set if tens is 10
		clrf	tens				
		retlw	00			;display passes 99 but not below 0
		
		
FU_3	incfsz	FastCount,1	;Increment FastCount
		movlw	d'5'		;Detrmines the speed of the increments
		xorwf	FastCount,0
		btfss	status,2	;reached 5 loops?
		retlw	00
		clrf	FastCount		
		goto	FU_2
			
		
Dwn		btfsc	Sw_Flag,3
		retlw	00
		bsf		Sw_Flag,3
		decf	units,1
		movlw	0FFh		;put FFh into w
		xorwf	units,0		;compare units file with FFh
		btfss	status,2	;zero flag in status file. Will be set if units is 10
		retlw	00
		movlw	09
		movwf	units		;put 9 into units file
		decf	tens,1
		movlw	0FFh		;put 0FFh into w
		xorwf	tens,0		;compare tens file with 0FFh
		btfsc	status,2	;zero flag in status file. Will be set if tens is 0FFh
		goto	$+2			;tens file is 0FFh
		retlw	00						
		clrf	tens
		clrf	units
		retlw	00			;display  not below 0	
		
		
show01	
		bsf		status, rp0 	;Bank 1			
       	movlw	b'00011001'		;Set TRIS  to show seg B
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  
		movlw	b'00100101'		;   
		movwf	GPIO		
		call 	_5mS
		bsf		status, rp0 	;Bank 1			
       	movlw	b'10010011'		;Set TRIS to show seg C
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  		
		movlw	b'00000110'		;   
		movwf	GPIO
		call 	_5mS			
		retlw 	00			
		
show02	
		bsf		status, rp0 	;Bank 1			
       	movlw	b'00000101'		;Set TRIS  to show seg A & E
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  
		movlw	b'00010111'		;   
		movwf	GPIO		
		call 	_5mS
		bsf		status, rp0 	;Bank 1			
       	movlw	b'10011000'		;Set TRIS to show seg B & D & G
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  		
		movlw	b'00100000'		;   
		movwf	GPIO
		call 	_5mS						
		retlw 	00				
		
show03	
		bsf		status, rp0 	;Bank 1			
       	movlw	b'00010001'		;Set TRIS  to show seg A & C
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  
		movlw	b'00010111'		;   
		movwf	GPIO		
		call 	_5mS
		bsf		status, rp0 	;Bank 1			
		movlw	b'10011000'		;Set TRIS to show seg B & D & G  
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  		
		movlw	b'00100000'		;   
		movwf	GPIO
		call 	_5mS						
		retlw 	00		
		
		
show04	
		bsf		status, rp0 	;Bank 1			
       	movlw	b'00010011'		;Set TRIS  to show seg  C
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  
		movlw	b'00010111'		;   
		movwf	GPIO		
		call 	_5mS
		bsf		status, rp0 	;Bank 1			
		movlw	b'10001100'		;Set TRIS to show seg B & F & G
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  		
		movlw	b'00100000'		;   
		movwf	GPIO
		call 	_5mS							
		retlw 	00			

show05	
		bsf		status, rp0 	;Bank 1			
       	movlw	b'00010001'		;Set TRIS  to show seg A & C
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  
		movlw	b'00010111'		;   
		movwf	GPIO		
		call 	_5mS
		bsf		status, rp0 	;Bank 1			
		movlw	b'10001010'		;Set TRIS to show seg D & F & G
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  		
		movlw	b'00100000'		;   
		movwf	GPIO
		call 	_5mS						
		retlw 	00			
				
show06	
		bsf		status, rp0 	;Bank 1			
       	movlw	b'00000001'		;Set TRIS  to show seg A & C & E
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  
		movlw	b'00010111'		;   
		movwf	GPIO		
		call 	_5mS
		bsf		status, rp0 	;Bank 1			
		movlw	b'10001010'		;Set TRIS to show seg D & F & G
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  		
		movlw	b'00100000'		;   
		movwf	GPIO
		call 	_5mS						
		retlw 	00			
				
show07	
		bsf		status, rp0 	;Bank 1			
       	movlw	b'00010001'		;Set TRIS  to show seg A & C
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  
		movlw	b'00010111'		;   
		movwf	GPIO		
		call 	_5mS
		bsf		status, rp0 	;Bank 1			
		movlw	b'10011101'		;Set TRIS to show seg B
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  		
		movlw	b'00100000'		;   
		movwf	GPIO
		call 	_5mS						
		retlw 	00			
				
show08	
		bsf		status, rp0 	;Bank 1			
       	movlw	b'00000001'		;Set TRIS  to show seg A & C & E
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  
		movlw	b'00010111'		;   
		movwf	GPIO		
		call 	_5mS
		bsf		status, rp0 	;Bank 1			
		movlw	b'10001000'		;Set TRIS to show seg B & D & F & G
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  		
		movlw	b'00100000'		;   
		movwf	GPIO
		call 	_5mS						
		retlw 	00			
				
show09	
		bsf		status, rp0 	;Bank 1			
       	movlw	b'00010001'		;Set TRIS  to show seg A & C
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  
		movlw	b'00010111'		;   
		movwf	GPIO		
		call 	_5mS
		bsf		status, rp0 	;Bank 1			
		movlw	b'10001000'		;Set TRIS to show seg B & D & F & G
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  		
		movlw	b'00100000'		;   
		movwf	GPIO
		call 	_5mS						
		retlw 	00			
				
showX0	
		bsf		status, rp0 	;Bank 1			
       	movlw	b'00000001'		;Set TRIS  to show seg A & C & E
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  
		movlw	b'00010111'		;   
		movwf	GPIO		
		call 	_5mS
		bsf		status, rp0 	;Bank 1			
		movlw	b'10001001'		;Set TRIS to show seg B & D & F 
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  		
		movlw	b'00100000'		;   
		movwf	GPIO
		call 	_5mS							
		retlw 	00			
			
		
show0X	
		bsf		status, rp0 	;Bank 1			
       	movlw	b'00100000'		;Set TRIS  to show seg A & C & E
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  
		movlw	b'00010110'		;   
		movwf	GPIO		
		call 	_5mS
		bsf		status, rp0 	;Bank 1			
		movlw	b'10101000'		;Set TRIS to show seg B & D & F 
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  		
		movlw	b'00100001'		;   
		movwf	GPIO
		call 	_5mS							
		retlw 	00			
							
show10	
		bsf		status, rp0 	;Bank 1			
       	movlw	b'00111000'		;Set TRIS  to show seg B
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  
		movlw	b'00100101'		;   
		movwf	GPIO		
		call 	_5mS
		bsf		status, rp0 	;Bank 1			
       	movlw	b'10110010'		;Set TRIS to show seg C
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  		
		movlw	b'00000110'		;   
		movwf	GPIO
		call 	_5mS			
		retlw 	00		
		
		
					
					
show20	
		bsf		status, rp0 	;Bank 1			
       	movlw	b'00100100'		;Set TRIS  to show seg A & E
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  
		movlw	b'00010110'		;   
		movwf	GPIO		
		call 	_5mS
		bsf		status, rp0 	;Bank 1			
       	movlw	b'10011000'		;Set TRIS to show seg B & D & G
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  		
		movlw	b'00000001'		;   
		movwf	GPIO
		call 	_5mS							
		retlw 	00
		
show30	
		bsf		status, rp0 	;Bank 1			
       	movlw	b'00110000'		;Set TRIS  to show seg A & C
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  
		movlw	b'00010110'		;   
		movwf	GPIO		
		call 	_5mS
		bsf		status, rp0 	;Bank 1			
		movlw	b'10011000'		;Set TRIS to show seg B & D & G
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  		
		movlw	b'00000001'		;   
		movwf	GPIO
		call 	_5mS							
		retlw 	00		
				
show40	
		bsf		status, rp0 	;Bank 1			
       	movlw	b'00110010'		;Set TRIS  to show seg  C
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  
		movlw	b'00010110'		;   
		movwf	GPIO		
		call 	_5mS
		bsf		status, rp0 	;Bank 1			
		movlw	b'10001100'		;Set TRIS to show seg B & F & G
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  		
		movlw	b'00000001'		;   
		movwf	GPIO
		call 	_5mS						
		retlw 	00			
		
show50	
		bsf		status, rp0 	;Bank 1			
       	movlw	b'00110000'		;Set TRIS  to show seg A & C
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  
		movlw	b'00010110'		;   
		movwf	GPIO		
		call 	_5mS
		bsf		status, rp0 	;Bank 1			
		movlw	b'10001010'		;Set TRIS to show seg D & F & G
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  		
		movlw	b'00000001'		;   
		movwf	GPIO
		call 	_5mS							
		retlw 	00
		
		
show60	
		bsf		status, rp0 	;Bank 1			
       	movlw	b'00100000'		;Set TRIS  to show seg A & C & E
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  
		movlw	b'00010110'		;   
		movwf	GPIO		
		call 	_5mS
		bsf		status, rp0 	;Bank 1			
		movlw	b'10001010'		;Set TRIS to show seg D & F & G
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  		
		movlw	b'00000001'		;   
		movwf	GPIO
		call 	_5mS						
		retlw 	00			
		
							
show70	
		bsf		status, rp0 	;Bank 1			
       	movlw	b'00110000'		;Set TRIS  to show seg A & C
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  
		movlw	b'00010110'		;   
		movwf	GPIO		
		call 	_5mS
		bsf		status, rp0 	;Bank 1			
		movlw	b'10111100'		;Set TRIS to show seg B
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  		
		movlw	b'00000001'		;   
		movwf	GPIO
		call 	_5mS							
		retlw 	00	
		
show80	
		bsf		status, rp0 	;Bank 1			
       	movlw	b'00100000'		;Set TRIS  to show seg A & C & E
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  
		movlw	b'00010110'		;   
		movwf	GPIO		
		call 	_5mS
		bsf		status, rp0 	;Bank 1			
		movlw	b'10001000'		;Set TRIS to show seg B & D & F & G
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  		
		movlw	b'00000001'		;   
		movwf	GPIO
		call 	_5mS						
		retlw 	00		
		
show90	
		bsf		status, rp0 	;Bank 1			
       	movlw	b'00110000'		;Set TRIS  to show seg A & C
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  
		movlw	b'00010110'		;   
		movwf	GPIO		
		call 	_5mS
		bsf		status, rp0 	;Bank 1			
		movlw	b'10001000'		;Set TRIS to show seg B & D & F & G
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0  		
		movlw	b'00000001'		;   
		movwf	GPIO
		call 	_5mS							
		retlw 	00							
		
		
;****************************************************************
;* Main 							*
;****************************************************************

Main	bsf		status, rp0 	;Bank 1			
       	movlw	b'11111101'		;Set TRIS  
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0 		
		movlw	b'00000000'		;Set GP1 low  
		movwf	GPIO
		btfsc	GPIO,3
		call	FastUp			;UP switch pressed
		bsf		status, rp0 	;Bank 1			
       	movlw	b'11111101'		;Set TRIS  
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0 		
		movlw	b'00000010'		;Set GP1 High 
		movwf	GPIO
		btfss	GPIO,3			;down produces 90
		call	Dwn				;Down switch pressed
		clrf	GPIO
		movf	units,0			;copy unit value into w
		call	table1			;unit display value will return in w
		movf	tens,0			;copy tens value into w		
		call	table2			;tens display value will return in w
		bsf		status, rp0 	;Bank 1			
       	movlw	b'11111101'		;Set TRIS  
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0 		
		movlw	b'00000010'		;Set GP1 High 
		movwf	GPIO
		btfsc	GPIO,3			;bit will be zero when sw is pressed - down		
		bcf		Sw_Flag,3		;button not pressed. Clear down flag			
		bsf		status, rp0 	;Bank 1			
       	movlw	b'11111101'		;Set TRIS  
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0 		
		movlw	b'00000000'		;Set GP1 low  Testing for UP switch
		movwf	GPIO
		btfsc	GPIO,3			;UP will be High when pressed
		goto	Main		
		bcf		Sw_Flag,2		;button not pressed. Clear Up flag
		bcf		Sw_Flag,7		;Clear Up repeat flag
		clrf	FastCount
		goto	Main	
				
		
			;oscillator calibration


		call 	03ffh
		movwf	OSCCAL		

		end
		

	
	