Shop OBEX P1 Docs P2 Docs Learn Events
Code for PWM PAL — Parallax Forums

Code for PWM PAL

huynhhuynh Posts: 16
edited 2009-07-07 10:52 in BASIC Stamp
Dear all forum members,

I would like to ask about the BASIc Stamp code for PAL to generate PWM signal for controlling of Servo of helicopter.


According to the example given in the manual of PWM PAL, to generate a PWM signal with frequency of 5000Hz with duty cycle of 25%, the code is:

SEROUT 0, 6, [noparse][[/noparse]"!PWMM1", 2, 0, 6, 0] % output in pin 12 of PWM PAL;

In another example, code for servo with 1.5ms ON and 20ms OFF is:

SEROUT 0, 6, [noparse][[/noparse]"!PWMM4", 60, 0, 32, 3] %output in pin 15

as I understand, the number of 60 is the ON time (in unit of 25Microsecon). How about the number of 32 and 3, there are OFF time, right? why are they 32 and 3? The OFF time in unit of 25microsecond would be 800 = 20/(25*10-3), wouldn't it?

It's my hornor to have the suggetion for the correct code.

Thanks,

Comments

  • stamptrolstamptrol Posts: 1,731
    edited 2009-07-06 15:59
    The PWMPAL is designed for controlling DC motor speed by applying PWM to the motor though an external transistor.

    I have just started using the PWMPAL myself, so I am not fully aware of all possible details.

    The frequency varies according to the on and off times sent out. The on and off times are WORD values so require two bytes each, hence the 4 numbers. So in your example, 32 is the low byte and 3 is the high byte.

    In driving DC motors I have found good results with on and off times in the range 0 to 255. For smaller motors, Ton + Toff = 100 gives a higher frequency .Much higher frequencies are possible, as shown in the first example.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tom Sisk

    http://www.siskconsult.com
    ·
  • huynhhuynh Posts: 16
    edited 2009-07-07 06:41
    Hi Tom ,

    I agree with you that in controlling of DC motor, the Ton+Toff should be 100. Actually DC motor does not have constrain in the frequency. However, in controlling of Servo, we have to sastify the constrain that TOff<20ms. If we fixed Toff = 20ms, How can we set this value in the command to PWM PAL? SEROUT 0, 6, [noparse][[/noparse]"!PWMM4", 60, 0, 32, 3] %output in pin 15? How can we obtain the value of 32?

    Regards,

    Pthien,
  • stamptrolstamptrol Posts: 1,731
    edited 2009-07-07 10:52
    Pthien,

    In the servo example, the total period is (ONlow.byte + ONhighbyte+OFFlowbyte+OFFhighbyte). That is: 60*25 + (3*25)+(32*256*25). As long as the total remains the same, the frequency will be the same. You can then calculate ON and OFF times as WORD values and send the corresponding highbyte and lowbyte to give the pulse width the servo is expecting.

    I haven't used the PWMPAL for this purpose, but it is described as being able to do it.

    Cheers,

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tom Sisk

    http://www.siskconsult.com
    ·
Sign In or Register to comment.