PWMOUT Wizard Bug ??

zorgloub

Member
Greetings to all.

I am trying to understand the values of the parameters to be introduced in the PwmOut command.

In view of the formulas, the frequency of the MCU obviously influences these values.

While testing the PwmOut Wizard, I am surprised to get the same values at 16Mhz as for 4Mhz and also at 8Mhz as 32Mhz!
How is it possible ?
Bug in the wizard ?

PWMOUT Wizard
Picaxe : 08M2
Freq. : 10000Hz
Duty : 35%

Résults
Clock:
4Mhz PwmOut C.2, 99,139
8Mhz PwmOut C.2, 199,279
16Mhz PwmOut C.2,pwmdiv4, 99,139 same that 4Mhz !?
32Mhz PwmOut C.2,pwmdiv4,199,279 same that 8Mhz !?
----------------------

Oops, I missed the frequency divider introduced in the control!

Corrolar question:
When should you use this divider?
Regards
 
Last edited:

Buzby

Senior Member
That's not what I get when I use the wizard !

pwmout C.2, 99, 139 ; 10000Hz at 35% @ 4MHz

pwmout pwmdiv4, C.2, 99, 139 ; 10000Hz at 35% @ 16MHz

Same numbers, but 16MHz uses the divider, pwmdiv4, so I would expect the numbers to be the same, because the 'clock' is now divided by 4.

Cheers,

Buzby
 

zorgloub

Member
Hi Buzby

« Answer : When the Wizard tells you to ! »
I think you didn't understand the meaning of my question.
It is not my intention to blindly and stupidly follow the answers of the pwmout wizard but rather to understand the calculation method to get there.
The Wizard is only there to confirm the results of my calculations.
 

AllyCat

Senior Member
Hi,

The maximum division ratio of the PWM modulator is 1024 (i.e. 4 * (255 + 1) ) so if the resulting frequency is too high (i.e. the period too short) then you need to use one of the "prescaler" dividers of 4 , 16 or 64. Most of the details for the PWM hardware limits and calculation are in the Base PIC data sheet, for example section 24.3.4 for most M2 chips.

Cheers, Alan.
 

inglewoodpete

Senior Member
Corrolar question:
When should you use this divider?
Regards
Add the divider when you need to use low frequencies for your PWM.

Example: On a recent project for a client, my business installed custom RGBW lighting in the garden shelter of a residential development. I used a couple of PICAXE 28X2s to drive the LED lighting (4 x 10W outdoor luminaires). The long cables, up to 25 metres in length, are quite reactive to higher frequency PWM and the relatively heavy 'on' currents (700mA).

To counter the effect of the long cables, I run the 28X2s at 2MHz and PWM with the pwmdiv416 parameter. Unfortunately pwmdiv1664 is not available on the 28X2. This results in a PWM frequency of around 487Hz, low enough to minimise electrical losses in this situation.

Edit: Corrected my parameter values
 
Last edited:

zorgloub

Member
Hey guys
Thank you for the explanations.
For my part, I continued my research on the formulas to be applied and I placed them in an ecel file (see attached).
What do you think ?
Have a good day.
 

Attachments

inglewoodpete

Senior Member
Hey guys
Thank you for the explanations.
For my part, I continued my research on the formulas to be applied and I placed them in an ecel file (see attached).
What do you think ?
Have a good day.
Hmm. I downloaded the spreadsheet and tried to input the parameters that I used in the project I described, above. It would not accept my PICAXE processor speed (2MHz). End of test!:(
 

zorgloub

Member
Hi Inglewoodpete,
I completed the file with more possibilities for Clock frequencies and a control of the min and max frequencies of the cycle period.
You can try again with your data.

You said: « Unfortunately pwmdiv16 is not available on the 28X2.»
Are you sure, I don't think so?
Look also this link: https://picaxeforum.co.uk/threads/using-pwmdiv-options-with-the-picaxe-28x2-and-40x2.21867/
For Code required to force each 'pwmout' channel to a specific 'pwmdiv' setting .

Hi lbenson,
The goal was to make a calculation file available to the PICAXE community to understand the calculation of PwmOut values.
It could be further improved by adding a macro which would determine the best coeficient of the prescaler.
Print screens would therefore be unnecessary.

Cordially
 

Attachments

Last edited:

inglewoodpete

Senior Member
Hi Inglewoodpete,
I completed the file with more possibilities for Clock frequencies and a control of the min and max frequencies of the cycle period.
You can try again with your data.

You said: « Unfortunately pwmdiv16 is not available on the 28X2.»
Are you sure, I don't think so?
Look also this link: https://picaxeforum.co.uk/threads/using-pwmdiv-options-with-the-picaxe-28x2-and-40x2.21867/
For Code required to force each 'pwmout' channel to a specific 'pwmdiv' setting .
Sorry, yes you're right - I actually use pwmdiv16 in my recent project! It's pwmdiv64 that is not available on the 28X2. The limitation is the range of prescalers available in the internal clock module that drives the PWM.

I'll edit my earlier post.
 
Last edited:

zorgloub

Member
Hi,
In the Picaxe instruction book, for PwmOut, the following formula is indicated: Period PWM = (Period + 1) x 4 x Clock.
(With clock at 4Mhz, use 1/4000000)
This formula does not seem correct to me and does not corroborate my Excel calculations which were verified with the PwmOut Wizard.
In addition, this formula in the book does not consider the requested signal frequency or the prescaler parameter !!?
 

AllyCat

Senior Member
Hi,

The term "Clock" is vague; in this case it does NOT mean "Clock Frequency" but "Clock Period", as the qualification in braces () indicates: i.e. Period = 1 / Frequency. In the Command Reference it calls it "Resonator Speed", which is an improvement on the original User Manual. Similarly, as the PWMDIVxx parameter is "optional", it is explained as a "Qualification", rather than in the basic formula itself. To a novice, explaining that writing "nothing" (i.e. no PWMDIV entry) actually means "multiply by 1", might not be very helpful.

The problem is keeping the description a "KISS" (Keep It SimpleS) for novices, when the complete function is NOT simple! That's why the Wizard exists! For full details, the "base PIC" data sheet is the (only) place to go.

Cheers, Alan.
 

zorgloub

Member
Hi AllyCat,

Indeed, there are several parameters to take into account to be complete.
Seeking to understand, I finally think I wrote the complete formula in my excel file attached above.
My results are confirmed if I check in parallel with the PwmOut Wizard.
(Sometimes just down to one unit)

There is then just one thing that I cannot explain to myself, it is the "+1" of the term (Period + 1) in the formula.
What does it correspond to?
Do you have an explanation?
 

AllyCat

Senior Member
Hi.

Yes, the reason is that byte values start at "zero" (and wrap back to zero after incrementing from 255), but a zero Period is not usable. However, a value of 256 (which is multiplied by 4 to give the full PWM resolution of 1024 clock pulses) is useful. The "zero" could be used to represent 256, but it is mathematically more correct (and Microchip chose) to add 1 to the byte value to give a PWM period from 1 (* 4 = 4) up to 256 (* 4 = 1024). The maximum Pulse period is actually 1023 (10 bits) because a zero value is useful, so a PWM of 1024 / 1024 is not possible. But the programmer can specify the pin to be "HIGH", or some other PWM duty cycle such as 1020 / 1020 (coded as PWMOUT ... 254 , 1020) etc.

Cheers, Alan.
 

zorgloub

Member
Just for fun.
I corrected and improved my Excel file to calculate Period and Duty Cycle values. (PwmOut Wizard)
It checks and validates the possible data for each type of Picaxe: admissible frequency, prescaler and clock ranges.
The specific data specific to each Picaxe can be selected from a drop-down menu.
Any error or exceeding of range triggers an explanatory warning message.
Enjoy…
 

Attachments

Top