Shop OBEX P1 Docs P2 Docs Learn Events
Checking for CNT timeout... — Parallax Forums

Checking for CNT timeout...

KyeKye Posts: 2,200
edited 2010-06-16 14:47 in Propeller 1
Mmm, I forgot how to do this...

  result := cnt
  
  repeat until(...)
 
    if((cnt - result) > (clkfreq / 200))
      .. Do stuff


So, I backup a copy of CNT in result. Then I wait until their difference is over 5ms apart... or (clkfreq / 200).

However, in spin the ">" operator is signed so this comparison does not always work... So, How do I fix this?

Thanks,

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,

Comments

  • W9GFOW9GFO Posts: 4,010
    edited 2010-06-15 18:54
    Use "||" to make (cnt-result) an absolute value.

    Rich H

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    The Simple Servo Tester, a kit from Gadget Gangster.
  • Dave HeinDave Hein Posts: 6,347
    edited 2010-06-15 19:30
    cnt - result will produce a 32-bit signed result independent of the starting value of cnt.· The difference will increase from 0 to $7fffffff, and then become negative at $80000000.· The statement if ((cnt - result) > (clkfreq / 200)) will work just fine.
  • JonnyMacJonnyMac Posts: 9,208
    edited 2010-06-15 19:39
    If you're waiting until the difference between now and when you first grabbed count, why not just wait five milliseconds with standard delay? -- or are you doing something else in between? Otherwise, as others have pointed out, make the result of now minus the cnt capture positive:

    t0 := cnt
    ...
    if (||(cnt - t0) > (clkfreq / 200)) ...
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon McPhalen
    Hollywood, CA
  • KyeKye Posts: 2,200
    edited 2010-06-15 19:41
    ||(cnt - to) works fine. Thanks,

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
  • Dave HeinDave Hein Posts: 6,347
    edited 2010-06-15 22:01
    The || is unnecessary.· It will work with or without it.
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2010-06-15 22:22
    I use a few different methods that can be helpful:
      wait := cnt       
      REPEAT
        IF ((cnt - wait) => length)
          wait += length
          '' do stuff once every [i]length[/i]
        '' continually do stuff
    


    This one allows you to do a continuously repeating process, then every length cycles something specific is executed.

    Then there is the repetitive timing loop:
      wait := cnt
      REPEAT
        '' do stuff once every [i]length[/i]
        waitcnt(wait += length)
    



    and here is the one I think you are thinking of:
      wait := cnt       
      REPEAT UNTIL ((cnt - wait) => length)
        '' continually do stuff for [i]length[/i] cycles
    



    EDIT: looks like I misunderstood the questions...oh well, that is still good information above scool.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    April, 2008: when I discovered the answers to all my micro-computational-botherations!

    Some of my objects:
    MCP3X0X ADC Driver - Programmable Schmitt inputs, frequency reading, and more!
    Simple Propeller-based Database - Making life easier and more readable for all your EEPROM storage needs.
    String Manipulation Library - Don't allow strings to be the bane of the Propeller, bend them to your will!
    Fast Inter-Propeller Comm - Fast communication between two propellers (1.37MB/s @100MHz)!

    Post Edited (Bobb Fwed) : 6/16/2010 2:30:41 PM GMT
  • ErNaErNa Posts: 1,752
    edited 2010-06-16 06:17
    Maybe, "checking for CNT timeout" points more precisely to the problem than: "Will the universe oscillate", but I think, I was talking about the same problem: http://forums.parallax.com/showthread.php?p=912500.

    The essence is: Never use : if a > CNT, always: if (a - CNT) > 0

    I hope I'm right. I didn't systematically verify that, but since I changed the If statement, my program didn't hang!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    cmapspublic3.ihmc.us:80/servlet/SBReadResourceServlet?rid=1181572927203_421963583_5511&partName=htmltext
    Hello Rest Of The World
    Hello Debris
    Install a propeller and blow them away wink.gif

    Post Edited (ErNa) : 6/16/2010 7:43:01 AM GMT
  • kuronekokuroneko Posts: 3,623
    edited 2010-06-16 09:36
    Dave Hein said...
    cnt - result will produce a 32-bit signed result independent of the starting value of cnt. The difference will increase from 0 to $7fffffff, and then become negative at $80000000. The statement if ((cnt - result) > (clkfreq / 200)) will work just fine.
    Since we're talking SPIN here you could simply shift right the result by 1 (e.g. (cnt-result)>>1), it's not really important whether you have 12.5ns or 25ns resolution (@80MHz). But at least you're safe for about 53sec unless 26sec are enough [noparse]:)[/noparse] Using || is actually dangerous as e.g. $FFFFFFFF is downsized to 1.
  • KyeKye Posts: 2,200
    edited 2010-06-16 14:47
    The difference will never be over 400,000 so there really is no problem.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
Sign In or Register to comment.