Shop OBEX P1 Docs P2 Docs Learn Events
Timing issue — Parallax Forums

Timing issue

WebheadFredWebheadFred Posts: 27
edited 2010-11-18 07:11 in Propeller 1
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,
Fred



con
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000

.
.
.
.


repeat
    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)

Comments

  • JonnyMacJonnyMac Posts: 9,208
    edited 2010-11-12 07:26
    Your waitcnt line should be:
    waitcnt(clock += clkfreq)
    

    -- this will provide the synchronization you're looking for.
  • WebheadFredWebheadFred Posts: 27
    edited 2010-11-12 07:38
    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.

    Fred
  • JonnyMacJonnyMac Posts: 9,208
    edited 2010-11-12 10:55
    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().
  • WebheadFredWebheadFred Posts: 27
    edited 2010-11-13 03:48
    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.

    Regards,
    Fred
  • WebheadFredWebheadFred Posts: 27
    edited 2010-11-14 17:13
    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.
  • JonnyMacJonnyMac Posts: 9,208
    edited 2010-11-15 08:08
    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.
  • WebheadFredWebheadFred Posts: 27
    edited 2010-11-18 07:11
    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.

    Regards,
    Fred
Sign In or Register to comment.