Thanks for the replies gents, for some reason I didn't get a notification to my email address.
As it turns out, it's not glitch free, have a look at these trends showing a missing pulse on the PWM output:
This is when the PR2 is modified. Not sure exactly why that is, but I'm guessing it's down to when the update happens and where the TMR2 is in counting up (it's a spurious thing). I've now modified it to do the following:
if(PR2 > 18)
{
TMR2IF = 0;
while (TMR2IF == 0) {}
PR2 = PR2 - 6;
}
and that seems to do the trick. The only issue is waiting for the PWM cycle to finish, which can add even around 1000us to the overall loop length. I have this running in an INT routine which is triggered every 100ms and I'm not so crazy about having to do the while (TMR2IF == 0) {} in the interrupt. Then again I guess 1ms is not a massive amount of time and the PIC is not really busy with anything else at that point (PWM ramp only takes 1sec total).
Regards,
dsc.
EDIT: having thought about this it makes sense to miss a pulse if the following happens:
- you want to change PR2 from 77 to 70
- TMR2 is counting up, goes past 70, say into 71
- you modify the PR2 to be 70
- TMR2 cannot match the new PR2 in this PWM cycle, so it will count up to 255, overflow, starting counting up again from 0 and then match PR2
- the above will effectively make it miss a pulse
The strange thing is that I've seen this even when modifying PR2 by 1 and I'd say that chances of changing the PR2 exactly when it passes through the new PR2-1 value are rather slim to impossible.