	LIST	p=16F876A		;tell assembler what chip we are using
	include "P16F876A.inc"		;include the defaults for the chip
	__config 0x3D18			;sets the configuration settings 
					;(oscillator type etc.)

	cblock 	0x20
		currentgear
		oldgear
		count1 			;used in delay routine
		counta 			;used in delay routine
		countb 			;used in delay routine
	endc

DISPPORT	Equ	PORTA			;set constant LEDPORT = 'PORTA'
DISPTRIS	Equ	TRISA			;set constant for TRIS register
GEARPORT	Equ PORTB
GEARTRIS	Equ TRISB
SHIFTPORT	Equ PORTC
SHIFTTRIS	Equ TRISC
GEAR1	Equ	0
GEAR2	Equ 1
GEAR3	Equ	2
GEAR4	Equ	3
GEAR5	Equ	4
GEAR6	Equ	5
GEAR7	Equ	6
GEAR8	Equ	7

	;org	0x0004			;org sets the origin, 0x0000 for the 16F628,
	org	0x0000
					;this is where the program starts running	
	movlw	0x07
	movwf	CMCON			;turn comparators off (make it like a 16F84)

   	bsf 	STATUS,		RP0	;select bank 1
   	movlw 	b'11111111'		;set PortB all inputs
   	movwf 	TRISB
	movlw 	b'11110000'
	movwf	TRISA
	movlw	b'00000000'
	movwf	TRISC
	bcf	OPTION_REG,NOT_RBPU	;enable PORTB pull-up resistors
	bcf	STATUS,		RP0	;select bank 0

	movlw d'1'
	movwf currentgear
	movlw d'5'
	movwf oldgear
	

Loop	call GearDisplay
	btfss	GEARPORT,	GEAR1
	call	Gear1
	btfss	GEARPORT,	GEAR2
	call	Gear2
	btfss	GEARPORT,	GEAR3
	call	Gear3
	btfss	GEARPORT,	GEAR4
	call	Gear4
	btfss	GEARPORT,	GEAR5
	call	Gear5
	btfss	GEARPORT,	GEAR6
	call	Gear6
	btfss	GEARPORT,	GEAR7
	call	Gear7
	btfss	GEARPORT,	GEAR8
	call	Gear8
	
	call ChangeGear

	goto Loop

ChangeGear
	movf	currentgear,W
	subwf	oldgear,W
	btfss	STATUS,C
	call GearUp		; not a number, get back to main loop

	movf	oldgear,W
	subwf	currentgear,W
	btfss	STATUS,C
	call GearDown		; not a number, get back to main loop
	retlw 0x00

GearUp
	incf oldgear,F
	bsf SHIFTPORT , 0
	call Delay
	bcf SHIFTPORT , 0
	call Delay
	goto Loop

GearDown
	decf oldgear,F
	bsf SHIFTPORT , 1
	call Delay
	bcf SHIFTPORT , 1
	call Delay
	goto Loop

GearDisplay
	movf currentgear,W
	movwf DISPPORT
	retlw 0x00

Gear1
	movlw d'1'
	movwf currentgear
	retlw 0x00

Gear2
	movlw d'2'
	movwf currentgear
	retlw 0x00

Gear3
	movlw d'3'
	movwf currentgear
	retlw 0x00

Gear4
	movlw d'4'
	movwf currentgear
	retlw 0x00

Gear5
	movlw d'5'
	movwf currentgear
	retlw 0x00

Gear6
	movlw d'6'
	movwf currentgear
	retlw 0x00

Gear7
	movlw d'7'
	movwf currentgear
	retlw 0x00

Gear8
	movlw d'8'
	movwf currentgear
	retlw 0x00

Delay	movlw	d'250'			;delay 250 ms (4 MHz clock)
	movwf	count1
d1	movlw	0xC7
	movwf	counta
	movlw	0x01
	movwf	countb
Delay_0
	decfsz	counta, f
	goto	$+2
	decfsz	countb, f
	goto	Delay_0

	decfsz	count1	,f
	goto	d1
	retlw	0x00

	end