Shop OBEX P1 Docs P2 Docs Learn Events
Hardware Timers - Basic Stamp 2 — Parallax Forums

Hardware Timers - Basic Stamp 2

maxima10maxima10 Posts: 2
edited 2011-08-22 08:58 in BASIC Stamp
Basic Stanp 2

Is there a way to generate a PWM for a servo motor without "pausing" the execution of my program?

I want to be able to read other inputs and execute the rest of the program and not wait 20 ms as shown in example below.

' CenterParallaxCrServo.bs2
' {$STAMP BS2}
' {$PBASIC 2.5}
#SELECT $Stamp
#CASE BS2, BS2E, BS2PE ' PULSOUT Duration units are 2 us for these models
Center CON 750
#CASE BS2SX, BS2P, BS2PX ' PULSOUT Duration units are 0.8 us for these models
Center CON 1875
#ENDSELECT

ToServo PIN 12 ' connect servo to I/O pin P12, or change it here

DO
PULSOUT ToServo, Center ' ToServo pin outputs 1.5 ms pulse
PAUSE 20 ' refresh pulse every 20 milliseconds
LOOP


I have also not seen a method for flashing an LED without "pausing" the program. If you wanted to flash an led every 1 second, I would not want to pause my software 1 second just to blink an LED.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2011-08-21 10:06
    Look at programs like Roaming with the BoeBot Robot and Roaming with the PING))). Typically, you breakdown your "reading other inputs" and "rest of the program" into short (< 20ms) pieces and substitute those pieces for the PAUSE 20. In Roaming with the PING))), the program triggers the PING and waits for the echo response, then (ideally) uses the response to figure out how much time is left in the 20ms interval and PAUSEs for that amount of time before issuing the next PULSOUT for the servo.

    The easiest way to flash an LED once a second is to use the PAUSE. There really is no other way to generate accurate intervals. You could use multiple short pauses, like 10ms to generate your clock "tick", count up 100 of those PAUSEs, then toggle the LED. That way, you could check pushbuttons or other brief actions in-between the 10ms PAUSEs. If you need fairly accurate timing, you'd then have to account for the time it takes for those brief actions to actually execute.

    To more directly answer the question ... No, you can't access the hardware timers and, while the Stamp is doing some time-related operation, it can't do anything else. It's strictly a single-threaded processor with no buffering. If you need to do several things at once, you typically add external hardware, like the ServoPAL in this case, to take over the task. Another option is to use the Propeller which can do several things at once.
  • maxima10maxima10 Posts: 2
    edited 2011-08-21 11:51
    Thanks for the very quick response.

    Adruino ..............here I come.
  • Mike GreenMike Green Posts: 23,101
    edited 2011-08-21 12:07
    So much depends on what sorts of things you want to eventually do. The Arduino is cheap. There are excellent tools available for its use and there's a large and growing library of code fragments available, let alone all the "shields" you can add. On the other hand, the Propeller is cheap as well with the QuickStart at $25. There's even an Arduino shield compatible Propeller board for $35. You can program the Propeller in C (Catalina) if you want, but Spin works better for a lot of things. There are two Basic interpreters and the PropBasic compiler which is similar to PBasic.
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2011-08-21 12:31
    maxima10 declares,
    Adruino ..............here I come.
    That about figures.

    One post and it's all about "Adruino".

    Good luck with that, Gianni Trollmaestro.
  • ercoerco Posts: 20,256
    edited 2011-08-22 08:58
    Wasn't Rocky yelling for "Adruin" at the end of Rocky 1?
Sign In or Register to comment.