Shop OBEX P1 Docs P2 Docs Learn Events
Timing in SPIN — Parallax Forums

Timing in SPIN

K2K2 Posts: 693
edited 2011-06-13 12:40 in Propeller 1
The following code snippet is obviously flawed. How is this fixed in SPIN without using something clunky like WAITCNT, that doesn't let the cog do anything else until the timed interval is up?

  temp := 10_000_000 + cnt                       
  repeat
    if cnt => temp                                     
      temp += 10_000_000
      (do stuff)
    (do other stuff)

Comments

  • Mark_TMark_T Posts: 1,981
    edited 2011-06-13 12:28
    I presume you are meaning the comparison doesn't work when cnt wraps round to negative values?

    The correct way is to compare a difference, as in
      if  cnt - temp >= 0
         'do stuff
    
    This will work so long as the delay is less than 2^31
  • K2K2 Posts: 693
    edited 2011-06-13 12:40
    Thanks! Looks simple...now. :)
Sign In or Register to comment.