Hi ,what is the best way to get a time delay of 5 minutes.I looked in the book but still not clear. I've tried waitcnt(clkfreq*300+cnt) ....this comes up way short. Thanks-mike
The longest delay waitcnt can provide is about 56 seconds (2^32 clocks) at 80MHz. What you need to do is put a shorter wait in a repeat loop. I would suggest:
repeat 300
waitcnt(clkfreq + cnt) ' one second delay
This will be very close to a 5 minute delay, but slightly more since it does not take execution time into account.
PRI delay5Minutes | time
time := cnt
repeat 300
waitcnt(time += clkfreq) ' delay 1 second
This works by waiting for one second beyond the current time. Since all time references are relative to the initial reference to cnt, any overhead in the loop is absorbed into the wait.
Comments
This will be very close to a 5 minute delay, but slightly more since it does not take execution time into account.
PRI delay5Minutes | time time := cnt repeat 300 waitcnt(time += clkfreq) ' delay 1 secondThis works by waiting for one second beyond the current time. Since all time references are relative to the initial reference to cnt, any overhead in the loop is absorbed into the wait.CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 OBJ pst : "Parallax Serial Terminal" DAT seconds long $00 VAR long stack[20] PUB Main pst.Start(115_200) pause(500) cognew(SecondCounter, @stack) repeat pst.dec(seconds) pst.char(13) if(seconds == 20) 'Do something SetSeconds(0) pause(1000) PUB GetSeconds return seconds PUB SetSeconds(value) seconds := value PUB SecondCounter repeat pause(1_000) seconds++ PRI pause(Duration) waitcnt(((clkfreq / 1_000 * Duration - 3932) #> 381) + cnt) return