Spin CNT Help Needed
hippy
Posts: 1,981
I am having no end of trouble trying to get my head round solving this, so any help would be gratefully received ....
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.
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
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?
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
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"