; This module services the real time interrupt

;

; Get a Real Time interrupt every 40 microseconds (from Timer 0)

;

; This routine generates a 40usec pulse which turns on a Triac (via an opto-isolator)

; The 10msec LINE half cycle is divided into 250 40usec 'slots'

; The position of the TurnON pulse will vary the phase angle of the power control

;

; The PC Host provides 6 "brightness" values, these are counted down each half-cycle

;

; The Triacs are on Port A bits [5:0]

; A Line zero-cross detector is on Port C bit 0

; 

ServiceTimerRoutine:

	MOV	DPTR, #PortC_Pins	; First check if we have just changes cycles

	MOVX	A, @DPTR

	MOV	DPTR, #PortA_OUT	; Will need this later

	MOV	C, LastCycle

	JNB	ACC.0, PositiveCycle    ; Need an XRL C, but we don't have one!

	CPL	C

PositiveCycle:

	JC	SameCycle

CycleChange:

	MOV	C, ACC.0		; Retreive current cycle

	MOV	LastCycle, C		; Save it for next time

	MOV	A, #10000000b		; Ensure all TurnON signals are low

	MOVX	@DPTR, A		; Trigger the 'scope on Bit 7

	MOV	R0, #LightValues

	MOV	R1, #WorkingValues	

	MOV	R7, #6

CCLoop:	MOV	A, @R0			; Get the Light Values

	CPL	A			; Since we count down

	MOV	@R1, A			; Update the Working Values

	INC	R0

	INC	R1

	DJNZ	R7, CCLoop		; Copy all six values

SameCycle:				; Check to see if any counters have expired

	MOV	R0, #WorkingValues

	MOV	Mask, #0		; Allow for XCH

	MOV	A, #00100000b		; Turn on signal for a Triac

NextTriac:

	XCH	A, Mask			; Accumulate TurnON signals in A

	MOV	Temp, @R0

	DJNZ	Temp, KeepOFF

	ORL	A, Mask			; Set a TurnON bit

KeepOFF:MOV	@R0, Temp

	INC	R0			; Ready for next loop

	XCH	A, Mask

	RR	A			; Rotate Mask pattern

        JNZ	NextTriac

	MOV	A, Mask			; Retrieve TurnON pattern

	MOVX	@DPTR, A		; TurnON triac(s) if required

	RET