# ADXL345: Convert 16-Bit Twos Complement to Float

Question asked by JamesC on Jun 16, 2012
Latest reply on Jun 19, 2012 by jml

I am not clear on exactly how to convert DATAX0 and DATAX1 in register 0x32 into a float.  I have the following setup for the DATA_FORMAT register: range:  +/- 2G, D1/D0=00

justify: right justified w/sign extension, D2=0

full_res:  10-bit, D3 = 0

D7-D4 = 0000

comm = I2C

Here is a code snippet of what I am doing.  I am using a multi-byte read, as shown in another discussion to fetch the accel data.

// let's print out the data to see what we have
aX = ((signed int16) (accelIntArray[0] | accelIntArray[1]<<8));
aY = ((signed int16) (accelIntArray[2] | accelIntArray[3]<<8));
aZ = ((signed int16) (accelIntArray[4] | accelIntArray[5]<<8));
printf("AAAF  ");   // just a couple of bytes to mark the start of the data
printf("%4Ld %4Ld %4Ld  ", aX, aY,  aZ);
for(ix = 0; ix<6; ix++) {
printf("%X ", accelIntArray[ix]);
}

// read register 0x00 and check if we get 0xE5
tempByte = i2cBuffer[0];      // after the read, this should have 0xE5

printf("%X", tempByte);
printf("\n\r");

Here is some output displayed in Hyperterminal.  "AAAF" are just some sync bytes from my PIC MCU.  "252  13  209"  are the aX  aY  aZ values in signed int, although I never see a sign change when I change the sensor orientation.  The next 6 bytes are the actual DATAX0, DATAX1...DATAZ0, DATAZ1 byte values.  And E5 is just the read result from a single byte read to register 0x00.

AAAF   252   13  209  FC FF 0D 01 D1 FF E5

AAAF   252   13  209  FC FF 0D 01 D1 FF E5

AAAF   252   13  209  FC FF 0D 01 D1 FF E5

AAAF   252   13  209  FC FF 0D 01 D1 FF E5

AAAF   251    7  189  FB FF 07 01 BD FF E5

AAAF   251    7  189  FB FF 07 01 BD FF E5

AAAF   251    7  189  FB FF 07 01 BD FF E5

AAAF   251    7  189  FB FF 07 01 BD FF E5

Any help deciphering this data would be greatly appreciated.  Thank you very much!