Shop OBEX P1 Docs P2 Docs Learn Events
Pause using 1/2 as duration mystery — Parallax Forums

Pause using 1/2 as duration mystery

T ChapT Chap Posts: 4,217
edited 2006-06-17 04:36 in BASIC Stamp
I was looking for ways to get a shorter Pause than 1ms, which I have been told is the shortest duration available for pause times. I just tried using a variable for Pause duration called PauseTime. The resuslts are strange in that using the code below, the time it takes to run the loop is as follows:


(Measure starting at count 1 on release of reset on demo board)

PauseTime = 0 5 seconds approx
PauseTime = 1 5 seconds
PauseTime = 1/2 close to 3 seconds
PauseTIme = 1/3 or smaller 3 seconds

I guess it really is dividing the pause time in half, although there is some slight variance with my counting(without a stopwatch). What is going on here?




COUNTER VAR WORD
PauseTime VAR BYTE


PulseCount = 0 'Zero out PulseCount
'PauseTime = 0
PauseTime = 1/2
'PauseTime = 1



MAIN:
FOR counter = 1 TO 1000
Mstep = 1
PAUSE pausetime
DEBUG "*"
Mstep = 0
PAUSE pausetime
DEBUG "-"
NEXT
DEBUG ? counter
END

Comments

  • edited 2006-06-17 02:37
    The result of pausetime = 1/2 is 0. So the command later in your program is really PAUSE 0, which results in a 230 us (approximate) delay for a BASIC Stamp 2.

    If you want better control over your pauses, you can send the PULSOUT command to an unused I/O pin. For example, PULSOUT 1, 0, results in about a 210 us delay. Since PULSOUT command's Duration argument specifies the number of 2 us increments the pulse should last on the BASIC Stamp 2, you can use the fact that the PULSOUT command itself takes 210 us to create delays with a finer resolution than the PAUSE command's 1 ms Duration time increments.

    For example, let's say your application needs a 1.5 ms (1500 us) pause. For a 1.5 ms pulse, you would use PULSOUT 1, 750 because 750 x 2 us = 1500 us = 1.5 ms. Since the BASIC Stamp 2 execution time for a PULSOUT command is about 210 us, subtract 105 (2 us per PULSOUT Duration unit) from 750. The result (PULSOUT 1, 645) should give you a delay that's very close to 1.5 ms.
  • Lee HarkerLee Harker Posts: 104
    edited 2006-06-17 02:43
    Andy beat me to it. I was also going to mention that if you use the debug command as you showed, the time it takes to display on the screen takes a bit of time also.

    Lee Harker
  • T ChapT Chap Posts: 4,217
    edited 2006-06-17 03:12
    Ok got it. In that case, what is method to send X pulses, while varying the Pause betwee the pulses? Lets say I want to start the pulses out spaced out at 200 pulses per sec, then with a linear curve in the ramp move to 500 pulses per second, in a stepper motor application.

    Using Pause it is simple, just use:

    STEP VAR Out1
    PauseTime VAR BYTE
    Counter VAR Word
    PauseTime = 255

    RampUp:
    For counter = 1 to 255
    PauseTime = PauseTime -1
    STEP = 1
    PAUSE PauseTime
    STEP = 0
    PAUSE PauseTime
    Next
    GOTO Speed1Dir1

    Speed1Dir1:
    PauseTime = 1
    For counter = 1 to 40000
    STEP = 1
    PAUSE PauseTime
    STEP = 0
    PAUSE PauseTime
    Next
    GOTO RampDown


    etc etc

    This makes the pauses very slow at dirst on rampup, then after every pulse the pause is reduced, so that after 255 pulses, pausetime is at 1, wqhich is full spped on the stepper.

    Can something similar be written using PULSEOUT using a variable for duration?
  • edited 2006-06-17 03:23
    This takes some tuning, and each time you change your code, the timing will change too. When using a BASIC Stamp, I typically insert a toggle command into a loop so I can watch the high/low/high/low timing on an oscilloscope. Then, I tinker with the loops, PULSOUT commands to unused pins, and things like that to get the timing right.

    I don't really know of a way to get that kind of timing without that kind of tinkering. The SX·and Propeller microcontrollers both have features that will give you much more control over your stepper motor timing.
  • T ChapT Chap Posts: 4,217
    edited 2006-06-17 04:36
    This seems to be identical to using PAUSE but with smaller pauses between pulse, so it is really no different as far as programming, but very fine resolution! Thanks for the tips, I think this solves it for my app that needs less than 1ms pulses. You can see in debug the results of incrementally reducing the pause:

    COUNTER VAR WORD
    PauseTime VAR WORD

    OUT0 = 0
    main:
    counter = 0
    pausetime = 10000
    FOR counter = 1 TO 1000
    PAUSEtime = PAUSEtime -10
    PULSOUT 0, PAUSEtime
    DEBUG "-"
    NEXT
    DEBUG "done"

    END
Sign In or Register to comment.