; This module services the real time interrupt

;

; Get a Real Time interrupt every One millisecond (using SOF interrupt)

;

; Pulse the stepper motor every 10msec (100Hz) if required

;	EZ-USB PortA drives an Allegro 8219

;	Phase A is connected to bits 5,4 & 3, Phase B to bits 2,1 & 0

;	Bits 5 and 2 are PHASE

;	Bits 4:3 and 1:0 are I1:I0 current inputs

;

ServiceTimerRoutine:

	DJNZ    Msec_counter, Done      ; Only need to step every 10msec

	MOV     Msec_counter, #10	; Reinitialize



	MOV	R0, #MotorControl+1	; Point to LS byte of count

	MOV	R1, #MotorControl+2	; Point to MS byte of count

	MOV	A, @R0

	ORL	A, @R1

	JZ	Done			; No stepping required

StepTheMotor:

	DEC	@R0			; Unfortunately, no flags are set

	MOV	A, @R0

	CPL	A			; Test for LS = FF

	JZ	STM1

	DEC	@R1			; Underflow on LS, therefore adjust MS byte

STM1:	MOV	A, MotorControl		; Get the direction indicator

	JNZ	Clockwise

	MOV	A, #-1			; CounterClockwise = backup one

Clockwise:

	ADD	A, CurrentPosition

	ANL	A, #0FH			; Table is only 16 entries long

	MOV	CurrentPosition, A

	MOV	DPTR, #StepperTable

	MOVC	A, @A + DPTR		; Index into table 

	MOV	R0, #Low(PortA_Pins)

	MOVX	@R0, A			; Select the new step position

Done:	RET



StepperTable:

	DB 000111b	;  0 = Up

	DB 000010b	;  1

	DB 001001b	;  2

	DB 010000b	;  3

	DB 011000b	;  4 = Right

	DB 110000b	;  5

	DB 101001b	;  6

	DB 100010b	;  7

	DB 100011b	;  8 = Down

	DB 100110b	;  9

	DB 101101b	; 10

	DB 110100b	; 11

	DB 111100b	; 12 = Left

	DB 010100b	; 13

	DB 001101b	; 14

	DB 000110b	; 15