How to make pwm in pic16f877a?


Joined Aug 23, 2012
i have done it is working what is theory behind it
The normal charge path is R1(1K) → R2(5K) → C1(1n), and discharge path is C1(1n) → R2(5K), the R2(5K) is the common path and all the same value whatever during charge or discharge.

When it change to pwm that it just used two diodes to separated the R2 to two parts, the charge time and discharge time is different.
The charge time :
R1(1K) → D1 (left one, up is(+), down is (-)) → left side of R2(5K) → C1(1n)

The discharge time :
C1(1n) → D2 (right one, up is(-), down is (+)) → right side of R2(5K)

When you adjust R2 then The charge time and The discharge time will be different, that's how it became a pwm circuit.

and how to build sawtoth wave and triangular waveform..
Please read the page 143, 144 carefully in the links of your first post, it has comparator and it can be a D/A converter, so you can using it to generate the wave you want.

If you do not satisfy the D/A resolution of above method then you can add a 8 bits dac as dac0800, and output 8 bits data from I/O port to dca0800 to generate the wave you want.

For further improvement then you can add a op amp and capacitor to make the waveform more smooth.