Algorithm for a program to generate pwm signals using pic 16f877

Thread Starter


Joined Jan 8, 2010
hi.i am currently working on a project which converts a 50 hz frequency input into 800hz frequency output.the principle employed is to first convert the input ac to dc using a rectifier and then to use an inverter to convert this dc to the ac output at the required frequency. i am planning to implement the inverter using a mosfet bridge which is triggered by pwm signals supplied by means of pic 16f877 microcontroller. please help me with the pic programming. i would be very thankful if anyone could post the program or algorithm


Joined Apr 20, 2004
Why do the up conversion? Use some fixed oscillator to generate a frequency and divide it down to 800 Hz.

Thread Starter


Joined Jan 8, 2010
hi thanks for your reply.mine is an industrial project.the company requires 800hz for transformer testing purposes.please help me with the pic programming to generate pwm signals to trigger the mosfet.


Joined Jul 17, 2007
Well, if you take a 50Hz AC signal and run it through a full-wave bridge, the output will be DC with 100Hz ripple.

What are you trying to do; create a pseudo-sinewave output via PWM? Or are you trying to simply perform a push-pull conversion?


Joined Sep 7, 2009
You should be able to get 800Hz with a 4MHz clock.
Just use the formulas in section 8.3 of the datasheet, ie:
PWM period = [(PR2) + 1] • 4 • TOSC • (TMR2 prescale value)
With 4 MHz clock TOSC would be 0.00000025
and 800Hz is 0.00125
PR2 should be 77 (decimal) and TMR2 prescale should be 16
This gives 801.28 Hz
You will need to do a similar calculation for the duty cycle.


Joined Sep 10, 2007
The 16F877 has a hardware PWM pin.
You can just set it and your program can run at the same time.