
//*********External Interrupt [zero cross]***************
void ex1_isr(void) interrupt 0 using 3
{
	uchar ii, itestroot=0, ibackroot, iorer=0x80;
	uint itestsqr;

	SYSTIME=0;			//reset SYSTIME variable.
	EX0=0;				//disable interrupt ex0 to avoid immediate next intrpt.
	sysset=0;			//reset syset variable.

	if (onfire)			//if fire is enabled set fire counter 20 to get 10 pulses.
		firecount=20;

	cycles++;					//Increament the number of cycles variable		
	MSSUM=MSSUM/samples;		//divide squre sum by samples to get mean sqare.

	for(ii=0; ii<8; ii++)		//Square root calculation	
	{
		ibackroot=itestroot;
		itestroot|=iorer;		
		itestsqr=itestroot*itestroot;
		if(itestsqr>MSSUM)
			itestroot=ibackroot;
		iorer=_cror_(iorer,1);
	}

	if (ARMS)
		ARMS=(ARMS+itestroot)/2;	//Add it into previous value and divide by 2 
	else							//to get Average RMS.
		ARMS=itestroot;

	MSSUM=0;						//clear the square sum variable and samples.
	samples=0;						//clear the samples variable.
}

/*********Interrupt function for timer 1****************
This interrupt is always on and is 100us*/
void t1_isr(void) interrupt 3 using 2 
{
	SYSTIME++;		//increament the SYSTIME variable used for fire timing.
	if (SYSTIME>99 && !sysset )	//if systime=100 there is a zero cross since .
	{							//10ms elapsed past last EX0 interrupt.
		SYSTIME=0;			//Reset systime upon zero cross.
		sysset=1;			//Set sysset variable to take a not of this reset.
		if (onfire)			//If fire is enabled set pulse count as 20.	
			firecount=20;   //to give 10 pulses to thryistor.
	}

	if (onfire && SYSTIME>firedly && firecount)	//If firing is enable 
			//and required firing delay has elapsed and pulse down count is not 0.
	{
		FIREPORT=!FIREPORT;	//Togle Firing output pin
		firecount--;		//Reduce fire count variable by 1.
	}

	if (sysset && (SYSTIME>50)) 
		EX0=1;				//Enable external interrupt 0 to detect next low 
							//for zero cross detection
}
