Question about PWMOUT and PWMDUTY

abenn

Senior Member
Some short time ago inglewoodpete posted a basic program to fade an LED on/off to look like a pulsating beacon light, in response to a query of mine. It basically loops, using PWMDUTY and incrementing the duty cycle each time. It starts with "PWMOUT c.2,63,0" and then increments up to "PWMDUTY,c.2,254" before reversing direction to dim the LED, then starting again.

It works fine, but the numbers have me slightly puzzled: In the manual it says that the Period is a variable/constant from 0-255, and the Duty Cycle is a variable/constant from 0-1023, but the accompanying graphic appears to suggest that the Duty Cycle should be smaller than the Period, but that's obviously not the case when one reads the text.

So, taking "PWMDUTY,c2,245" as an example, does that mean that the LED will be powered for 245/1023 of whatever the Period is, and using Duty Cycle = 1023 will power it for 100% of whatever the Period is -- i.e. fully on?
 

premelec

Senior Member
I don't know if this is how the terms are being used for PICAXE but period would commonly be ON to ON [1/f] and duty percent _during_the period that the signal is ON. If I'm not correct someone correct me!
 

inglewoodpete

Senior Member
It works fine, but the numbers have me slightly puzzled: In the manual it says that the Period is a variable/constant from 0-255, and the Duty Cycle is a variable/constant from 0-1023, but the accompanying graphic appears to suggest that the Duty Cycle should be smaller than the Period, but that's obviously not the case when one reads the text.
The values of the two numbers (period and maximum duty) do bear a relationship of about 1:4. However, they are different parameters that go into different internal registers within the PWM generator silicon in the chip to perform different sub-functions. Both of these internal registers are very basic binary counters and we programmers have to live by the 'rules' associated with them.

So, taking "PWMDUTY,c2,245" as an example, does that mean that the LED will be powered for 245/1023 of whatever the Period is, and using Duty Cycle = 1023 will power it for 100% of whatever the Period is -- i.e. fully on?
Perhaps the best way to describe how the parameters fit together is to use three examples that I have created using the PWM Wizard in the Programming editor.

These commands were generated for a 20X2, running at its default speed of 8MHz. In each case, I have chosen a duty cycle of 100%, since this shows the maximum number that should be used for duty in each case. I have also chosen PWM frequencies >=7820Hz to avoid using the PWM clock divider, which can be used to produce lower PWM frequencies but could confuse the discussion.

100% duty for a PWM frequency of 7,820Hz:
pwmout C.5, 255, 1023 'allows steps of about 0.1%

100% duty for a PWM frequency of 15,640Hz:
pwmout C.5, 127, 511 'allows steps of about 0.2%

100% duty for a PWM frequency of 31,280Hz:
pwmout C.5, 63, 255 'allows steps of about 0.4%

You can see that, for each selected PWM frequency, the maximum duty value (100%) is always 4 times the period value**. ** although the actual formula for a 100% duty value = ((period+1)*4)-1 due to quirks is how the binary counters work.

Also, you will see that the larger the duty cycle range (Eg 0 to 1023 versus 0 to 255), that smaller the increments can be. This can be useful when driving an LED down to 0% duty cycle as the larger steps can become obvious to the human eye.

Note that the higher PWM frequencies may be OK for driving an LED directly from a microcontroller but may present problems when driving an inductive load (Eg motor) or a capacitive load like the gate of a MOSFET. In general terms, larger loads require lower PWM frequencies.
 

abenn

Senior Member
Thanks again. I wish the manual would go into that kind of simple explanation. Before posting I experimented by using various different numbers in your pulsating beacon light program, including PWMOUT c.2,63,1023 and PWMOUT c.2,255,255. In the former case it seemed to result in the LED dimming less, but in the latter case it seemed, to my eyes, to make no difference to the pulsing. Anyway, I'm now going to stick with 63 and 255 since it works so well in this application, where my 08M2 is driving the LED via a Darlington Pair.

I appreciate the need to choose the correct frequency when using PWM with devices other than LEDs.
 

premelec

Senior Member
@IP - is there any limitation as to PWMOUTDIVxx and clock speed? I'm wondering how low can you go [assuming chip has low clock available - SETFREQ] - thanks for the excellent discussion!
 

Rick100

Senior Member
100% duty for a PWM frequency of 7,820Hz:
pwmout C.5, 255, 1023 'allows steps of about 0.1%
If you look at the output of the pwm pin on a scope or logic analyzer, I think you will see the duty cycle is not 100% . The pin will be high for .000127875 seconds and low for .000000125 seconds. A total period of .000128 seconds and a 99.9 percent duty cycle. I think this is a quirk of the 10 bit pic pwm module. If the period value is 255, you need a count of 1024 for 100% duty cycle, so it won't fit in the 10 bit register. It won't matter when driving leds but for some applications it might be important. I've looked at the output on a scope and a logic analyzer to confirm this. The output is not a constant high.
 
Top