I'm trying to rearrange the binary numbers. I'm getting zero for my x and y which is correct, but when I move it just a little bit, the number goes to 127 which means that I need to organized the bytes. I'm trying to read register 0x33 before 0x32 for the x value, 0x35 before 0x34 for y value, and 0x37 before 0x36, but I don't know how. If anyone knows how, please let me know!
Below is the code that I'm using to read the data:
// === Read acceleromter data === //
Wire.beginTransmission(ADXL313);
Wire.write(0x32); // Start with register 0x32
Wire.endTransmission(false);
Wire.requestFrom(ADXL313, 6, true); // Read 6 registers total, each axis value is stored in 2 registers
X_out = ( Wire.read()| Wire.read() << 8); // X-axis value
Y_out = ( Wire.read()| Wire.read() << 8); // Y-axis value
Z_out = ( Wire.read()| Wire.read() << 8); // Z-axis value
C/C++ does not guarantee the order of execution of operations within a line of code.
This is safer. Rearrange the shift as required.
int t = Wire.read() << 8;
X_out = t | (Wire.read()); // X-axis value
t = Wire.read() << 8;
Y_out = t |( Wire.read()); // Y-axis value
t = Wire.read() << 8;
Z_out = t | ( Wire.read()); // Z-axis value