Code Speedup?
RiJoRi
Posts: 157
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?
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...
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...
bs2
1K
Comments
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:
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.
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