			DEVICE PIC16C84,XT_OSC,WDT_OFF,PROTECT_OFF

pc		equ	02h

cnt0		equ	0ch	;need for rolling cnounter (16)
digit_select	equ	0dh	;test

bin_high	equ	10h	;binary data high register
bin_low		equ	11h	;binary data low register

data		equ	15h
system		equ	16h

w0		equ	17h
w1		equ	18h

wbuff		equ	1bh
stat		equ	1ch

int_cnt0	equ	1dh
int_idozites	equ	1eh

toll		equ	1fh

bcd_100		equ	20h	;result
bcd_10		equ	21h	;result
bcd_1		equ	22h	;result


button		=	ra.1
rpm_in		=	ra.4

neg		=	system.0



	org 0
	goto start

;-------------------------------------------------------------------------------
;-------------------------------------------------------------------------------
;-------------------------------------------------------------------------------
	org     4
	movwf wbuff
	swapf status,0
	movwf stat

;-------------------------------------------------------------------------------
	decfsz int_cnt0,1	;/60=1, /30=2, /20=3, /15=4
	goto intveg
	movf int_idozites,0
	movwf int_cnt0
;	incf digit_select,1	;eredetileg
;---------------------------------------------

	call bin2bcd16
	clrf bin_low
	clrf bin_high
 
;--------------------- Button check -----------------
bchk	btfss button
	goto intveg

	incf toll,1
	call toll_tabla
	movwf int_idozites
;--------------------- Button check -----------------

;-------------------------------------------------------------------------------
intveg	incf digit_select,1
	movf digit_select,0
	andlw 3
	addwf pc
	goto d100
	goto d10
	goto d1
	bcf ra.0
	goto intveg2

d100	bcf ra.0
	bsf ra.3
	movf bcd_10,0
	andlw 0fh
	call display_w
	goto intveg2

d10	bcf ra.3
	bsf ra.2
	swapf bcd_1,0
	andlw 0fh
	call display_w
	goto intveg2

d1	bcf ra.2
	bsf ra.0
	movf bcd_1,0
	andlw 0fh
	call display_w
;	goto bchk

;intveg eredeti
intveg2	swapf stat,0
	movwf status
	swapf wbuff,1
	swapf wbuff,0
	bcf intcon,2
	retfie
;-------------------------------------------------------------------------------
;-------------------------------------------------------------------------------
;-------------------------------------------------------------------------------

start	clrf ra
	clrf rb
	movlw 00010010b
	tris ra
	movlw 00000000b
	tris rb	
	movlw 0
	movwf 0ah

	clrf digit_select

	movlw 2
	movwf toll
	call toll_tabla
	movwf int_idozites
	movwf int_cnt0

	clrf bin_low
	clrf bin_high
	call bin2bcd16

	movlw 00000100b
	option
	bsf t0ie
	bsf gie

main	nop
mc0	btfsc rpm_in
	goto mc0
mc1	btfss rpm_in
	goto mc1
	incf bin_low
	btfsc z
	incf bin_high

	goto mc0

;------------------------- "188" LED Display Driver -------------------------
display_w	call led_table
		movwf rb
		return
;------------------------- "188" LED Display Driver -------------------------

bin2bcd16	clrf bcd_1
		clrf bcd_10
		clrf bcd_100
		movlw 16
		movwf cnt0

c_b2b16		movf bcd_10,0
		andlw 11110000b
		sublw 040h
		btfsc c
		goto check10l

		movlw 030h		;pseudo1000
		addwf bcd_10,1

check10l	movf bcd_10,0
		andlw 00001111b
		sublw 4
		btfsc c
		goto check1h

		movlw 3			;pseudo100
		addwf bcd_10,1

check1h		movf bcd_1,0
		andlw 01110000b
		sublw 040h
		btfss c
		goto pseudo10

		btfsc bcd_1.7
		goto pseudo10

xxx		movf bcd_1,0
		andlw 00000111b
		sublw 4
		btfss c
		goto pseudo

		btfsc bcd_1.3
		goto pseudo

roll		clrb c
		rlf bin_low,1
		rlf bin_high,1
		rlf bcd_1,1
		rlf bcd_10,1
		rlf bcd_100,1
		decfsz cnt0,1
		goto c_b2b16
		return

pseudo10	movlw 030h
		addwf bcd_1,1
		goto xxx

pseudo		movlw 3
		addwf bcd_1,1
		goto roll

wait		movlw 1
		movwf w1
		goto wait_n
bigwait		clrf w1
wait_n		clrf w0
wait_c		nop
		decfsz w0,1
		goto wait_C
		decfsz w1,1
		goto wait_n
		return

;	org 0100h

led_table	addwf pc
		retlw 01000000b		;0
		retlw 01111001b
		retlw 00100100b
		retlw 00110000b
		retlw 00011001b
		retlw 00010010b
		retlw 00000010b
		retlw 01111000b
		retlw 00000000b
		retlw 00010000b		;9

toll_tabla	decf toll,0
		andlw 00000011b
		addwf pc
		retlw 60	;1
		retlw 30	;2
		retlw 20	;3
		retlw 15	;4


; | | | |
; g a f b
;
; | | | |
; c e d A