Shop OBEX P1 Docs P2 Docs Learn Events
Counter Problems — Parallax Forums

Counter Problems

StevenGStevenG Posts: 9
edited 2010-05-18 16:45 in BASIC Stamp
I'm sure someone has run into this problem before...

I am making a simple quadrature reader that reads the current position from the Parallax Position Controller Kit (#27906), calculates the number of "actual" units and sends it out the COM port. It does this once a second using a DS1302 time keeping chip to ensure that it is as consistent as possible.

The problem I am having is this... Going positive everything works beautifully. But going negative when DepthR crosses the zero line and goes to 65535 everything gets screwed up.

If anyone has some experience in this or a better way of doing it your help would be much appreciated!!

Code attached...

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-05-10 14:16
    The Stamps do all their arithmetic in unsigned 16-bit arithmetic. Although 65535 is the same as -1 in signed 2's complement 16-bit arithmetic, that's not how the Stamps handle it. For example, multiplication and division are unsigned. Addition, subtraction, and comparisons will work as long as both operands are considered signed or unsigned and you respect the maximum signed or unsigned values that can fit in 16 bits. For input and output, there are separate "formatters". DEC works for unsigned values and SDEC works for signed values. See their descriptions in the Basic Stamp Syntax and Reference Manual for details. Remember that 16 bits can be used to represent values from 0 to 65535 or from -32768 to 32767.
  • Martin_HMartin_H Posts: 4,051
    edited 2010-05-15 18:37
    Mike, I've have some problems with signed comparisons in PBASIC, but not math. For example:

    X = -1

    IF ( X < 0 ) THEN
    DEBUG "X is less than zero", CR ' This should print but doesn't
    ENDIF

    But this works

    IF (X.BIT15 = 1) THEN
    DEBUG "X's signed bit is set", CR ' This does print.
    ENDIF

    Now my code works, but directly testing the signed bit is awkward. Is there syntax in the first case that I'm missing?

    I've had similar issue adding signed bytes to signed words, but worked around that by copying the byte to a word, sign extending it, and then adding the values.
  • Mike GreenMike Green Posts: 23,101
    edited 2010-05-15 20:14
    The problem you're running into is that -1 is negative while 0 is positive and you can't use an unsigned comparison to correctly compare a negative number with a positive number.
  • Martin_HMartin_H Posts: 4,051
    edited 2010-05-15 23:42
    Thanks Mike. I guess that's what is confusing me. In two's compliment the constant 0 is the same signed or unsigned. Since zero never has a sign, it is the type of the variable that determines the collating sequence. For example, C requires you specify that a type is either signed or unsigned, and then the compiler takes into account the sign bit. But as near as I can tell PBASIC only has Word, not Signed Word. So sign management is the coder's responsibility.

    I'm fine with that, but I wanted to make sure I wasn't missing some key language feature.
  • sumdawgysumdawgy Posts: 167
    edited 2010-05-18 16:45
    Ah.
    This is why "x=x-1 \\ 0" casues unreliable results.

    I never did understand why it occasionally "lit all bits" and maxxed out insteadsof zeroing.
    The signed word trips up the operation....

    Thanks mike.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Before you criticize someone, walk a mile in his shoes. That way if he gets angry, he'll be a mile away and barefoot. - unknown
Sign In or Register to comment.