Shop OBEX P1 Docs P2 Docs Learn Events
PWMing the Propeller — Parallax Forums

PWMing the Propeller

NewzedNewzed Posts: 2,503
edited 2007-05-20 23:22 in Propeller 1
How would I output a PWM signal on a Propeller pin?

Sid

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Yesterday is history, tomorrow is a mystery, and today is a gift.

That is why they call it the present.

Don't have VGA?
Newzed@aol.com
·

Comments

  • Bryan K.Bryan K. Posts: 47
    edited 2007-05-19 21:47
    What kind of PWM? the duty cycle modulation for an H-bridge, or the RC servo pwm pulse?
  • NewzedNewzed Posts: 2,503
    edited 2007-05-19 21:55
    Does it really matter what I do with the PWM output?· As a matter of fact, I want to drive four Nichia RGB chips.· That takes 12 pins, each with its own PWM duty cycle.

    Sid

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Yesterday is history, tomorrow is a mystery, and today is a gift.

    That is why they call it the present.

    Don't have VGA?
    Newzed@aol.com
    ·
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-05-19 22:51
    The servo type is not really PWM as the pulse width changes but the time between pulses does not where as PWM proper is the case where the frequency is fixed and the on time and off time add up to the time period. Its pedantic but I'd keep the term pwm for that use only, all engineering literature does.

    There is a thread right now on making PWM on many pins and there has been stuff in the past. There is a pwm motor drive object as well I think.

    Have you come up with any ideas yourself?

    Graham
  • NewzedNewzed Posts: 2,503
    edited 2007-05-19 23:03
    Not yet, Graham.· I'm using a BS2 at the moment to drive the Nichia chips, but at 3 pins per chips the Stamp can only handle four max.· I made a new board for the Propeller on SuperMill but I haven't assembled it yet.· I was hoping to dedicate that Prop to the Nichia project, if, I say if, I can find out how to PWM 21 different pins, each with its own unique duty cycle.

    Sid

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Yesterday is history, tomorrow is a mystery, and today is a gift.

    That is why they call it the present.

    Don't have VGA?
    Newzed@aol.com
    ·
  • mynet43mynet43 Posts: 644
    edited 2007-05-19 23:11
    If you look here, there's a good application note on Propeller Counters. It may be hard to understand but it covers how to do a PMW.



    www.parallax.com/propeller/downloads.asp
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-05-20 00:57
    Unfortunately you can only do two pins per cog with counters.

    Have a look at:

    http://forums.parallax.com/showthread.php?p=650896

    What Dennis and I suggest is essentially the same thing, you take the time period and split it into say 100 bits. So a low duty would be on for one slice and off for 99. Dennis loads the pin states for each slice into a table and then runs through them, I suggest using a fixed time length loop toggle the pins depending on the state of some variables acting as counters.

    Mine would have a lower maximum possible frequency but quicker PWM change, his the opposite I think because to change any of the PWM rates you have to update the whole table, mine you just update one of the on_times of off times but mine will require more processing per slice.

    Graham
  • NewzedNewzed Posts: 2,503
    edited 2007-05-20 13:17
    Graham, I had already decided on something like that.· Clkfreq/80000 = 1ms.· If I write in the CON block 'dur = clkfreq/80000', then:

    repeat x times

    dira[noparse][[/noparse]0] =·1
    outa[noparse][[/noparse]0] = 1
    wait(dur*5 + cnt)
    outa[noparse][[/noparse]0] = 0
    wait(dur*5+ cnt)

    Then that would be a duty cycle of 50 percent with an On time of 5ms, which basically is what I am using with the Stamp.· The duty cycle varies with each color and with each color combination such as yellow, magenta, orange and white.· Total time for the cycle is·5 + 5 = 10.· At least, that will be my initial approach.· When I generate a color like white, all three LEDs need to be turned on, 5ms On for each LED is OK, but if I am running two chips (6 LEDs), the time On for each LED has to be about 3ms to avoid flickering.· I'll have to fine tune the math once I get it going.

    Sid

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Yesterday is history, tomorrow is a mystery, and today is a gift.

    That is why they call it the present.

    Don't have VGA?
    Newzed@aol.com
    ·
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-05-20 14:44
    but you can't use a waitcnt like that if you have multiple outputs at different duties.

    Graham
  • AnubisbotAnubisbot Posts: 112
    edited 2007-05-20 16:46
    Hi , i had the same problemi want to drive 4 RGB leds and 2 normal ones, and 5 mosfets for led cluster , and i got around it.

    i use the 8 Bit PCA9551 I2C LED Dimmer, Mixer on my new board. So only i cog has to deal with 16 leds.

    And the good thing about that is you keep more pins for other stuff.

    Anubisbot
  • NewzedNewzed Posts: 2,503
    edited 2007-05-20 18:07
    Graham, for multiple outputs with different duties, I would use it exactly like that.· For instance, to generate yellow on the Stamp I write:

    PWM red, 225,3
    PWM green, 125,3

    The total On time is 225 + 125 = 340.· The On time for red is 225, or
    66% of the total time.· With the Prop I would write:

    CON
    wait = 80_000_000/80_000···· ·'wait = 1_000us or .1ms

    outa[noparse][[/noparse]red] := 1
    waitcnt(wait*88+ cnt)···' = 8.8ms·· PWM of 225 is ON 88% of the time.
    outa[noparse][[/noparse]red] := 0
    waitcnt(wait*12 + cnt)· ' = 1.2ms···PWM of 225 is OFF 12% of the time.

    outa[noparse][[/noparse]grn] := 1
    waitcnt(wait*49+ cnt)· '= 4.9ms··· PWM·of 125·is on 49% of the time.
    outa[noparse][[/noparse]grn] := 0
    waitcnt(wait*51 + cnt)·' = 5.1ms· PWM of 125 is OFF 51% of the time.

    Cycle time for each color is 10ms.

    The ratio of 8.8:4.9 is the same as 225:125.· I might have to tweak a bit here and there, but basically that is how it would go.

    Do you concur with this approach?

    Sid

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Yesterday is history, tomorrow is a mystery, and today is a gift.

    That is why they call it the present.

    Don't have VGA?
    Newzed@aol.com
    ·
  • whickerwhicker Posts: 749
    edited 2007-05-20 19:44
    With the clock speeds we're dealing with and dedicated cogs of the propeller, there's no reason why one couldn't PWM all 32 pins.

    Just have a byte array holding the brightness (B) for each pin you want.
    Let's say it's 0..100% (101 total steps)

    Have an outer for-loop (let's call its variable T)
    Have T count from 0 to 99... (100 total steps)

    The inner for-loop steps through all the pins you want,
    if T => B, then the output is off, otherwise the output is on.
    Finish the inner loop

    wait for 100uS, or whatever. (0.0001 seconds) (Pulse frequency F = (1/T) / 100 steps = (1 / .0001 s) / 100 steps = 100 Hz)
    Finish the outer loop

    ---

    The weirdness of brightness comes from the case that if the brightness is zero, you never want the pin on.
    T = 0, B = 0, "T => B" is true, output is off here and for the entire period.

    And if the brightness is 100, you always want the pin on.
    T = 99, B=100, "T => B" is false, output remained on for the entire period.



    (ARGH, I hate spin's syntax for ">=". Seriously, why go down this road?)
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-05-20 19:53
    Sid,

    Well not really, you say the red is on for 88% of the time but it isn't because it is also off for the whole time the green led is on (and off). Imagine you have 20 outputs it might be on for 8.8ms then off for 1.2ms but then it would be off for a further 19* 10ms = 190ms, hardly 88% duty.

    Really you want both of the leds to come on at once and then one to turn off at a different time to another up to a maximum time equal to the time period of your PWM frequency.

    For your example hand coded:

    
    repeat
       outa[noparse][[/noparse]r] := 1
       outa[noparse][[/noparse]g] := 1
       waitcnt(wait*49+ cnt)    
       outa[noparse][[/noparse]g] := 0
       waitcnt(wait*39+ cnt)    
       outa[noparse][[/noparse]r] := 0
       waitcnt(wait*12+ cnt)  
    
    
    
    



    But you can't code it like that if you want it adjustable, hence my time sliced idea explained in the other thread.

    Graham

    Post Edited (Graham Stabler) : 5/20/2007 7:59:28 PM GMT
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-05-20 19:59
    Sid, just looking at what you are doing again, in the situation where you ar cycling through leds, perhaps multiplexing them that would work but not if you want true PWM.

    Graham
  • NewzedNewzed Posts: 2,503
    edited 2007-05-20 20:50
    Graham, on the Stamp, if I'm running cyan, I PWM blue for 10ms, the PWM green for 10ms.· Technically, when the blue is on the green is off and vice versa.· However, the human eye can not see this On/Off sequence because of POV (persistance of vision). There fore the eye sees both colors that when mixed produce cyan.· Same for orange, magenta, yellow and white.· I will start out running only one chip at a time.· When I get that working I'll try two chips and see if I can switch them fast enough to avoid flicker.· I will have four chips on the board but I may noit run more than two at a time.· I'll have to see how it works out.

    Does that clear things up a bit?

    Sid

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Yesterday is history, tomorrow is a mystery, and today is a gift.

    That is why they call it the present.

    Don't have VGA?
    Newzed@aol.com
    ·
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-05-20 22:55
    Well if you do it the way I suggest you will get much better maximum brightness, although you are varying pulse width it is not a true PWM output and that is what you asked about.

    The way you are doing it you already know how!

    Graham
  • NewzedNewzed Posts: 2,503
    edited 2007-05-20 23:22
    Graham, a bit of background on the chip.· Max current on red is 50ma, green 35ma and blue 35ma.· I'm using 68 ohms on red and 47 on green and blue.··With each LED full on, the green is very bright, red is not quite so bright and blue is a bit dim.· I adjusted the brilliance via PWM so that each one had almost the same brilliance, althoiugh blue is not as bright.· I'm drawing about 20ma on red and green and about 27ma on blue.· Current varies on the mixed colors with pink drawing a low of about 9ma.

    This is really a fascinating chip - you should get some samples!

    Sid

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Yesterday is history, tomorrow is a mystery, and today is a gift.

    That is why they call it the present.

    Don't have VGA?
    Newzed@aol.com
    ·
Sign In or Register to comment.