View Full Version : Timing issue

11-11-2010, 01:25 PM
I'm having a small issue with a timer. This is a snippet of the code below and the following is happening. Everything is in spin.

When the pll16x is used, the first time through the loop takes 2 seconds and all subsequent times takes a second. If I change to pll2x or lower, it times correctly. What am I missing?

Best regards,

_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000


clock := cnt 'Get the current clock for accuracy as the instructions take time to run.
load_array(minutes,tens,sec) 'Minutes, Tens, Sec are set from the button COGS
send_frame 'Send it....
case sec 'Now we're going to count down. Case statements I thought were the easiest to use.
0: 'Seconds at zero...are we at zero?
if (minutes | tens) 'Nope...Sec wraps back to 9
sec := 9 '
case tens 'check Tens....
0: tens := 5 'Zero?....Tens wrap back to 5
if minutes > 0 'Minutes are decremented if
minutes-- 'greater than Zero
other: if tens > 0 'and then....tens are decremented...
tens-- 'if greater than Zero

other: sec-- 'if sec is anything else.....decrement it.

waitcnt(clkfreq + clock) 'Wait for the rest of the second to pass.....

while (minutes | tens |sec)

11-12-2010, 02:26 PM
Your waitcnt line should be:

waitcnt(clock += clkfreq)

-- this will provide the synchronization you're looking for.

11-12-2010, 02:38 PM
Thanks Jon. I shall try that. I thought it must have been some sort of timing issue and I am not schooled enough in spin to have figured it out yet. Best regards.


11-12-2010, 05:55 PM
I've attached a little demo that may be helpful. It has a method called runtimer() that can be called normally (will "block" program until done) or as a "background" process via cognew().

11-13-2010, 10:48 AM
Odd. Still didn't work. I used the serial terminal and displayed 'minutes' before and after the case statement. Before, it showed 3 and after it showed 2 as expected, but the second time through the loop it showed 3 before the case statement and 2 after again. Subsequent times through the loop, it operated as expected.

I have minutes, tens, and sec set by another cog. This other cog simply monitors a button and debounces it. If pushed, minutes, tens, and sec are set. Strange that it all works if the pll is set to 2x or 1x. I'll keep at it till I find out what's happening.


11-15-2010, 12:13 AM
I got it fixed but I didn't figure it out. I segregated all the timing loops in its' own cog and it worked perfectly. When the timing loop was in the main body, it would always take 2 seconds on the first tick if it received it's value from another cog. If I hard coded the minute value it worked fine. Not sure why. Nothing succeeds like success.

11-15-2010, 03:08 PM
It would be easier for others to assist if you posted your whole program. I, too, find it strange that you can make it work in one cog but not another.

11-18-2010, 02:11 PM
Thanks Jon,
The entire program is posted in the completed projects forum. It's the original iteration of the program so I'm sure the problem would be obvious to the experts. To fix the problem, I essentially placed the case statements loop in a cog of it's own and and the main loop simply displayed the numbers for the digits. It worked as I wanted and the additional benefit was that the user could select the color they wished while the display sat at 0:00 instead of waiting for the count to commence. We're using in my air traffic control tower now and the controllers are pleased with it's ease. Thanks for your input.