# Using PWMOUT @ 20MHz

#### pjrebordao

##### Senior Member
Hi all

I'm using a 28X1 with an external 20Mhz oscillator.
I want to generate a PWMOUT output the closest to 16Khz.
However, the wizard doesn't support 20MHz and I can't understand the explanation given on the manual for calculating the period for PWMOUT...

Can anyone explain it in more understandable terms ? Thanks

Last edited:

#### eclectic

##### Moderator
Or use the PE.5 PICAXE wizard.

#### Attachments

• 68.2 KB Views: 18

#### reywas

##### Member
I am also having difficulty understanding the calculations as described in the BASIC commands manual. I need to be able to make these calculations "on-the-fly" within my program to adjust the PWM based on a varying input. A little help with making the calculations would be much appreciated. I'm using a resonator freq of 32MHZ and need the PWM to be 14KHZ.

#### reywas

##### Member
Well after giving it some thought, I realized all I need to do is calculate "duty" based on the value from a pulsin command, which is simple enough to do. I would still like to understand the formulas given in the manual though.

From the BASIC manual:
"The PWM period = (period + 1) x 4 x resonator speed (resonator speed for 4MHz = 1/4000000)
The PWM duty cycle = (duty) x resonator speed"

Given the knowns: PICAXE resonator = 32MHZ
required PWM freq = 14KHZ
required duty-cycle = 70%
How would the calculations be made?

#### AllyCat

##### Senior Member
Hi,

Yes, "speed" isn't a very good term to use and IMHO the description isn't very clear. The calculation can be done either with "frequency" or "period" values and here frequency is probably easiest:

First, simply divide the clock frequency by the required PWM frequency, i.e. 32,000,000 by 14,000 = 2285.7 cycles

But the maximum PWM count is 1023 so we need to use the "prescaler" to divide by 4 with PWMDIV4 in this example to give 571

Next, the PIC PWM instruction fits the period counter into a single byte (max 255) and also starts coding the value from zero, so we need to divide by 4 (again) and then subtract 1. This gives 142.8 - 1 = 141 , but in this case it would probably be better to round up to 142 .

Thus the PWM period would have 143 * 4 = 572 cycles and 70% becomes 400.4 . Again, normally you would subtract 1 to code as 399, but the Wizard appears to give 400 .

Cheers, Alan.

#### Rick100

##### Senior Member
Here's an interesting "feature" of the pic PWM module. If you use 255 for the period , it's not possible to get 100% duty cycle. It would require a value of (255 +1) * 4 or 1024 be put into the 10 bits of the duty cycle but 1024 requires 11 bits. Unless the firmware checks for this value greater than 10 bits and sets the output high. I loaded this program into a 20x2 and captured the results with a logic analyser.
Code:
``````[color=Blue]pwmout pwmdiv16[/color][color=Black], [/color][color=Blue]C.5[/color][color=Black], [/color][color=Navy]255[/color][color=Black], [/color][color=Navy]1023 [/color][color=Green]; 489Hz[/color]
[color=Blue]pause [/color][color=Navy]250[/color]
[color=Blue]pwmduty C.5[/color][color=Black],[/color][color=Navy]1024[/color]

[color=Black]here:[/color]
[color=Blue]goto [/color][color=Black]here[/color]`````` The screen capture shows the transition from a duty cycle of 1023, which is 2 us short of being a continuous high, to a duty cycle of 1024 which takes the output continuously low instead of high. I noticed this several years ago while writing a Qbasic program to convert the register values into frequency and duty cycle percents so I wouldn't have to use a calculator every time. A google search turned up this thread on the microchip forum.
http://www.microchip.com/forums/m390812.aspx

#### hippy

##### Technical Support
Staff member
I am also having difficulty understanding the calculations as described in the BASIC commands manual. I need to be able to make these calculations "on-the-fly" within my program to adjust the PWM based on a varying input.
That can get a little complicated because the maths requires more than 16-bits.

For calculating a period using a MHz operating speed -

Code:
``````          /    MHz * 250000    \
period = (  ------------------  ) - 1
\ frequency * pwmdiv /``````
Where MHz is the operating speed of the PICAXE ( usually 4 for M2, 8 for X2 ) and pwmdiv is 1, 4, 16 or 64.

And for calculating a period using a Hz operating speed -

Code:
``````          /          Hz            \
period = (  ----------------------  ) - 1
\ 4 * frequency * pwmdiv /``````
Where Hz is the operating speed of the PICAXE ( usually 4000000 for M2, 8000000 for X2 ) and pwmdiv is 1, 4, 16 or 64.

#### reywas

##### Member
Thanks for the help gents. I'm new to this stuff and trying to understand things as I go. I hope the OP also found the help he was looking for.