Hardware Timers - Basic Stamp 2
maxima10
Posts: 2
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.
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.
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
' {$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
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.
Adruino ..............here I come.
One post and it's all about "Adruino".
Good luck with that, Gianni Trollmaestro.