I'm working on an inverter with a dsPIC controller. I have the sine wave generation code running well, but now I want to add period skipping at particularly high or low duty cycles in order to improve efficiency. For those who don't specialize in power electronics, period skipping is a trick that involves alternating between periods of full on or full off and periods where the on time or off time is doubled, effectively halving the switching frequency.
Here's (part of) the code:
unsigned int pwm;
//code to calculate initial PWM value
if(pwm>600) {
if(PWMtick&0x1) { //PWMtick is a variable that increments every PWM period
pwm=(pwm<<1)-801; //this doesn't seem to work
if(pwm>775)
pwm=801; //801 commands 100% duty cycle
}
else
pwm=801;
}
else if(pwm<200) {
if(PWMtick&0x1) {
pwm=pwm<<1; //but this does!
if(pwm<25)
pwm=0;
}
else
pwm=0;
}
The result is that at low duty cycles, it does indeed skip periods as intended. But at high duty cycles, it just abruptly switches to full on. The code for the high and low duty cycles are identical except for some comparison constants and the PWM value calculation. Is there something peculiar about the dsPIC and/or the XC16 compiler that causes that calculation to fail?