	
	;Up/Down Counter for Bikes.
	;Down for 1 then up for 2,3,4,5,6
	;or if up is pressed at the beginning, display shows 2
	;   12F629.asm
	;  13-3-2010 


	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	;
count		equ	23h	; current value for display
Sw_Flag		equ	24h	;
File5mS		equ	25h
FileXmS		equ	26h

;****************************************************************
;Equates
;****************************************************************
status		equ	0x03
rp1			equ	0x06
rp0			equ	0x05
GPIO 		equ 0x05
			



status		equ	03h
option_reg	equ 81h


						; bits on GPIO
				
pin7		equ	0	;GP0  reset  and cycle 1-6 on display - input
pin6	 	equ	1	;GP1   segments a low b high     - output
pin5		equ	2	;GP2    segments c  low d  high  - output
pin4		equ	3	;GP3 input only
pin3		equ	4	;GP4    segments e low  f high  - output
pin2		equ	5	;GP5     g low     - output
 

						;bits
				
rp0		equ	5			;bit 5 of the status register



;****************************************************************
;Beginning of program
;****************************************************************
Start	org		0x00	;reset vector address
		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'11001001'		;Set TRIS  
		movwf	TRISIO	   	    ;	
		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	Sw_Flag	
		clrf	count			;initialise count = 0
		;incf	count,f				;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx	
		goto 	M_First			
	


table	addwf   PCL,F     ;02h,1  add W to program counter 
        goto	show0     ; "0"   -|F|E|D|C|B|A
        goto	show1     ; "1"   -|-|-|-|C|B|-
        goto	show2     ; "2"   G|-|E|D|-|B|A
        goto	show3     ; "3"   G|-|-|D|C|B|A 
        goto	show4     ; "4"   G|F|-|-|C|B|-
        goto	show5     ; "5"   G|F|-|D|C|-|A
        goto	show6     ; "6"   G|F|E|D|C|-|A
        goto	show7     ; "7"   -|-|-|-|C|B|A
        goto	show8     ; "8"   G|F|E|D|C|B|A
        goto	show9     ; "9"   G|F|-|D|C|B|A



;****************************************************************
;* Delay 10mS 		10 x 1,000uS			*
;****************************************************************

		
_10mS	movlw	0Ah
		movwf	temp2
D_a		nop
		decfsz temp1,1
		goto D_a
		decfsz temp2,1
		goto D_a	
		retlw 00
		
		
_3mS	movlw 	03h
		movwf 	File5mS	
D_3mS	nop
		decfsz 	FileXmS,1		; ,1 denotes the result of the decrement 
		goto 	D_3mS	
		decfsz 	File5mS,1		;     is placed in the file	
		goto 	D_3mS
		retlw 	00
		
				
		;Delay 0.005 sec (5mS)

_5mS	movlw 	05h
		movwf 	File5mS	
D_5mS	nop
		decfsz 	FileXmS,1		; ,1 denotes the result of the decrement 
		goto 	D_5mS	
		decfsz 	File5mS,1		;     is placed in the file	
		goto 	D_5mS
		retlw 	00



;****************************
;* Sub-routines		*
;****************************

show0	bsf		status, rp0 	;Bank 1			
       	movlw	b'11101001'		;Set TRIS  
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0 	
		movlw	b'00000000'		;turn on a c e  
		movwf	GPIO	
		call	_5mS
		movlw	b'00010110'		;turn on b d f  
		movwf	GPIO	
		call	_5mS
		retlw	00

show1	bsf		status, rp0 	;Bank 1			
       	movlw	b'11111001'		;Set TRIS  
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0 	
		movlw	b'00000100'		;turn on b c   
		movwf	GPIO	
		call	_5mS
		bsf		status, rp0 	;Bank 1			
       	movlw	b'11111111'		;Set TRIS all input 
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0 
		call	_5mS			;turn all outputs off
		retlw	00


show2	bsf		status, rp0 	;Bank 1			
       	movlw	b'11001101'		;Set TRIS  
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0 	
		movlw	b'00010000'		;   
		movwf	GPIO	
		call	_5mS
		bsf		status, rp0 	;Bank 1			
       	movlw	b'11111001'		;Set TRIS  
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0 
		movlw	b'00000010'		;   
		movwf	GPIO	
		call	_5mS			;
		retlw	00

show3	bsf		status, rp0 	;Bank 1			
       	movlw	b'11011001'		;Set TRIS  
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0 	
		movlw	b'00000000'		;   
		movwf	GPIO	
		call	_5mS
		bsf		status, rp0 	;Bank 1			
       	movlw	b'11111001'		;Set TRIS  
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0 
		movlw	b'00000110'		;   
		movwf	GPIO	
		call	_5mS			;
		retlw	00


show4	bsf		status, rp0 	;Bank 1			
       	movlw	b'11011011'		;Set TRIS  
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0 	
		movlw	b'00000110'		;   
		movwf	GPIO	
		call	_5mS
		bsf		status, rp0 	;Bank 1			
       	movlw	b'11101101'		;Set TRIS  
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0 
		movlw	b'00000000'		;   
		movwf	GPIO	
		call	_5mS			;
		retlw	00

show5	bsf		status, rp0 	;Bank 1			
       	movlw	b'11011001'		;Set TRIS  
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0 	
		movlw	b'00000010'		;   
		movwf	GPIO	
		call	_5mS
		bsf		status, rp0 	;Bank 1			
       	movlw	b'11101011'		;Set TRIS  
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0 
		movlw	b'00000110'		;   
		movwf	GPIO	
		call	_5mS			;
		retlw	00


show6	bsf		status, rp0 	;Bank 1			
       	movlw	b'11001001'		;Set TRIS  
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0 	
		movlw	b'00000010'		;   
		movwf	GPIO	
		call	_5mS
		bsf		status, rp0 	;Bank 1			
       	movlw	b'11101011'		;Set TRIS  
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0 
		movlw	b'00010110'		;   
		movwf	GPIO	
		call	_5mS			;
		retlw	00


show7	bsf		status, rp0 	;Bank 1			
       	movlw	b'11111001'		;Set TRIS  
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0 	
		movlw	b'00000000'		;   
		movwf	GPIO	
		call	_5mS
		bsf		status, rp0 	;Bank 1			
       	movlw	b'11111101'		;Set TRIS  
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0 
		movlw	b'00010110'		;   
		movwf	GPIO	
		call	_5mS			;
		retlw	00


show8	bsf		status, rp0 	;Bank 1			
       	movlw	b'11001001'		;Set TRIS  
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0 	
		movlw	b'00100000'		;   
		movwf	GPIO	
		call	_5mS
		bsf		status, rp0 	;Bank 1			
       	movlw	b'11111101'		;Set TRIS  
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0 
		movlw	b'00110110'		;   
		movwf	GPIO	
		call	_5mS			;
		retlw	00


show9	bsf		status, rp0 	;Bank 1			
       	movlw	b'11011001'		;Set TRIS  
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0 	
		movlw	b'00000000'		;   
		movwf	GPIO	
		call	_5mS
		bsf		status, rp0 	;Bank 1			
       	movlw	b'11101001'		;Set TRIS  
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0 
		movlw	b'00110010'		;   
		movwf	GPIO	
		call	_5mS			;
		retlw	00

		
Up		btfsc	Sw_Flag,1
		goto	M1
		bsf		Sw_Flag,1
		movlw	06h			;put 6 into w
		xorwf	count,0		;compare units file with 6
		btfsc	03,2		;zero flag in status file. Will be set if units is 6
		goto	M1
		incf	count,f		;increment display
		goto	M1			;
		
		
Down	btfsc	Sw_Flag,1
		goto	M1
		bsf		Sw_Flag,1
		movlw	01h			;put 1 into w
		xorwf	count,0		;compare units file with 1
		btfsc	03,2		;zero flag in status file. Will be set if units is 1
		goto	M1
		movlw	00h			;put 0 into w
		xorwf	count,0		;compare units file with 1
		btfsc	03,2		;zero flag in status file. Will be set if units is 0
		goto	M1		
		decf	count,f		;decrement display
		goto	M1			;
		
		
		;changes for R Cermak    Reset ONLY produces 0
		
Reset	bsf		status, rp0 	;Bank 1			
       	movlw	b'11111101'		;Set TRIS  to detect switches GP0=in GP3=in 
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0
		call	show0
		
		btfsc	GPIO,0	
		goto	$+4 			;reset not pushed
		btfss	GPIO,3			;reset or down pushed
		goto	$+1				;	
		goto	Reset			;reset pushed
		goto	SetUp			;reset not pushed
		
		
		

		;btfsc	Sw_Flag,1
		;goto	M1
		;bsf		Sw_Flag,1
		;movlw	06h			;put 6 into w
		;xorwf	count,0		;compare units file with 6
		;btfsc	03,2		;zero flag in status file. Will be set if units is 6
		;goto	$+3
		;incf	count,f		;increment display
		;goto	M1			
		;goto	SetUp
	
;****************************************************************
;* Main 							*
;****************************************************************

			;On turn-on display 0 and detects reset down and UP
			
			
M_First	bsf		status, rp0 	;Bank 1			
       	movlw	b'11111101'		;Set TRIS  to detect switches GP0=in GP3=in 
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0 
		btfsc	GPIO,0	
		goto	$+4 			;reset not pushed
		btfss	GPIO,3			;reset or down pushed
		goto	Up				;down pushed but count UP!!!	
		goto	Reset			;reset pushed
		btfss	GPIO,3
		goto	$+5				;up pushed - display to show 2	
		clrf	Sw_Flag			;nothing pushed		
		movf	count,w
		call	table			
		goto	M_First

		incf	count,1
		goto	Up


Main	bsf		status, rp0 	;Bank 1			
       	movlw	b'11111101'		;Set TRIS  to detect switches GP0=in GP3=in 
		movwf	TRISIO	   	    ;	
		bcf		status, rp0		;bank 0 
		btfsc	GPIO,0	
		goto	$+4 			;reset not pushed
		btfss	GPIO,3			;reset or down pushed
		goto	Down			;down pushed	
		goto	Reset			;reset pushed
		btfss	GPIO,3
		goto	Up				;up pushed	
		clrf	Sw_Flag			;nothing pushed		
M1		movf	count,w
		call	table			
		goto	Main
		
	
		
			;oscillator calibration


		call 	03ffh
		movwf	OSCCAL		

		end
		

	
	