; Bike speedometer - Phil_G Sept 1991
; add set-0 on no wheel pulse
; longer advert delay
; hundreds of miles (blank if 0)
;
	pl 58
	page
;
;	Bike speedo for 16x2 display & 8748.
;	Wheel pulse via hall-effect switch to INT.
;
;	R0 = ptr               R0' = ptr during ints
;	R1 = ptr               R1' = ptr during ints
;	R2 =                   R2' = ticks per second count
;	R3 = gp counter        R3' = ticks per rev
;	R4 = string pointer    R4' = 
;	R5 =                   R5' = temp work reg
;	R6 = scratch           R6' = 
;	R7 = delays, scratch   R7' = acc save during int
;
;	RAM variables:
;
HOURS	EQU 32	; 2xBCD byte
MINS	EQU 33	; 2xBCD byte
SECS	EQU 34	; 2xBCD byte
TICKS	EQU 35	; byte, 'live' tick count
SPEED	EQU 36	; 2xBCD byte
DIST1	EQU 37	; nibble, msn dist
DIST2	EQU 38	; 2xBCD lsb dist
MAX	EQU 39	; 2xBCD byte
REV10	EQU 40	; byte, wheel revs
;	constants:
RPTVAL	EQU 75	; revs per tenth-mile
;
	JMP	BEGIN
;
	ORG	3
	JMP	INT	; wheel pulse
	ORG	7	; timer
	JMP	TIMER
;
BEGIN:	CALL	INITD	; init display
;
PROG:	MOV	A,80h	; goto top line
	CALL	CURPOS
	MOV	R4,# < ADVERT1-1
	CALL	STROUT	; print top line text
	MOV	A,0C0h	; goto bottom line
	CALL	CURPOS
	MOV	R4,# < ADVERT2-1
	CALL	STROUT	; print bottom line
	MOV	R4,40
ADWAIT:	CALL	DELAY
	DJNZ	R4,ADWAIT
	SEL	RB1
	MOV	R2,125	; init ticks per sec
	SEL	RB0
; clear ram variables
	MOV	R0,#HOURS
	MOV	R3,8
	MOV	A,0
CLEAR:	MOV	@R0,A
	INC	R0
	DJNZ	R3,CLEAR
	MOV	A,RPTVAL
	MOV	@R0,A
;
	MOV	A,#-100	; 125 per second
	MOV	T,A
	EN	TCNTI	; enable timer ints
	STRT	T	; start timer
	EN	I
;
LOOP:	MOV	A,80h	; goto top line
	CALL	CURPOS
	MOV	R0,#SPEED
	MOV	A,@R0
	CALL	B2HEX	; say speed
	INC	R0	; to DIST1
	MOV	R4,# < XTOP-1
	CALL	STROUT	; print top line text
	MOV	A,@R0	; say msn dist
	CALL	DHEX10	;
	INC	R0	; to DIST2
	MOV	A,@R0
	SWAP	A
	CALL	DHEX10	; say 2nd digit
	MOV	A,"."
	CALL	SEND	; say "point"
	MOV	A,@R0
	CALL	DHEX10	; say 3rd digit
	INC	R0	; to MAX
	MOV	A,0C0h	; goto bottom line
	CALL	CURPOS
	MOV	A,@R0
	CALL	B2HEX	; say max
;
	MOV	R4,# < XBOT-1
	CALL	STROUT
;
	MOV	A,0C8h	; goto bottom rhs
	CALL	CURPOS
	MOV	R0,#HOURS
	MOV	A,@R0
	INC	R0
	CALL	B2HEX	; do hours
	MOV	A,":"
	CALL	SEND
	MOV	A,@R0
	INC	R0
	CALL	B2HEX	; do mins
	MOV	A,":"
	CALL	SEND
	MOV	A,@R0
	CALL	B2HEX	; do secs
	JMP	LOOP
;
; send ASCIIZ string at R4 (this page) to display
;
STROUT:	MOV	A,R4
	INC	A
	MOV	R4,A
	MOVP	A,@A
	JZ	STREND
	OUTL	P1,A
	CALL	PULSE
	JMP	STROUT
STREND:	RET
;
XTOP:	DB " MPH  DIS ",0
XBOT:	DB " TOP  ",0
ADVERT1:DB "Bike Speedo  (c)",0
ADVERT2:DB "Phil Green, 1991",0
;
	ORG 100h
;
TIMER:	SEL	RB1	; come here 125 times/sec
	MOV	R7,A	; save acc
	MOV	A,#-100	; 125 per second
	MOV	T,A
	MOV	R0,#TICKS
	MOV	A,@R0	; get tick count
	ADD	A,1	; increment @ 125/sec
	JZ	VSLOW	; but stop at 255
	MOV	@R0,A
VSLOW:	MOV	A,R2	; dec count
	ADD	A,-1
	MOV	R2,A
	JNZ	TIM2
	MOV	R2,125	; reset count
;
; inc clock once per second...
;
	MOV	R5,2	; secs & mins
	MOV	R0,#SECS
CLOCK:	MOV	A,@R0	; get secs
	ADD	A,1	; add one
	DA	A	; cvt to bcd decimal
	MOV	@R0,A	; store
	XRL	A,60h	; minute/hour up?
	JNZ	TIM2	; jmp if no sec/min carry
	MOV	A,00	; zero sec on inc min
	MOV	@R0,A	; store
	DEC	R0	; to MM
	DJNZ	R5,CLOCK; sec was 60 so do mins 
	MOV	A,@R0	; get hours
	ADD	A,1	; add one
	DA	A	; cvt to bcd decimal
	MOV	@R0,A	; store
	XRL	A,24h	; 24 hours?
	JNZ	TIM2	; jmp if no secs carry
	MOV	A,00	; zero if 24
	MOV	@R0,A	; store
TIM2:	MOV	A,R7	; restore acc
	RETR		; restore RB0 & ret
;
B2HEX:	MOV	R6,A		; send a byte as 2 ascii chars
	RR	A
	RR	A
	RR	A
	RR	A
	CALL	DHEX10		; do msn
	MOV	A,R6		; restore byte
DHEX10:	ANL	A,#00FH		; do lsn
	ADD	A,#090H
	DA	A
	ADDC	A,#040H
	DA	A
SEND:	ORL	P2,4	; set data mode
	OUTL	P1,A
PULSE:	MOV	R7,32
	DJNZ	R7,$	; delay 160uS
	ORL	P2,8	; set E
	ANL	P2,0F7h	; reset E
	MOV	R7,32
	DJNZ	R7,$	; delay 160uS
	RET	
;
; 50 ms delay
DELAY:	MOV	R7,40	; 2 x 2.5 =          5
	MOV	R6,0	; 2 x 2.5 =          5
DEL1:	DJNZ	R6,$	; 256 x 2.5 x 2 = 1280
	DJNZ	R7,DEL1	; 40 x 1280
	RET
;
CURPOS:	MOV	R4,A	; save A
	MOV	A,0	; control port map
	OUTL	P2,A	; cmd mode, enable low
	MOV	A,R4	; goto XY
	OUTL	P1,A
	CALL	PULSE	; do this cmd
	ORL	P2,4	; set data mode	
	RET
;
INITD:	MOV	A,0	; init display
	OUTL	P1,A
	OUTL	P2,A	; cmd mode, enable low
	CALL	DELAY
	MOV	R4,# < INIT-1
SET1:	MOV	A,R4
	INC	A
	MOV	R4,A
	MOVP	A,@A
	JZ	INIRET
	OUTL	P1,A
	CALL	PULSE
	CALL	DELAY
	JMP	SET1
INIRET:	RET
;
; Set up display mode, number of lines, cursor, etc
INIT:	DB 38h,38h,06h,0Ch,01h,00h
;
INT:	SEL	RB1	; come here on wheel pulse
	MOV	R7,A	; save acc
	MOV	R1,REV10
	MOV	A,@R1	; get rev count
	ADD	A,-1
	MOV	@R1,A
	JNZ	NOT10
	MOV	A,RPTVAL; reset count
	MOV	@R1,A
	MOV	R1,DIST2
	MOV	A,@R1
	ADD	A,1
	DA	A
	MOV	@R1,A
	JNC	NOT10
	DEC	R1
	MOV	A,@R1
	ADD	A,1
	DA	A
	MOV	@R1,A
;
NOT10:	MOV	R1,TICKS
	MOV	A,@R1	; get ticks
	MOV	R4,A	; save a mo
	MOV	A,0	; zero ticks &
	MOV	@R1,A	; start timing again
	MOV	A,R4	; back again
	MOVP3	A,@A	; convert to mph (bcd)
	MOV	R1,SPEED
	MOV	@R1,A	; store BCD speed
	MOV	R4,A	; speed as now
	MOV	R1,MAX	; last hi score
	MOV	A,@R1	; higher?
	CPL	A
	ADD	A,R4	; SUB A,R4
	CPL	A
	JNC	PEND
	MOV	A,R4	; new hi score
	MOV	@R1,A
PEND:	JNI	$	; await pulse end (INT=1)
	MOV	A,R7	; restore acc
	RETR
;
	ORG 300h	; division table. 625/ticks = speed (bcd)
;
	DB 00h,95h,95h,95h,92h,90h,95h,84h,74h,66h,59h,54h,49h,46h,43h,40h
	DB 38h,35h,33h,32h,30h,29h,28h,27h,26h,25h,24h,23h,22h,21h,21h,21h
	DB 19h,19h,18h,18h,17h,17h,17h,16h,16h,15h,15h,15h,14h,14h,14h,14h
	DB 13h,13h,13h,13h,12h,12h,12h,12h,11h,11h,11h,11h,11h,11h,10h,10h
	DB 10h,10h,10h,10h,10h,09h,09h,09h,09h,09h,09h,09h,09h,09h,08h,08h
	DB 08h,08h,08h,08h,08h,08h,08h,08h,08h,07h,07h,07h,07h,07h,07h,07h
	DB 07h,07h,07h,07h,07h,07h,07h,07h,06h,06h,06h,06h,06h,06h,06h,06h
	DB 06h,06h,06h,06h,06h,06h,06h,06h,06h,06h,06h,06h,06h,05h,05h,05h
	DB 05h,05h,05h,05h,05h,05h,05h,05h,05h,05h,05h,05h,05h,05h,05h,05h
	DB 05h,05h,05h,05h,05h,05h,05h,05h,05h,05h,05h,05h,04h,04h,04h,04h
	DB 04h,04h,04h,04h,04h,04h,04h,04h,04h,04h,04h,04h,04h,04h,04h,04h
	DB 04h,04h,04h,04h,04h,04h,04h,04h,04h,04h,04h,04h,04h,04h,04h,04h
	DB 04h,04h,04h,04h,04h,04h,04h,04h,04h,04h,04h,04h,04h,04h,04h,04h
	DB 03h,03h,03h,03h,03h,03h,03h,03h,03h,03h,03h,03h,03h,03h,03h,03h
	DB 03h,03h,03h,03h,03h,03h,03h,03h,03h,03h,03h,03h,03h,03h,03h,03h
	DB 02h,02h,02h,02h,02h,02h,02h,02h,01h,01h,01h,01h,01h,01h,01h,00h
;
	END
