Republished Library, to be refined for use with the SparkFun 9DOF in HARP project.
Fork of ADXL345 by
Diff: ADXL345.cpp
- Revision:
- 2:e64d8b9096cd
- Parent:
- 1:45faba962a46
- Child:
- 3:7b83694c7292
--- a/ADXL345.cpp Fri Oct 07 21:06:14 2011 +0000 +++ b/ADXL345.cpp Sun Sep 09 15:37:26 2012 +0000 @@ -136,9 +136,12 @@ char buffer[6]; multiByteRead(ADXL345_DATAX0_REG, buffer, 6); - readings[0] = (int)buffer[1] << 8 | (int)buffer[0]; - readings[1] = (int)buffer[3] << 8 | (int)buffer[2]; - readings[2] = (int)buffer[5] << 8 | (int)buffer[4]; + // Readings are expressed in 16bit 2's complement, so we must first + // concatenate two bytes to make a word and sign extend it to obtain + // correct negative values. + readings[0] = (int)int16_t(uint16_t(buffer[1]) << 8 | buffer[0]); + readings[1] = (int)int16_t(uint16_t(buffer[3]) << 8 | buffer[2]); + readings[2] = (int)int16_t(uint16_t(buffer[5]) << 8 | buffer[4]); }