	
	;Up/Down Counter for Cars.
	;1,2,3,4
	;
	;  1-8-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	;

;****************************************************************
;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   cycle 1-4 on LEDs - input
pin6	 	equ	1	;GP1   LED1    - output
pin5		equ	2	;GP2   LED 2  - output
pin4		equ	3	;GP3   input only
pin3		equ	4	;GP4   LED 3  - output
pin2		equ	5	;GP5   LED 4  - 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  to detect switches GP0=in GP3=in 
		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 = 1
		incf	count,1
		goto 	Main	
		 
		
		
		
			
	
table	addwf   PCL,F     ;02h,1  add W to program counter 
        nop
        goto	show1     ;show LED1
        goto	show2     ;show LED2
        goto	show3     ;show LED3
        goto	show4     ;show LED4

;****************************************************************
;* Delay 20mS 		20 x 1,000uS			*
;****************************************************************

		
_20mS	movlw	14h
		movwf	temp2
D_a		nop
		decfsz temp1,1
		goto D_a
		decfsz temp2,1
		goto D_a	
		retlw 00
		
		

;****************************
;* Sub-routines		*
;****************************

		
Up		btfsc	Sw_Flag,1
		goto	M1
		bsf		Sw_Flag,1
		movlw	04h			;put 4 into w
		xorwf	count,0		;compare units file with 4
		btfsc	03,2		;zero flag in status file. Will be set if units is 4
		goto	M1
		incf	count,f		;increment display
		goto	M1			;
		
		;cycle 1-4
		
		
Cycle	btfsc	Sw_Flag,1
		goto	M1
		bsf		Sw_Flag,1
		movlw	04h			;put 4 into w
		xorwf	count,0		;compare units file with 4
		btfsc	03,2		;zero flag in status file. Will be set if units is 4
		goto	$+3
		incf	count,f		;increment display
		goto	M1			
		clrf	count
		incf	count,1
		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			
		decf	count,f		;decrement display
		goto	M1			;
		

		
show1	movlw	b'00000010'		;
		movwf	GPIO
		call	_20mS
		retlw	00
		
show2	movlw	b'00000100'		;	
		movwf	GPIO
		call	_20mS
		retlw	00
		
show3	movlw	b'00010000'		;	
		movwf	GPIO
		call	_20mS
		retlw	00
						
show4	movlw	b'00100000'		;
		movwf	GPIO
		call	_20mS
		retlw	00		
		
	
;****************************************************************
;* Main 							*
;****************************************************************

					

Main	btfsc	GPIO,0	
		goto	$+4 			;reset not pushed
		btfss	GPIO,3			;reset or down pushed
		goto	Down			;down pushed	
		goto	Cycle			;cycle 1-4 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
		

	
	