Ed Straker
Active member
I have been searching through the forum for information on my next learning challenge, using PWM.
I have run into a wall of sorts when it comes to understanding the the Period vs Duty coding relationship.
Since I plan on using this feature to it's full potential, primarily for lighting purposes,
I would really like to get firm grip on what's going on.
I did find some code posted on the forum by Hippy, so this is sort of directed his way, showing a simple fade program.
I tried to decipher what is going on from code only in order to Block Code it out so I might better understand it but was unsuccessful.
So, @hippy:
Could you show what this looks like as Block:
The manuals have created a bit of confusion for me in regard to duty reference.
In code examples, I see duty cycles referenced as 0-255. But in Manual2 it references a range of 0-1023?
I know I am seriously just missing something here.
Since the Period(Freq) is a default 4kHz (which I assume to be more than sufficient for discreet LED work).
It would be the Duty(Fade/brightness) which would be of the most concern?
The monkey in the works came when looked up the PWM Wizard.
4MHz is actually the Clock Speed, ok, understood.
The a period is asked for in kHz, this where it goes off the rails for me as I don't know what Freq would be reqired for a specific
outcome (in my case LED fade and crossfade)
Next is duty cycle (which should be responsible for (brightness in my case) in percent which makes complete sense.
Since there is no pwmduty Block per se, and any duty adjustment would I assume then have to be done via Var"X" commands, this is where I hope the Block conversion of the above code would come in handy for me to begin to fully understand it.
My apologies for the long and hopefully not to confusing post.
Thanks in advance
I have run into a wall of sorts when it comes to understanding the the Period vs Duty coding relationship.
Since I plan on using this feature to it's full potential, primarily for lighting purposes,
I would really like to get firm grip on what's going on.
I did find some code posted on the forum by Hippy, so this is sort of directed his way, showing a simple fade program.
I tried to decipher what is going on from code only in order to Block Code it out so I might better understand it but was unsuccessful.
So, @hippy:
Could you show what this looks like as Block:
Code:
#Picaxe 14M2
Symbol MIN_LEVEL = 15
Symbol MAX_LEVEL = 1023
Symbol TIME_MS = 15000
Symbol MIN_MAX_GAP = MAX_LEVEL - MIN_LEVEL
Symbol STEP_PERIOD = TIME_MS / MIN_MAX_GAP
PwmOut C.0, 255, 0
Do
For w0 = MIN_LEVEL To MAX_LEVEL Step 1
PwmDuty C.0, w0
Pause STEP_PERIOD
Next
For w0 = MAX_LEVEL To MIN_LEVEL Step -1
PwmDuty C.0, w0
Pause STEP_PERIOD
Next
Loop
The manuals have created a bit of confusion for me in regard to duty reference.
In code examples, I see duty cycles referenced as 0-255. But in Manual2 it references a range of 0-1023?
I know I am seriously just missing something here.
Since the Period(Freq) is a default 4kHz (which I assume to be more than sufficient for discreet LED work).
It would be the Duty(Fade/brightness) which would be of the most concern?
The monkey in the works came when looked up the PWM Wizard.
4MHz is actually the Clock Speed, ok, understood.
The a period is asked for in kHz, this where it goes off the rails for me as I don't know what Freq would be reqired for a specific
outcome (in my case LED fade and crossfade)
Next is duty cycle (which should be responsible for (brightness in my case) in percent which makes complete sense.
Since there is no pwmduty Block per se, and any duty adjustment would I assume then have to be done via Var"X" commands, this is where I hope the Block conversion of the above code would come in handy for me to begin to fully understand it.
My apologies for the long and hopefully not to confusing post.
Thanks in advance
Last edited: