Shop OBEX P1 Docs P2 Docs Learn Events
How Would you count a greater value than 65535 Please give a very simple examp — Parallax Forums

How Would you count a greater value than 65535 Please give a very simple examp

sam_sam_samsam_sam_sam Posts: 2,286
edited 2009-09-01 02:11 in Propeller 1
·I know that this question is very simple for most of you but..........

I·know that with P Basic you would

do this to count

count = count + 1

and you only count 65535 and then it would roll over

and have· another counter start when the first one got to 65534 then start your next counter

The only thing that I have done with the Propeller is the few being example when this chip first came out I would like to learn more about programing this chip as well



▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

·
·
·
·
Sam

Comments

  • BradCBradC Posts: 2,601
    edited 2009-08-30 16:38
    sam_sam_sam said...
    I know that with P Basic you would



    do this to count



    count = count + 1



    and you only count 65535 and then it would roll over

    The propeller won't roll over until you get to 4294967295 as it counts in 32 bits natively rather than 16. Just declare your counter variables as LONG rather than WORD or BYTE.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    lt's not particularly silly, is it?
  • AribaAriba Posts: 2,690
    edited 2009-08-30 18:02
    With SPIN you would do this to count:
    count := count + 1
    
    


    or this:
    count += 1
    
    


    or this:
    count++
    
    


    or this:
    ++count
    
    


    All this variations increment the variable count by 1. If count is a long-variable (=32 Bit) , then you can count up to 2_147_483_647, then the value rolls over to -2_147_483_648 (That is because SPIN handles longs as signed variables).

    If you need to count higher then use more than 1 variable and count the roll-overs in a second count variable:
    count1 := count1 + 1
      if count1 == 2_147_483_647
        count2 := count2 + 1
        count1 := 0
    
    


    this can also be written like this:
    if ++count1 == POSX
         count2++
         count1~
    
    



    Andy
  • James NewmanJames Newman Posts: 133
    edited 2009-08-30 18:43
    If you need more than that, ASM add and subract routines can use the overflow bit from a previous operation, allowing you to chain them together for a value as big as you need. Look around the forum, I remember a post about 64 bit numbers that demonstrated it.
  • localrogerlocalroger Posts: 3,452
    edited 2009-08-30 19:00
    There's actually a more economical way to do multiword count in Spin:

    if not (++count)
    counthiword ++

    This works for both signed and unsigned numbers, as long as you treat counthiword as the 2's complement extension of count. This works by (1) incrementing count, (2) logical NOT converts any nonzero value to zero and only zero to -1, and if it's -1 (3) carries to the hiword.
  • mparkmpark Posts: 1,305
    edited 2009-08-31 09:41
    Possibly slightly more economical:
    ifnot ++count
      counthiword++
    
    


    Saves you a not operation.
  • localrogerlocalroger Posts: 3,452
    edited 2009-08-31 12:08
    @mark, I totally forgot about ifnot. Spin is deeper then Loch Ness, I tells ya.
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2009-08-31 23:46
    I want to thank every one that replayed to this post

    Arida

    Can·you help little more with this in explain what is the different between each one that you have here in the way that it would count
    count := count + 1
    



    or this:
    count += 1
    
    


    or this:
    count++
    
    


    or this:
    ++count
    
    

    or this

    ·localroger and mpark

    ··············· if not (++count)
    ··············· counthiword ++

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 8/31/2009 11:54:10 PM GMT
  • localrogerlocalroger Posts: 3,452
    edited 2009-09-01 00:30
    @sam: As standalone statements, all of the code-bracketed examples you give have exactly the same effect. The only difference is with count++ vs. ++count, if you use that as the ARGUMENT for another function (such as p := count++ or p := ++count) count++ returns the value BEFORE the ++ increment, while ++count returns the value AFTER the ++ increment. Both leave count one higher after the operation.

    What the ifnot thingie does is it checks to see if the result of the ++count is zero, which means it has rolled over; just as if you are adding in a one-digit decimal register and you get 7, 8, 9, 0 ... that 0 means there should be a 1 added to the tens. So by checking ++count for zero we are asking, did we just roll over? And if the answer is yes, we add 1 to the next higher "digit." That gives you 64 bit counts (and more if you extend the idea). And that can be a useful thing when the CNT register overflows after 2 minutes or so at 80 MHz.
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2009-09-01 00:43
    localroger

    Thank you for exlplaine this to me that helps a Lot· smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them smile.gif

    ·
    ·
    ·
    ·
    Sam
  • Ole Man EarlOle Man Earl Posts: 262
    edited 2009-09-01 02:11
    get more fingers !
Sign In or Register to comment.