How to loop exactly 1 second?
william chan
Posts: 1,326
Hi,
I need a loop that updates the second and blinks one LED accurately.
My code
dira[noparse][[/noparse]led_pin]~~ 'Set I/O pin to output direction
repeat
rtcsecs++ ' Increment RTC time
if rtcsecs > 59
rtcsecs := 0
rtcmins++
!outa[noparse][[/noparse]led_pin] ' Toggle I/O Pin
waitcnt(clkfreq + cnt) ' Delay 1 sec cycles
I know this code will run a bit slower than real time.
How do I make it loop exactly 1 second per loop?
Thanks.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
www.fd.com.my
www.mercedes.com.my
I need a loop that updates the second and blinks one LED accurately.
My code
dira[noparse][[/noparse]led_pin]~~ 'Set I/O pin to output direction
repeat
rtcsecs++ ' Increment RTC time
if rtcsecs > 59
rtcsecs := 0
rtcmins++
!outa[noparse][[/noparse]led_pin] ' Toggle I/O Pin
waitcnt(clkfreq + cnt) ' Delay 1 sec cycles
I know this code will run a bit slower than real time.
How do I make it loop exactly 1 second per loop?
Thanks.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
www.fd.com.my
www.mercedes.com.my
Comments
This way the WAITCNT always waits for exactly one second from the last wait regardless of what has to be done after the wait.
Post Edited (Mike Green) : 3/3/2008 4:06:23 AM GMT
I personally like separating the pacing section of a loop from the logic sections. This makes it easier to add other functions into the scan/timing loop. (a good reason to count 1/100th of a second [noparse]:)[/noparse] )
Marty
p.s. Mike posted a shorter way of doing the same thing above. He must've been programming is something other than spin recently 'cause ":=" is the assignment operator in spin, "=" is a conditional only
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Lunch cures all problems! have you had lunch?
Post Edited (Lawson) : 3/3/2008 4:05:17 AM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker
Propeller Applications Engineer
Parallax, Inc.
Yes - just you can do other things too.
Perhaps I should of said "Or another option would be..." - my post sounds a bit like Mike bashing....
J
That way you would have the COG free to do whatever else you wanted to do as long as you didn't use that counter for anything else.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Beau Schwabe
IC Layout Engineer
Parallax, Inc.