
;Name: .asm
; By Edgardo Gonzalez

; This Program takes input from port 1 from a quadrant shaft encoder
; to increment or decrement a variable for use elsewhere. Here the variable


; *****************************Equates*******************************
echo	equ	p1.0	;Equates "echo" to address p3.0
init	equ	p1.1	;Equates "init" to address p3.1

; ***************************Main Program****************************

	org	00
	mov	TMOD,#01H	;Use both timers ... T0=16 Bit timer mode T1=13 Bit timer mode
	lcall	INTB		;Allows sensor to finish internal blanking

MAIN:	mov	R0,#10		;Sets how many times R0 is going to repeate
	clr	init
;	mov	P3,#0

LOOP:	mov	TH1,#00		;Sets the timer
	mov	TL1,#00		;;;;; to zero
	setb	TR1		;Starts the Timer 1
	setb	init		;Sends out the pulse
WAIT2:	jnb	echo,wait2	;Waits untill there is a return signal
	clr	TR1		;Stops Timer 1
	clr	init		;Stops sending the pulse
	clr	TF1		;Clears the overflow flag in Timer 1
	mov	R1,TH1
	mov	p1,r1
	ret

; *******************************Sub Routines************************

INTB:	mov	TH0,#-04H	;Set the timer for approximitly
	mov	TL0,#-0F8H
	setb	TR0		;Starts Timer 0
WAIT:	jnb	TF0,wait	;Waits till the overflow flag is set
	clr	TR0		;Stops Timer 0
	clr	TR0		;Clear overflow flag in Timer 0
	ret
