Electronic – PIC PWM and the use of PostScaler

picpwm

I'm working on a project with an SG90 servomotor, that requires the use of PWM. However the motor works with PWM frequency of 50 Hz (20 ms period), and in my PIC16F690, when I try to calculate a value for the PR2 register (per page 129), I always get a value above 255, until I reduce the Fosc to less than 1 MHz, and also use a prescaler of 16.

So the question is: is possible to use post-scaler to achieve my desired results?

Thanks

Best Answer

No, unfortunately the PWM peripheral won't do what you want because it can't run that slow. Next best option is to use the Compare mode instead and use an interrupt service routine to handle the PWM duty.

Configure TIMER1 to run at, say, 1MHz by using the Fosc/4 clock source and the appropriate pre-scaler. Then set the PWM module to Compare mode and configure it to toggle the CCP1 output. Set the CCPR1 to the number of microseconds in your PWM "on" period. Eg. for 75% duty at 50Hz, set CCPR1 to 15000. Also enable the CCP1IF interrupt and write a interrupt service routine which writes the complement value to the CCPR1 register. Eg. for 75% duty at 50Hz, the CCPR1 should be changed from 15000 to 20000-15000 = 5000. Next time the interrupt is fired it'll do the opposite, setting the CCPR1 register back to 20000-5000 = 15000.

You'll need to handle cases close to 0% and 100% differently (eg. if <2% or >98% then disable the toggle and set the pin appropriately), but then should give you the PWM signal you need at the CCP1 pin.