# Help needed with PWM Duty Cycle formula

#### spinnaker

Joined Oct 29, 2009
7,837
My datasheet has the following formula for the CCP peripheral in PWM mode.

PWM Duty Cycle = (CCPR4L:CCP4CON<5:4>) •
TOSC • (TMR2 Prescale Value)

What is TOSC exactly? Does this reference my main OSC? Or is it the timer?

My main OSC is running at 8MHZ.
The value I set PR2 to is 255.
I am using a prescaler of 1 (T2CKPS is set to 0).

What values are entered here?

Speaking of PR2, I see it referenced but I am not seeing where it is defined in the datasheet. My chip is the PIC18F47J53.

How do I determine optimum values of PR2 and the prescaler?

Joined Jul 18, 2013
19,485
There is quite a bit of detail in the manual, section 18.
The equations are in there 18-1 and 18-3 etc.
I would not expect that an analogue meter will require that high a PWM frequency.
I tend to keep the freq. as low as possible for the application.
For example, for motors Microchip suggest keeping the PWM as low as possible without causing any perceptible noise, which usually occurs less than 5khz.
Max.

#### spinnaker

Joined Oct 29, 2009
7,837
There is quite a bit of detail in the manual, section 18.
The equations are in there 18-1 and 18-3 etc.
I would not expect that an analogue meter will require that high a PWM frequency.
I tend to keep the freq. as low as possible for the application.
For example, for motors Microchip suggest keeping the PWM as low as possible without causing any perceptible noise, which usually occurs less than 5khz.
Max.

I am not seeing it. Or just maybe not interpreting it correctly. I am still at a lose of the meaning of TOSC in the formula. Is that the period of my main osc?

Wouldn't the have of PR2 come into play?

Joined Jul 18, 2013
19,485
FOSC is your system clock.
The 18F I use is the 18F23K22 and the manual has quite a detailed description of the needs and formulae , see page 186-188 for e.g.
Max.

#### spinnaker

Joined Oct 29, 2009
7,837
FOSC is your system clock.
The 18F I use is the 18F23K22 and the manual has quite a detailed description of the needs and formulae , see page 186-188 for e.g.
Max.

Yes I know FOSC is the system clock. So TOSC is the period?

I looked at the datasheet for the 18f23k22. Now it brings up even more of a mystery.

My datasheet says duty cycle is calculated as such

PWM Duty Cycle = (CCPR4L:CCP4CON<5:4>) •
TOSC • (TMR2 Prescale Value)

Your datasheet says that is the formula for pulse width which makes a lot more sense.

It goes on to say the duty cycle ratio is

That makes more sense. It looks like my datasheet is wrong?

I am not seeing the PRx (or PR2) register documented anywhere in either datasheet. Is the value placed in the formula the same as the value put into PRx register?

#### shteii01

Joined Feb 19, 2010
4,647
From Microchip website: Fosc=1/(Tosc). So. Yes. Tosc is period.
8 MHz=1/Tosc
Tosc=1/8000000=0.125E-6=125E-9=125 nanoseconds

Last edited:

#### spinnaker

Joined Oct 29, 2009
7,837
I put together this spreadsheet to how PWN Duty Cycles.

#### Attachments

• 29.3 KB Views: 3

#### shteii01

Joined Feb 19, 2010
4,647
Ah... I made a small mistake... 10^-9 is nano. Sorry!

#### spinnaker

Joined Oct 29, 2009
7,837
According to my findings the formula is actually (CCPR4L:CCP4CON<5:4>/PRx)*100

What is the deal with the formula in the datasheet?

#### Travm

Joined Aug 16, 2016
282
I have seen errors on microchip datasheets before. I'm not going to try to figure out if that's your case because I'm pretty new to pics and would likely just be wrong. But if you haven't seen it before try this page. It's extremely well done.
http://microchip.wikidot.com/8bit:ccppwm
There is so much practical information and further explanation on what the datasheet is actually saying. Made pic pwms make sense for me, at least for the chips I'm presently learning.