Shop OBEX P1 Docs P2 Docs Learn Events
time delay — Parallax Forums

time delay

mikeamikea Posts: 283
edited 2013-01-20 11:22 in Propeller 1
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

Comments

  • kwinnkwinn Posts: 8,697
    edited 2013-01-20 11:10
    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.
  • mikeamikea Posts: 283
    edited 2013-01-20 11:13
    Understood, thank you kwinn-mike
  • Mike GreenMike Green Posts: 23,101
    edited 2013-01-20 11:19
    A more accurate scheme would be:
    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.
  • Mike GMike G Posts: 2,702
    edited 2013-01-20 11:22
    Here's another way using a cog as a timer and a global variable
    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
    
Sign In or Register to comment.