Shop OBEX P1 Docs P2 Docs Learn Events
Storing Negative Numbers — Parallax Forums

Storing Negative Numbers

chaching001chaching001 Posts: 2
edited 2011-03-12 12:23 in General Discussion
Hi-
I'm working with the 3D accelerometer 7455, and having issues storing the negative values. I used the online template program MMA7455L 3-Axis Accelerometer Demo (PBasic) v1.0 DEMO#1.bs2 to start, and have edited the code around so that I can store the values, and then when prompted to display them. I'm aware of the SDEC command, and have implemented it in the DEBUG command when I want to display them. But I think my issue is in the WRITE command. I'm sending the acceleration values into a write command to store, but I don't know if the negative's are correctly storing, because when I later extract them and DEBUG them, all my numbers are positive, and I'm pretty sure the values that are supposed to be -1, -2, ... etc are 255, 254...

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2011-03-10 11:56
    Yes, if you take negative numbers and store them in an 8-bit location, you'll get 255 for -1, 254 for -2, etc. The Stamps normally store negative values as 16-bit numbers, so -1 and 65535, -2 and 65534, etc. are equivalent. Read the Wikipedia article on 2's Complement for details. If your values range from -128 to +127, you can store them in a byte with WRITE. When you READ them back into a word variable like "value", do an "IF value > 127 THEN value = value - 256".
  • chaching001chaching001 Posts: 2
    edited 2011-03-11 09:36
    Thank you soo much!! I added the IF-THEN statement like you recommended, and I see minus signs!!!
    Thanks again. It really means a lot!
  • train nuttrain nut Posts: 70
    edited 2011-03-12 12:23
    I have been using the MMA7455L 3-Axis Accelerometer but in the 10bit mode. In this mode the sign bit is returned in bit 1 or the second byte and if you use the demo as written it does a sign extend but with the sign in bit 9 sign extend does not work. you have to physically shift the second byte six bits to the left and the rotate it back to get the sign correct. Hope this is helpful to anyone using 10bit mode.

    Ben
Sign In or Register to comment.