Shop OBEX P1 Docs P2 Docs Learn Events
Anyone have a barebones example of generating PWM using PropBASIC? — Parallax Forums

Anyone have a barebones example of generating PWM using PropBASIC?

MicksterMickster Posts: 2,694
edited 2013-02-02 09:26 in Propeller 1
I need to drive a motor and I have it somewhat working but not perfectly. Using "Countera Mode 32"

Regards,

Mickster

Comments

  • BeanBean Posts: 8,129
    edited 2012-12-22 16:31
    Mickster,
    You probably want to use mode 48 (DUTY) instead of 32 (PWM).
    The PWM mode creates pulses that are very narrow and will not drive a motor very well.

    This code will use a speed from 0 to 255
      OUTPUT motorpin
      COUNTERA 48, motorpin
      DO
        ' Other code here
        FRQA = speed << 24
        ' Other code here
      LOOP
    

    P.S. I haven't tried the code above, but it should be close.

    Bean

    [/code]
  • MicksterMickster Posts: 2,694
    edited 2012-12-22 16:49
    Thanks Bean....Yeah I came across similar code that you provided as an example for driving an LED but I thought that Mode 32 was the one to use. How do I get more than 8 bits of range though?

    Regards,

    Mickster
  • MicksterMickster Posts: 2,694
    edited 2012-12-22 18:15
    I got it...This works and gives me > 16 bits of speed range. PWM (in Viewport) is looking as it should. PropBASIC is the only way to go!!!!!!!!!!!!!!!!

    Start:
    RDLONG 0, Delta
    COUNTERA 32, MotPlus,0,1
    Delta = Delta >>10
    Target = Cnt + Delta
    
      Do
          PHSA = -20000           '~25% Duty cycle 
          WaitCnt Target, Delta
      Loop
    
    END
    
    

    I realize that this is low frequency stuff but, in this application I need to LPF it and generate a +/- 10v motor command.

    Regards,

    Mickster
  • pmrobertpmrobert Posts: 675
    edited 2013-02-02 09:26
    Thanks Mickster. I had a need for variable PWM freq, DC and duration. Your code helped me get my head wrapped around how the counters work.

    -Mike
Sign In or Register to comment.