Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of ADXL345 by
Revision 2:e64d8b9096cd, committed 2012-09-09
- Comitter:
- gltest26
- Date:
- Sun Sep 09 15:37:26 2012 +0000
- Parent:
- 1:45faba962a46
- Child:
- 3:7b83694c7292
- Commit message:
- Fixed a bug getOutput() returned incorrect negative values.
Changed in this revision
| ADXL345.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- 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]);
}
