When I generate PWM on CCP1 only, I am able to get SPWM of 50HZ with 10kHz switching frequency. When I enable the PWM on CCP2 for another SPWM, the frequency of both SPWMs changes. But if I generate it on either 1 of the outputs, it works fine. I am trying to produce 2 SPWMs with 90 degrees phase shift, for sin and cos, both at 50Hz and 10kHz switching frequency.
void main(void)
{
ANSELD=0X00;
ANSELC=0X00;
PORTD = 0;
TRISD = 0;
TMR2 = 0;
PR2 = 199; // PWM period = (PR2+1) * prescaler * Tcy
CCPR1L = 0;
CCPR2L = 0;
TRISC = 0; //0b11111011; // Make pin 17 (RC1/CCP2) an output
T2CON = 0b00000100; // Enable TMR2 with prescaler = 1
CCP1CON = 0b00001100; // Enable PWM on CCP1
CCP2CON = 0b00001100;
PIR1.TMR2IF = 0;
T2CON.TMR2ON = 1;
j = i+50;
while(1)
{
PIR1.TMR2IF = 0;
while( PIR1.TMR2IF ==0);
CCPR1L = 0.99*sin_table[i];
CCPR2L = 0.99*sin_table[j];
i = i+2;
j = j+2;
if(i==100)
{
i=0;
}
if(j==100)
{
j=0;
}
}
}