swapan
Full Member level 4
I think TBL_POINTER_OLD and TBL_POINTER_NEW should be reset after every 10mS i.e. on overflow of 16 bit register like this.
Code:
void interrupt() {
if((TMR2IE_bit) && (TMR2IF_bit)) {
//Enter your code here
//SET_FREQ = 65536 / (64 * 2) = 512
//Required AC Frequency = 50 Hz
//PWM Freq = 12821 Hz
//Tpwm = 1/12.821 KHz = 78 us (Timer2 Interrupt Period)
//78 us * 64 * 2 = 9984 = 9.984 ms (for 180 degrees)
//9.984 * 2 = 19.968 ms
//AC freq = 1/19.968 ms = 50.08 Hz
TBL_POINTER_NEW = TBL_POINTER_OLD + SET_FREQ;
if(TBL_POINTER_NEW < TBL_POINTER_OLD) {
CCP1CON.P1M1 = ~CCP1CON.P1M1; //Reverse direction of full-bridge
[COLOR="#FF0000"]TBL_POINTER_NEW = 0;
TBL_POINTER_OLD = 0;[/COLOR]
}
TBL_POINTER_SHIFT = TBL_POINTER_NEW >> 10;
DUTY_CYCLE = TBL_POINTER_SHIFT + adder;
//Assign PWM duty 10 bit
CCPR1L = (sine_table[DUTY_CYCLE] & 0x3FC) >> 2;
CCP1CON.DC1B1 = (sine_table[DUTY_CYCLE] & 0x02) >> 1;
CCP1CON.DC1B0 = (sine_table[DUTY_CYCLE] & 0x01);
TBL_POINTER_OLD = TBL_POINTER_NEW;
TMR2IF_bit = 0;