PWMCalculations
The PWM module is a hardware based PWM generator, available on four separate pins. The following formula was used to calculate the PWM frequency. They were taken from the Microchip Family Reference Manual, chapter 13 - Output Compare Registers.

The following formula was used to calculate the PWM resolution:

Worked Example
Assuming a 32MHz clock, then
| Name | Value | Notes |
|---|---|---|
| OSC | 32 | |
| FOSC | 16,000,000 | 32 * 1,000,000 / 2 |
| TCY | 6.25ns | 1 / 16,000,000 |
| Timer Period (PRy) | 1023 | PRy = Timer2 |
| Timer Prescale | 64 | can be 1,8,64 or 256 |
gives
| Name | Value | Notes |
|---|---|---|
| PWM Period | 0.004096 | (1023 + 1) * TCY * 64 |
| PWM Frequency | 244 Hz | 1 / 0.004096 |
| PWM Resolution | 10 bit | Log(16,000,000 / (244 * 64)) / Log(2) |
Download an Excel spreadsheet for the above calculations from here.


