Shop OBEX P1 Docs P2 Docs Learn Events
Code Speedup? — Parallax Forums

Code Speedup?

RiJoRiRiJoRi Posts: 157
edited 2009-06-10 13:12 in BASIC Stamp
I'm trying to make a pulse generator to make pulses from 1mSec to 1000 Sec. using 10 switches for the pattern. Using the switches, I can get a 1010101010 pattern, or a 1010010000 code, or any other combination.

The Seconds portion works well, as does most of the miiliSec portion. The problem is that the mSec timing has a 5millisecond overhead. I can adjust the counts for 10mSec & greater by subtracting 5 from the counts, but what do I do for 1, 2, and 5 mSec?

Delay:
   LOOKUP Freq, [noparse][[/noparse]  1,2, 5, 10,20,50, 100,200,500, 1000],DelayAmt
'  Approx. Times:   6 7 10 15 25 -- for millisecs
   IF Speed = 1 THEN
       DO ' seconds
           PAUSE 1000
           DelayAmt = DelayAmt - 1
       LOOP UNTIL DelayAmt = 0
   ELSE
       PAUSE DelayAmt
   ENDIF
   RETURN




I'm not sure if nested IF-THENs or SELECT-CASE would be speedier; moving the subroutine into the Main loop didn't.

--Rich

Full code is attached...

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-06-09 19:09
    Look in www.emesystems.com at the link "app-notes". There's a section on execution time of Stamp statements.

    You have a problem for the 1, 2, and 5ms timings since the execution time of statements is on the order of 10% of that. One way to handle this is to switch to a faster Stamp model like the BS2p or BS2px. Another way is to use the PULSOUT statement to generate the pulse. That works ok for one side of the pulse (change from baseline). You also have to accurately time the time between pulses. You can use another PULSOUT to a dummy I/O pin that you don't otherwise use. The PULSOUT is quite accurate and has a resolution of 2us on the BS2. You would use a simple loop like:
    FOR count = 1 TO maxCount
       PULSOUT ioPin,onTime
       PULSOUT dummy,offTime
    NEXT
    


    You'd adjust offTime to account for the execution time of the FOR/NEXT statement and the 2nd PULSOUT. You'd adjust onTime to account for the execution time of the 1st PULSOUT statement.
  • RiJoRiRiJoRi Posts: 157
    edited 2009-06-10 13:12
    Thanks, Mike!
    I had not thought of using a dummy pin. I'll see what happens, but it's a good trick to keep in mind for more precise timing.

    --Rich
Sign In or Register to comment.