Shop OBEX P1 Docs P2 Docs Learn Events
Spin CNT Help Needed — Parallax Forums

Spin CNT Help Needed

hippyhippy Posts: 1,981
edited 2007-09-16 17:28 in Propeller 1
I am having no end of trouble trying to get my head round solving this, so any help would be gratefully received ....

  timeout := ( clkfreq / 1000 ) * mS + CNT
  repeat
    if timeout > CNT
      NotTimedOut
    else
      TimedOut



It will work fine if 'timeout' and CNT are positive, but it does not handle all cases of CNT wrapround and overflows. The solution maybe obvious, but I cannot see it.

Comments

  • LawsonLawson Posts: 870
    edited 2007-09-16 04:41
    how about testing for
    (timeout - CNT) > 0
    


    the subtraction should be more tolerant of overflows than the greater than test.


    My 2 cents
    Marty

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Lunch cures all problems! have you had lunch?
  • deSilvadeSilva Posts: 2,967
    edited 2007-09-16 06:03
    This is how it works:
    + 2^31  <------- C -------- 0 -------- TOUT -- > -2^31
            ..>       ..........>              .....
    


    Starting with: timeout > CNT.
    subtracting "CNT" from both side gives "Lawson's formula"

    You see in the diagram, that this "moves" CNT to the center = 0, now "timeout" can easily be found
    either at the + (still time left) or the - (time out!) side.
    This works as long as ||(timeout-CNT) < 2^31 ticks.

    Post Edited (deSilva) : 9/16/2007 6:09:44 AM GMT
  • hippyhippy Posts: 1,981
    edited 2007-09-16 09:02
    Thanks both, it took a bit of paper working to convince myself that would work, but I believe that solves the problem.
  • deSilvadeSilva Posts: 2,967
    edited 2007-09-16 11:13
    hippy said...
    Thanks both, it took a bit of paper working to convince myself that would work, but I believe that solves the problem.
    I intended to save you the paperwork through my diagram smile.gif
    An even better diagram would show a circle where +/- 2^31 meet,opposite to zero - so a subtraction (or addition) is a "turn of that wheel"
  • hippyhippy Posts: 1,981
    edited 2007-09-16 17:28
    deSilva said...
    I intended to save you the paperwork through my diagram smile.gif
    And it was appreciated. It wasn't that your answer caused any problems, just that I needed to to do the working out to make myself understand and assure myself that the theory held up to practice. Thanks, it does.
Sign In or Register to comment.