Shop OBEX P1 Docs P2 Docs Learn Events
And still this problem with assembler and overflow — Parallax Forums

And still this problem with assembler and overflow

ErNaErNa Posts: 1,753
edited 2010-09-28 22:52 in Propeller 1
Again and still I face this problem:

I want to make a timeout, and that looks like this:

TimedOut is the time to test
MaxDura is the duration of one period
Every time, TimeOut elapses, TimeOut is incremented by MaxDura
Now I compare TimedOut to the counter, whenever the counter exceeds TimeOut, I signal that event

Running in a loop:
        cmp       TimedOut,  Cnt        wc       ' see if timeout elapsed
if_c    add       TimedOut, MaxDura             ' next timeout time
if_c    wrlong    MaxDura,  pVsp_CnPtr          ' store duration to global

The problem comes from when the TimedOut wraps around, because now TimedOut is small and Cnt still big.
Is there an elegant solution? Thanks in advance

Comments

  • LukeHLukeH Posts: 22
    edited 2010-09-28 13:44
    CNT will wrap around too. What is going wrong?
  • JonnyMacJonnyMac Posts: 9,208
    edited 2010-09-28 13:49
    I built a time-out feature in my DMX receiver object. The way I did it was by setting up one of the counters to free run. As long as DMX data is coming in the counter is cleared and the DMX RX LED is lit. If it ever reaches the timeout point (counts in 100ms) then the DMX RX LED gets extinguished.

    It's simple, and may work for you.
  • AribaAriba Posts: 2,690
    edited 2010-09-28 14:15
    ErNa

    You need a temporary register to calculate the difference of TimedOut and cnt first.
    The difference is always correct, because both are 32bit registers, only the carry (borrow) is lost.
    Then you can decide if cnt or TimedOut is higher by comparing to 0.
    mov       t1, TimedOut                  ' calculate difference
            sub       t1, Cnt
            cmps      t1, #0                wc      ' see if timeout elapsed
    if_c    add       TimedOut, MaxDura             ' next timeout time
    if_c    wrlong    MaxDura,  pVsp_CnPtr          ' store duration to global
    

    Andy
  • ErNaErNa Posts: 1,753
    edited 2010-09-28 22:52
    @Andy: du bist ein Schatz! Yes, that's it. I had this idea of calculating the difference due to my SPIN experience, but didn't realize, that only the cmps to 0 generates the right carry bit! Many thanks, I just had to enter this one line of code tomorrow and it worked ! Great forum.

    @LukeH: Yes, you are right, the cnt will also wrap, but there is still a gap, because duration wraps first and during this duration time, a small value of duration is compared to the high cnt value and this gives a wrong result. The way Andy's code solves this task works perfectly, as long as duration is less than 31 bit.

    Have a great day. ErNa
Sign In or Register to comment.