Shop OBEX P1 Docs P2 Docs Learn Events
need help with PWM — Parallax Forums

need help with PWM

radialrandyradialrandy Posts: 78
edited 2010-09-25 16:53 in Propeller 1
CON

    _xinfreq = 5_000_000                     
    _clkmode = xtal1 + pll16x                           '80MHz

    
    act = 1, tb = 2, mode = 3, up = 4, down = 5
    
VAR long Lpsi, time, co2psi, t1, r1, p1, t2, r2, p2, t3, r3, p3, t4, r4, p4, stack[50]


PUB Main
  Pwm(50, 1, 6)
                
PUB Pwm(duty, t, pin)

  ctra[30..26] := %00100  ' Configure Counter A to NCO
  ctra[5..0] := pin
  frqa := 1
  dira[pin]~~
  duty :=(clkfreq/20)/(100/duty)                                
  t := cnt                               
  
  repeat                                    
    phsa := -duty         'sets duty percent
    t += (clkfreq/20)     '20hz pulse
    waitcnt(t)

Im trying to tell this pwm method a specific duty cycle % values based on a 20hz cycle using PUB main . But with anything over 50 the output stops producing a signal and just stays high. What am I doing wrong here?
thanks

Comments

  • ElectricAyeElectricAye Posts: 4,561
    edited 2010-09-25 12:17
    ...But with anything over 50 the output stops producing a signal and just stays high.....

    By "anything over 50" do you mean "duty over 50"???

    If so, what happens to 100/duty when duty is greater than 50? Remember that operations like that will only give you integers.

    Could that be a problem?

    Also, I think there's at least one PWM object in the OBEX. Have you tried looking there?
  • radialrandyradialrandy Posts: 78
    edited 2010-09-25 12:25
    yes if i enter anything above 50 in the duty varible then the pwm output stops pulsing. the duty cycle looks good on a scope with values of 50 or less. 51 or up stops the pwm output and locks it at high.
  • Tracy AllenTracy Allen Posts: 6,666
    edited 2010-09-25 12:32
    You might try a formula like,
    duty := CONSTANT(clkfreq/2000) * duty

    Since 2000 divides 80_000_000 evenly, there is no error and you avoid the integer division problem that ElectricAye pointed out.
  • ElectricAyeElectricAye Posts: 4,561
    edited 2010-09-25 12:32
    ...51 or up stops the pwm output and locks it at high.

    In SPIN, 100/51 will yield an integer value of 1 .
    Is that really what you want?

    If you need to use fractional values, you will have to use the FloatMath object or use a trick like Tracy just pointed out.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-09-25 12:55
    Use Tracy's suggestion, but either drop the constant( ) pragma, or use _clkfreq instead of clkfreq. (clkfreq is not a constant.)

    -Phil
  • radialrandyradialrandy Posts: 78
    edited 2010-09-25 16:06
    You might try a formula like,
    duty := CONSTANT(clkfreq/2000) * duty

    Since 2000 divides 80_000_000 evenly, there is no error and you avoid the integer division problem that ElectricAye pointed out.

    took "CONSTANT" out and it works perfect.
    THANKS!!!!
  • Tracy AllenTracy Allen Posts: 6,666
    edited 2010-09-25 16:07
    Good call Phil.

    I just realized that the constant _clkfreq cannot be used unless it is defined in the initial CONstants block. I had the idea that defining _xinfreq would automatically allow the _clkfreq to be used. So the OP cannot use _clkfreq, because he used _xinfreq. Is that right? It gives a complile error.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-09-25 16:53
    Tracy,

    I made the same assumption you did, given the codependency of those constants. (I wonder what would happen if you specified all three, but with contradictory values. :devil:)

    -Phil
Sign In or Register to comment.