Search code examples
caccelerometer

Data from accelerometer


I am using BMA 220 and its an 8 bit sensor. In the datasheet it is been given that incoming data is in 2's complement form. so what i did in my application program is i checked the sign bit. If it is 1 then its a negative data and i need to make a conversion as ---> data^ff+1. Also in the datasheet its given that the sensitivity of the sensor in 2g mode is 16LSB/mg So when I get the data after doing data^ff+1, should I multiply it with 1/16?

I checked the sign bit as

          if ((data & 0x80)? 1:0)

and decomplementation

            data^0xff+1

Solution

  • The first thing I would note is that the logic in your if statement is redundant.

    if ((data & 0x80)? 1:0)
    

    is equivalent to the same statement without the ternary operator.

    if (data & 0x80)
    

    Also I would suggest avoiding the 'magic number' 0x80 in your code, and instead use a named constant instead, for example a macro like this:

    #define SIGN_BIT (0x80)
    /* some code */
    if (data & SIGN_BIT)
    

    And, for readability and to avoid precedence pitfalls, I would suggest using parentheses in your two's complement

    (data^0xff) + 1
    

    And maybe you should check for overflows, if data is of a type larger than 8 bits.

    ((data ^ 0xff) + 1) & 0xff
    

    Now, concerning your actual question, I think it depends what you want to do with the data. If you need the actual values for calculation of other physical data, then I would suggest normalizing the value as the datasheet suggests, however I think I would multiply by (1.0/16) or divide by (16.0) because this will implicitly cast your values to a floating point type, and avoid the loss of information that is inevitable if you use pure integer division.

    However, If you need the accelerometer data purely for comparison to other accelerometer data, for example if you were to develop a PID controller for something that should balance, there is no need to normalize the values.

    It all depends on your application.

    UPDATE: some more information on accelerometers can be found here: http://bildr.org/2011/04/sensing-orientation-with-the-adxl335-arduino/