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

NameValueNotes
OSC32 
FOSC16,000,00032 * 1,000,000 / 2
TCY6.25ns1 / 16,000,000
Timer Period (PRy)1023PRy = Timer2
Timer Prescale64can be 1,8,64 or 256

gives

NameValueNotes
PWM Period0.004096(1023 + 1) * TCY * 64
PWM Frequency244 Hz1 / 0.004096
PWM Resolution10 bitLog(16,000,000 / (244 * 64)) / Log(2)

Download an Excel spreadsheet for the above calculations from here.