Shop OBEX P1 Docs P2 Docs Learn Events
Is it possible to use counters as PWM generator with varible frequency? — Parallax Forums

Is it possible to use counters as PWM generator with varible frequency?

tripptripp Posts: 52
edited 2010-08-16 23:22 in Propeller 1
Is it possible to use counters as PWM generator with varible frequency?
And not use any wait command?

I have found this http://propeller.wikispaces.com/page/diff/PWM/92773078
DIRA[ledPin] := 1  
CTRA := %0_0110<<26 + ledPin  
FRQA := $7FFF_FFFF/50 * dimPercent 
But it how do i change the frequency?

The frequency span i would like to have is 10..500 Hz

It would realy help me now.

Comments

  • John AbshierJohn Abshier Posts: 1,116
    edited 2010-08-14 16:42
    NCO mode may be what you need. Check out PE Kit Labs page 137.

    John Abshier
  • tonyp12tonyp12 Posts: 1,951
    edited 2010-08-14 20:12
    Is it Ok if the signal have a 50% duty (=high and low is equal in time)

    Then the counter in NCO/PWM mode will work.

    CTRA := %0_0100<<26 + ledPin ' notice it's %0100

    FRQA between 536 to 26'800 should be good values for the freq you want

    4294967295 / 80'000'000 * wanted_hz = value to use ' the first number should end in 296 but would be 33bits

    shorten : 53.6870912 * wanted_hz = value to use

    video about counters:
    http://www.youtube.com/watch?v=tOkHvNjV4e0
  • T ChapT Chap Posts: 4,223
    edited 2010-08-14 22:16
    If you are trying to set up a way to dim and LED, you can use this:
    PUB INIT
          dira[ledpin]
          repeat
              setdim(freq)
    
    
    PUB setdim(freq)
        ctra := %00110 << 26 + ledpin
        frqa := freq
    
    


    You can ramp from 0 to 4billion to full brightness, or ramp from 4b to 0 to dim. It is not true PWM but for an LED is fine. Add a cap to smooth it out if you want to, but not required.
    PUB INIT    | x
          dira[ledpin]
          repeat
            repeat until   x == 4_200_000_000
              setdim(x)
              x := x + 100_000   'ramp rate
            repeat until   x == 0
              setdim(x)
              x := x - 100_000   'ramp rate
    
    PUB setdim(freq)
        ctra := %00110 << 26 + ledpin
        frqa := freq
    
    


    EDIT Changed freqa to frqa.
  • tripptripp Posts: 52
    edited 2010-08-14 23:06
    Thanks for all quick answers.

    T Chap: In what way is it not true PWM?

    The PWM will be used to control a solonoid, so i guess it needs to be true PWM.

    tonyp12: That is almost what i need but i need to be able to have 5-95% dutycycle.
    Could your code be modified to be able to do that?
    Maybe by preload PHSA it would adjust the dutycycle?
    Tanks for the video.

    John Abshier: Thanks i will read that part some more times.
  • T ChapT Chap Posts: 4,223
    edited 2010-08-15 03:30
    You could drive a solenoid with the same code, you just need to use a mosfet to turn on the low side, you couldn't drive the solenoid from the Prop pin.
  • Tired2Tired2 Posts: 29
    edited 2010-08-16 12:28
    Seems strange you would want to use PWM to drive a solenoid. That is like using it to drive a relay, you'll just make it clatter around a lot.

    Check out the object: pwmAsm. I think it will do what you want. I use it for motor speed control, it does 0%-100% pwm with user frequency set. 2 outputs per cog (some other objects will allow for more and phase shift, but I like to keep it simple since I dont need more than two).

    Object: http://obex.parallax.com/objects/216/
  • T ChapT Chap Posts: 4,223
    edited 2010-08-16 15:21
    Yes I would think it unusual to use PWM on a coil like that, but actually I am about to test out the same exact idea, with the hope to make the solenoid open and close a little quieter by ramping the voltage up and down to turn it on and off, but a fast ramp. For something as crude as ramping and LED or coil like that, it makes more sense to use the code I posted which can run the psuedo PWM in the background with no COG required, only update the FRQA as desired by another program. PWMASM requires a dedicated COG to PWM.
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2010-08-16 16:15
    >> Is it possible to use counters as PWM generator with varible frequency?
    No.

    But attached is a program to output variable PWM and variable Frequency on one pin.

    I also have objects that will do the same with up to 6 outputs or up to 31.
    6: http://obex.parallax.com/objects/422/
    31: http://obex.parallax.com/objects/506/

    They are all 1 - 99% duty, but they could be easily modified to do 0 - 100%.

    Take a look at this thread: http://forums.parallax.com/showthread.php?t=124450
  • jmgjmg Posts: 15,185
    edited 2010-08-16 16:32
    T Chap wrote: »
    Yes I would think it unusual to use PWM on a coil like that, but actually I am about to test out the same exact idea, with the hope to make the solenoid open and close a little quieter by ramping the voltage up and down to turn it on and off, but a fast ramp. For something as crude as ramping and LED or coil like that, it makes more sense to use the code I posted which can run the psuedo PWM in the background with no COG required, only update the FRQA as desired by another program. PWMASM requires a dedicated COG to PWM.

    PWM drive to solenoids is actually quite common.

    It can be used as you mention to lower shock, and also to power regulate, as the Force/Distance curve of a Solenoid is very non-linear, and Holding-Voltage can be much lower then pull-in voltage.

    If you want to remove coil-temperature effects, you can current-sense, and PWM drive using that info. (that avoids the copper PTC effect lowering amp-turns on a hot coil)
  • Tracy AllenTracy Allen Posts: 6,666
    edited 2010-08-16 23:22
    It is possible to make "standard" PWM with independently variable frequency by combining the output of two counters in NCO mode. The phases are offset and take advantage of the usual Prop OR logic for multiple sources driving one pin. The output duty cycle without outside help can be set from 50% to 100%. By adding an external XOR gate controlled by a second Prop pin, it can cover the whole 0% to 100% range. The XOR acts as an inverter when duty needs to be less than 50%. This uses ctra + ctrb only, no pasm and no program intervention to keep it running once it is set at a certain frequency and duty cycle.

    I'm attaching a demo in tutorial form of how it works.
Sign In or Register to comment.