Shop OBEX P1 Docs P2 Docs Learn Events
SX28 and PWM — Parallax Forums

SX28 and PWM

Larry MolterLarry Molter Posts: 28
edited 2009-02-17 16:28 in General Discussion
Could someone just verify that the PWM output is just plain-old regular PWM (you know, a square wave with varying duty-cycle)?· I know this is a stoopid question, but most of the examples I have looked at on the web are using PWM to create analog voltages.· I just want to drive a MOSFET for a simple dc motor controller.

Larry

Comments

  • Sparks-R-FunSparks-R-Fun Posts: 388
    edited 2008-03-20 00:19
    Larry,

    The SX/B PWM command is intended for use in creating analog output voltages. For example, with a 50% duty cycle setting the result is a stream of high and low pulses of equal durations during the time interval specified. If you want to control the speed of your motor this might work for you. However, if you want to create just a single pulse output (which you can repeat to make a pulse stream) where you determine how long the pulse is high and how long it is low you will probably want to use the PULSEOUT command instead.

    The PULSEOUT command will create a pulse with a single transition within the time period specified. The PWM will likely generate many transitions during the time period specified.

    Example outputs where H = HIGH, L = LOW
    PULSEOUT: HHHHHLLLLL at 50%
    PWM: HLHLHLHL at 50%


    NOTE: The SX48 has two internal timers you can use to set up two pulse streams in hardware that continue to run on their own once configured. These are very nice for motor control! If you are using an SX48 be sure to consider them!


    I hope this helps.

    - Sparks
  • RobotWorkshopRobotWorkshop Posts: 2,307
    edited 2008-03-20 01:24
    The SX48 timers do work well for motor control. There is an article in next months SERVO which uses an SX48 to control a motor and use an encoder for closed loop control. It worked out great!
  • Larry MolterLarry Molter Posts: 28
    edited 2008-03-20 22:34
    Ohhhhh.· Boy, am I a doofus.· I had it in my mind that once I issue a PWM command (via SX/B) that the chip would output a continuous stream of pulses whose duty cycle and period were determined by the parameters to the function.· What you really get is one pulse. D'Oh. You have to put the code in a loop to get a continuous stream (right? he asks sheepishly).·

    Here's what I want to do and it's real simple: Footswitch depresses and motor goes fast.· Press again and motor goes slow.· This action will toggle between fast and slow until the footswitch is held for more than 2 seconds. At this point, the motor goes off.· I'm trying to simulate the action of my Leslie 2101 Treble unit (you know, the speaker that rotates).· I made a bass unit with a rotating drum, but the ac motor is single-speed.· I would like to convert the motor to a dc motor and adjust the speed via a pwm system.· Since I already coded the footswitch in SX/B to light LEDs for fast and slow and off, I was hoping to just add some pwm code to drive a MOSFET and a motor.

    I bought a couple of IRF510s and couple of heatsinks (I only need one - I'm not making an H-bridge) and I'm ready to burn something out <g>.· Luckily everything I'm playing with is cheap. And no, I don't have the final motor yet.· All I have is a Radio Shack hobby motor for testing.

    Larry
  • BeanBean Posts: 8,129
    edited 2008-03-20 23:09
    Larry,
    · There are a couple options.
    · One would be to use the SX48 timers.
    · Another would be to create an interrupt driven PWM routine (this is fairly easy).

    · Note that the SX/B PWM command is meant to generate an analog output. It doesn't work well for motor control because the PWM pulses are narrow.

    · If you post your current code, I will show you how to modify it for an interrupt·generated PWM output.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    www.iElectronicDesigns.com



    Post Edited (Bean (Hitt Consulting)) : 3/21/2008 12:08:52 AM GMT
  • Larry MolterLarry Molter Posts: 28
    edited 2008-03-21 00:39
    Bean, I think I finally get the whole pwm thing. Hopefully I didn't embarass myself too badly in my posts <g>. So, it's on to Plan B: Dual 555 as pulse-width modulators - one for the fast speed and one for the slow. I'll use the i/o pins from my footswitch controller to switch the speeds through logic gates. This is actually a better approach because I can trim the speeds with small pots.

    Thanks to everyone who replied.

    Larry
  • Guenther DaubachGuenther Daubach Posts: 1,321
    edited 2008-03-21 08:29
    Larry,

    I've got a vintage Leslie 122 here (together with a Hammond B3), and I also was thinking to replace the old two-speed AC motor units by something new in the future. I did some testing with brushless DC motors available from Papst, a German company. These motors come with integrated driver electronics, and their speed can be controlled over a wide range by applying a DC voltage from 0 to 5V to an input.

    Although these motors are way more expensive than simple hobby motor, there is no need for any gears, or capstan drives as on the olt original units, and these motors are very silent.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Greetings from Germany,

    G
  • JonnyMacJonnyMac Posts: 9,412
    edited 2008-03-21 20:28
    Larry,

    It's pretty easy to create "background" PWM with an interrupt -- that would save you the trouble of external circuitry.
  • Guenther DaubachGuenther Daubach Posts: 1,321
    edited 2008-03-22 00:06
    Larry,

    as JonnyMac said, PWM signals can be generated with any type of SX using a timed interrupt. The "larger" SX48 offers the additional "luxury" of two multi-purpose timers/counters that can be configured to generate PWM output. Nevertheless, the "smaller" SXes are powerful enough to generate one or more PWM signals with an interrtupt routine.

    For one of my commercial applications, I'm using an SX28 to generate three independent PWM outputs for controlling three DC motors. This design works "rock-solid", and there are over 800 units out in the field so far. So why using 555s and logic gates - the SX can handle all the tasks you are looking for - PWM, and reading the footswitches.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Greetings from Germany,

    G
  • Larry MolterLarry Molter Posts: 28
    edited 2008-03-22 18:04
    Ok, Gunther and JohnnyMac, I'm game. Where do I start? I'm a novice (albeit an 53-year-old one <g>). I'm ok with interrupts in general - my first cut at the footswitch code was interrupt-driven. However, I simplified it with loops. I also had issue with the ISR not being able to access my subroutines because they were outside of the first page ("address 260 is not in the lower half of the page" message). When I made a looping model, no more page alerts. This kind of scared me away from interrupts. But... I'd rather not have to build the pwm circuitry if I don't have to.

    And can I do it with SX/B instead of SASM? I've lost my assembly-language skills way back in 1980 when I learned 'C' <g>.

    Larry
  • JonnyMacJonnyMac Posts: 9,412
    edited 2008-03-22 20:15
    Sure. Tell me what pins you want your PWM on and what the PWM frequency should be and I'll whip up a little program -- no assembly required -- that will take care of it for you.
  • Larry MolterLarry Molter Posts: 28
    edited 2008-03-22 20:30
    Ok. Any pin will do - how about rb.0? And as far as the pwm frequency goes... when controlling a motor, should the frequency be above the audible range? I've seen controllers put out at 15KHz, 9KHz, and some at 250Hz. Since you have more experience with motor control than I do (pardon my assumption), choose what's best. I forgot what the Motor Mind B does, but I believe it's above the audible range.

    Thanks again.

    Larry
  • JonnyMacJonnyMac Posts: 9,412
    edited 2008-03-22 20:46
    Have a look at the program I've attached. I used an ISR rate that will give you a PWM frequency of 600 Hz and allow for the addition of ISR-based transmit and receive should you want to add that later. If you don't, you can always bump the ISR rate (remember, the PWM takes 256 ISR cycles).

    When using interrupts in SX/B any time-based processes are affected; you'll see that this example has a replacement for PAUSE that uses cycles through the interrupt for timing.


    (File removed -- replaced with update below)

    Post Edited (JonnyMac) : 3/23/2008 5:29:17 PM GMT
  • Larry MolterLarry Molter Posts: 28
    edited 2008-03-23 02:09
    Gadzooks, I've lost my 20MHz resonator. All I've got is the 4MHz one. Also, Jon, could you give a quick overview of how the whole thing works. To me, it's tricky to understand at first glance. And after I figure it out, maybe I can modify it to work at 4MHz.

    L
  • JonnyMacJonnyMac Posts: 9,412
    edited 2008-03-23 17:26
    It's actually pretty simple: there is a timer that reloads a PWM accumulator every 256 cycles (when the timer rolls over from 255 to 0). After the accumulator is loaded with the desired duty cycle (0 to 255 = 0 to 100%), the accumulator is incremented each pass through the ISR. When the accumulator rolls over the output is set high, so the higher your duty cycle, the earlier the accumulator rolls over and the quicker the output goes high.

    I actually found a small error (I just got home from a trip and have my scope packed -- sorry). Give the version attached to this message a try.

    Post Edited (JonnyMac) : 3/23/2008 5:32:09 PM GMT
  • Larry MolterLarry Molter Posts: 28
    edited 2008-03-23 19:34
    Jon, I thought something was amiss because I stuck a LED on the motor port and it lit up once and never went out. The new code works as I expected it should. And thanks for the explanation. I'll stare at the code a little and try to figure out how to integrate the footswitch (on my own <g>).

    By running at 4MHz instead of 20, the ISR is still run 153000 times per second, but all the other timing is off, right? The one second pauses are now 5 seconds (of course). So did the pwm frequency go down to 100Hz then? Doesn't really matter, I guess. And I think I'm getting a scope on Monday (a real Tektronix (sp?), not a usb). That'll help me make sense of it all.

    Thanks again,

    Larry
  • JonnyMacJonnyMac Posts: 9,412
    edited 2008-03-23 21:00
    At 4MHz you are missing interrupts, hence the apparent change in speed. If you have an SX-Key you can let the Key generate the clock and see the timing work correctly.
  • ken261ken261 Posts: 4
    edited 2009-02-16 21:01
    JonnyMac,
    Sorry to resurrect an old thread, but this is my first post here and I'm not sure how else to ask directly about this code of yours..
    I works well, but I have two questions:
    1. Why do you use the DELAY_MS subroutine?

    2. Would it be possible to incorporate SPI into this ISR?

    My SX28 is going to be a slave device running 50MHz, and I want to send duty cycle (as well as other commands) via SPI

    Thanks
  • JonnyMacJonnyMac Posts: 9,412
    edited 2009-02-16 22:42
    1. When using interrupts time-oriented routines like PAUSE get hammered; DELAY_MS uses interrupt timing to create a delay

    2. I'm sure it is but I've never done it (I'll bet my pal, Gunther, has). Why use SPI? Can you use serial? -- easier to implement (one wire versus 3) and many of use have a lot of code that is ready to use. Bean has done some work turning the SX into an SPI slave -- you might do a search.

    [noparse][[/noparse]Edit] Found Bean's SPI code -- though it uses edge-triggered interrupts which won't help you with the PWM stuff.
    -- http://forums.parallax.com/showthread.php?p=671517

    Post Edited (JonnyMac) : 2/16/2009 11:32:07 PM GMT
  • JonnyMacJonnyMac Posts: 9,412
    edited 2009-02-16 23:49
    I've attached a serial example. This uses the Parallax AppMod protocol and is configured so that the device can have an address of %00 to %11 and a selectable baud rate (2400 or 38.4k -- matches the PSC). Communications with the host (e.g., BASIC Stamp, another SX) is Open-True mode serial; bi-directional half-duplex over one wire. The project is a simple 8-channel LED controller (PWM). Note that this style PWM is variable frequency based on duty cycle.
  • ken261ken261 Posts: 4
    edited 2009-02-17 13:25
    Thanks,
    The reason for SPI is that I have an entire bus of various devices (digital pots, 14bit ADCs, etc) that are all being controlled via USB --> SPI.
    I am integrating the PWM and digital I/O capabilities of the SX as a further development (without starting from scratch).

    I think I had found that SPI code, but thank you. I will try my hand at integrating the two into one interrupt unless this seems like something good for TASKs.
    My PWM cycle is only 60 Hz, so accuracy should be easy to attain.
  • JonnyMacJonnyMac Posts: 9,412
    edited 2009-02-17 16:28
    You can find SPI code demos (master and slave) on this page: www.parallax.com/tabid/460/Default.aspx
Sign In or Register to comment.