0
votes

I'm using a Synapse wireless module which has a ATMEL ATmega128 on it. I've connected a ADXL345 3 axis accelerometer to it and I'm communicating with it via I2C. The Synapse module uses a subset of python as the scripting language.

When I read the XYZ data I get back an array of 6 bytes. EC FF EF FF 14 01

The output data is twos complement with DATAx0 as the least significant byte and DATAx1 as the most significant byte. So in the example above, EC FF is the output for the x axis.

I'm struggling with how to convert this into G force. The datasheet says in full resolution mode (which I'm in) the output resolution increases with the g range set by the range bits to maintain a 4 mg/LSB scale factor.

I've tried the following:

x0 = ord(data[0])
x1 = ord(data[1])
raw = x0+(256*x1)

but the value of raw doesn't make any sense. How do I convert these two bytes into a signed integer?

Additional Information When I make the call to read the adxl345:

data = readAdxls(6)

it returns the string of bytes read back from the external I2C device.

Xlsb = data[0] # EC - 236
Xmsb = data[1] # FF - 255
Ylsb = data[2] # EF - 239
Ymsb = data[3] # FF - 255
Zlsb = data[4] # 14 - 20
Zmsb = data[5] # 01 - 1

So Xlsb is the least significant byte and Xmsb is the most significant byte. These are integers

I guess I just don't understand the twos complement and how to combine these 2 integers/bytes into a signed integer.

1
How complete is this subset? Can you use the struct module? - MaxNoe
No, the struct module is not part of the language. FWIW, there is no support for floats either. It's designed to be extremely light weight. - cce1911
I'm sorry but if the two byte are EC and FF, being the second the most significant, the correct order is FF EC. Therefore, to convert you just need: (most << 8 | least) This will give you the raw data, then multiply it for the choosen sensor resolution. If you go for the max resolution, just multiply for 0.0039 - Lornioiz

1 Answers

0
votes

I'm not sure what you're doing with ord() there... ord(c) takes c, a single character, and outputs its ASCII character code.

Either way, to convert a number whose representation is in two's complement to a signed integer in Python, one would do something like this:

def tc(val, bits):
    if val >= 2**(bits-1):
        return val - (2**bits)
    return val

>>> tc(0xECFF, 16)
-4865

Though as for how to actually call that function with your data, I'd need to see what your data looks like in Python. What sort of numbers are you expecting to see for G-force?