#define tx GPIO.GP5
unsigned TMR1 absolute 0x0E;
char current_dim = 0;
char done_flag = 0;
unsigned preload_time[10] = {65535,63823,63082,62481,61925,61370,60814,60259,59656,58916};
//******************************************************************************
//*** interrupt-on-change when zero-crossing occur, pre-load timer1 value
//*** turn on timer1 to perform delay before gate trigger
//*****************************************************
if(INTCON.GPIF){
tx = 1;
TMR1 = preload_time[current_dim]; //preload_time into Timer1
T1CON.TMR1ON = 1; //start timer1
INTCON.GPIF = 0;
}
//******************************************************************************
//*** perform gate trigger, turn off itself
//******************************************************************************
if(PIR1.TMR1IF){
T1CON.TMR1ON = 0; //stop timer1
tx = 0; //turn on triac
delay_us(150);
tx = 1; //turn off triac
PIR1.TMR1IF = 0; //clear interrupt flag
}
}