Reputation: 11
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;
}
}
}
Upvotes: 1
Views: 342
Reputation: 335
The issue is the shared use of timer 2. From the manual page 181), three actions happen once the timer is equal to the PRx register. They are:
Because TMRx (TMR2 in your case) is cleared, that resets the period of both outputs. There are other slight issues, such as you didn't specifically write to the "CCPTMRSx" register which assigns what timer to use, & I assume you deleted writing to PR1 to test just the 2nd output, but you need to write to both PR1 and PR2 to set the periods.
Solution should be to assign TMR2 to CCP1 and TMR4 to CCP2 if you want true independent PWMs
Upvotes: 2