Shop OBEX P1 Docs P2 Docs Learn Events
Problems using SDEC — Parallax Forums

Problems using SDEC

DougHubDougHub Posts: 2
edited 2013-01-06 19:24 in General Discussion
I'm using the HomeWork Board USB.
After loading and running the following program, I find 255 displayed when ever a -1 should be displayed. The line: "ATN (", SDEC xCoord, ", ", SDEC yCoord, ")" is placing 255 when xCoord or yCoord is equal to -1.
What am I doing wrong?

Thanks!

' {$STAMP BS2}
' {$PBASIC 2.5}

idx VAR Nib ' loop counter
xCoord VAR Byte ' x coordinate of vector
yCoord VAR Byte ' y coordinate of vector
brads VAR Word ' angle in brads
degr VAR Word ' angle in degrees

Main:
FOR idx = 0 TO 7 ' load vector data

LOOKUP idx, [ 1, 1, 0, -1, -1, -1, 0, 1], xCoord
LOOKUP idx, [ 0, 1, 1, 1 , 0, -1, -1, -1], yCoord

brads = xCoord ATN yCoord ' get angle of vector
degr = brads * 180 / 128 ' convert to degrees

DEBUG DEC (idx + 1), ": ",
"ATN (", SDEC xCoord, ", ", SDEC yCoord, ")",
CRSRX, 18, "= ", DEC brads, " (", DEC degr, ")", CR
NEXT
END

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2013-01-06 19:17
    Your problem is that bytes are always unsigned and have values from 0-255, so SDEC and DEC behave the same for byte values. If you store a -1 in a byte, it will be stored as 255, not -1. You can either use words to store xCoord and yCoord or you can use (xCoord << 8) / 256 instead of xCoord and that should convert the (signed) byte value to a signed word value for display.
  • DougHubDougHub Posts: 2
    edited 2013-01-06 19:24
    Thanks for the explanation Mike! Problem solved.
Sign In or Register to comment.