Shop OBEX P1 Docs P2 Docs Learn Events
Code speed for BS2 (highest frequency digital IO pin write) — Parallax Forums

Code speed for BS2 (highest frequency digital IO pin write)

rkremserrkremser Posts: 4
edited 2010-01-08 20:13 in BASIC Stamp
So I need to create a square wave of which i can send a specific number of pulses I need this code to be as fast as possible.
I am trying to determine how quickly a full cycle would be (From peak to peak)



I don't have my scope here with me and i was hoping someone could either let me know the time or suggest a more efficient way


count = 250
DO UNTIL count=0
High 1
low 1
count = count -1
Loop

Comments

  • allanlane5allanlane5 Posts: 3,815
    edited 2010-01-08 15:57
    http://www.emesystems.com/BS2speed.htm

    This is the best summary of execution times.

    Post Edited (allanlane5) : 1/8/2010 4:22:19 PM GMT
  • rkremserrkremser Posts: 4
    edited 2010-01-08 16:18
    so If i get this right the following will take 900 miliseconds or will go peak to peak at 1.1khz

    word steps
    steps = 400

    steploop 'Start timing here
    high1
    steps=steps-1
    low1
    if steps >1 steploop
  • allanlane5allanlane5 Posts: 3,815
    edited 2010-01-08 16:23
    Well, actually, 900 MICRO-seconds, or 1.1 Khz, but yes.

    And that's for a BS2 "plain". A BS2sx will go twice as fast.
  • ercoerco Posts: 20,256
    edited 2010-01-08 17:59
    Just looping back takes time. High-low is very fast. If you can live with a slightly irregular pulse output, have several high-low cycles within your loop. Toggle works just as well.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"If you build it, they will come."
  • rkremserrkremser Posts: 4
    edited 2010-01-08 18:40
    I didn't think of that, it should drastically speed up the seeking. I am using this to drive a stepper motor driver (step & dir inputs) and as much code as it will take up i'll probably setup a 1, 10, 50, 100 pulse subroutines and go about it that way.

    At least the bulk should then run at 4.1Khz with some pauses calling the subs.
  • allanlane5allanlane5 Posts: 3,815
    edited 2010-01-08 18:58
    Note that this means the BS2 runs at about 2000 Basic Instructions Per Second. For a processor, this is very slow. For a human, this is very very fast. And the simplicity of programming and robustness of the platform make up for the speed in like 90% of applications.

    However, sometimes you really do need the speed. In which case the SX-28 and SX-48 platforms provide 50 MIPS -- you might look at those boards from Parallax. AND there's the 'Propellor' processor, which also runs in real-time.

    Just a thought.
  • rkremserrkremser Posts: 4
    edited 2010-01-08 19:12
    Ya I have used Pic's before and obviously they are much faster, just the development time is so much longer in assembly than pbasic. I may end up going to the PIC but for the time being i'm just trying to avoid it.
    Very good point though, This may just be to much for the stamp.
  • ercoerco Posts: 20,256
    edited 2010-01-08 20:04
    If you're driving a stepper motor with these pulses, isn't there going to be an upper limit on how fast your pulses can be? And isn't that limit dependent on the application and the load?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"If you build it, they will come."
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2010-01-08 20:13
    The loop can be slightly faster using DO:LOOP WHILE ...
    steps=400
    steploop: 'Start timing here
    DO WHILE steps
      HIGH 1
      steps=steps-1
      LOW 1
      LOOP
    



    Note that a single TOGGLE in the loop makes the loop run faster, but each toggle is only 1/2 cycle, so the overall frequency is less.

    There are a couple of ways to make the Stamp generate faster pulse sequences, if the output can tolerate some jitter.
    Adat CON $AAAA
    SHIFTOUT 2,  1, 1, [noparse][[/noparse] Adat\16,Adat\16,Adat\16,Adat\2]
    


    generates 50 clock pulses on pin p1, and 25 data pulses on pin p2. You could use either one, but the p2 pulses would be more square-like. The data pulsewidth on a BS2 is about 60 microseconds and the space between groups of 16 is about 240 microseconds (caveat--old measurements).

    PWM 1, 100, 1 :LOW 1
    


    generates 100 pulses of 4.5 microseconds width in a total period of 1.15 milliseconds.
    And for counted pulse streams of 1 to 128 pulses in the same 1.15 millisecond frame.
    steps=82  ' 1 to 128 only
    PWM 1, steps, 1 : LOW 1
    


    The last one stands for the number of cycles, so to get 200 in 2.30 milliseconds:
    PWM 1, 100, 2 : LOW 1
    


    The LOW is necessary because PWM leaves the pin an input.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com

    Post Edited (Tracy Allen) : 1/8/2010 8:19:57 PM GMT
Sign In or Register to comment.