how do I loop for x seconds?
in Propeller 1
hey guys,
how do I loop a function for a certain number of seconds?
Thanks!
how do I loop a function for a certain number of seconds?
Thanks!

Comments
ticks := clkfreq * Nseconds + cnt ' the system clock at N seconds in the future ' provided the cnt <2^32, ~(53/2) seconds at clkfreq=80MHz repeat ' do whatever if cnt - ticks > 0 ' wait that number of seconds with uncertainty for the whatever code quit2. repeat the loop 1_000 or 1_000_000 divided by the know loop time.
If x < 27, use this:
Otherwise, this:
t0 := cnt repeat x repeat until (cnt - t0 => clkfreq) 'Do stuff for x seconds t0 += clkfreqIn both cases, it's assumed that "do stuff" doesn't take very long to perform.
-Phil
-Phil
Um, if ints are signed, that won't work. Also, even if ints are unsigned, that code is only good for times less than 54 seconds.
-Phil
The first one with x < 27 seconds:
int t0 = CNT; while (CNT - t0 < x * CLKFREQ) { //Do stuff for x seconds }I needed to keep track of the time and then do something and I needed multiple timers to do it so I wrote a library function for it.
int micros(unsigned long *D) { long t; t = CNT + 280 - *D; *D = CNT; if (t < 0) t = UINT32_MAX + t; *D = *D - t % us; t = t/us; return t; } int millis(unsigned long *D) { long t; t = CNT + 280 - *D; *D = CNT; if (t < 0) t = UINT32_MAX + t; *D = *D - t % ms; t = t/ms; return t; }Buy passing in a long variable you can keep track of multiple time instances.
unsigned long PCNT; unsigned long P2CNT; int main() { int sec, sec2; int d1, d2; micros(&PCNT); millis(&P2CNT); sec = 0; sec2 = 0; d1 = 0; d2 = 0; while(1) { d1 += micros(&PCNT); d2 += millis(&P2CNT); if (d1 > 1000000) // wait a second { d1 = d1 - 1000000; print("Timer: %d, %d\n", d1, sec++); } if (d2 > 1000) // wait a second { d2 = d2 - 1000; print("Timer2: %d, %d\n", d2, sec2++); } } }You can also use the mstimer.h library that starts a cog with the time in it.
Mike