ADXL357 Results

Hello,

I have a ADXL357 using SPI. When I query any registers, I get a suitable result and I can retrieve acceleration data from the registers, but the values I get are incorrect.

Code is shown below. Running the code I get the following output with no movement of the sensor and it oriented with Z in the direction of gravity. Even if I move the sensor's orientation the values don't change much. Any idea what the issue is?

---------SERIAL MONITOR OUTPUT---------------


173
29
237
1
X=0.93, Y=-72.36, Z=-74.78
X=0.92, Y=-69.30, Z=-72.58
X=0.92, Y=-69.74, Z=-75.44
X=0.90, Y=-67.86, Z=-74.16
X=0.92, Y=-70.76, Z=-73.14
X=0.92, Y=-71.56, Z=-73.56
X=0.92, Y=-70.94, Z=-72.76
X=0.92, Y=-72.56, Z=-73.52
X=0.92, Y=-70.92, Z=-73.70

---------CODE---------------------------


#include <SPI.h>

// Memory register addresses:
const int XDATA3 = 0x08;
const int XDATA2 = 0x09;
const int XDATA1 = 0x0A;
const int YDATA3 = 0x0B;
const int YDATA2 = 0x0C;
const int YDATA1 = 0x0D;
const int ZDATA3 = 0x0E;
const int ZDATA2 = 0x0F;
const int ZDATA1 = 0x10;
const int RANGE = 0x2C;
const int POWER_CTL = 0x2D;
const int DEVID_AD = 0x00;
const int DEVID_MST = 0x01;
const int PARTID = 0x02;
const int REVID = 0x03;

// Device values
const int RANGE_2G = 1;
const int RANGE_20G = 2;
const int RANGE_40G = 3;
const int MEASURE_MODE = 0x06; // Only accelerometer

// Operations
const int READ_BYTE = 0x01;
const int WRITE_BYTE = 0x00;

// Pins used for the connection with the sensor
const int CHIP_SELECT_PIN = 5;

unsigned int result;

void setup() {
Serial.begin(115200);

pinMode(23, OUTPUT); // MOSI for SPI
pinMode(19, INPUT); // MISO for SPI
pinMode(18, OUTPUT); // SCK for SPI
SPI.begin();
SPI.beginTransaction(SPISettings(8000000, MSBFIRST, SPI_MODE0));
// Initalize the data ready and chip select pins:
pinMode(CHIP_SELECT_PIN, OUTPUT);

//Configure ADXL355:
writeRegister(RANGE, RANGE_40G);
writeRegister(POWER_CTL, MEASURE_MODE); // Enable measure mode
//SPI Pin out setup


// Give the sensor time to set up:
delay(100);

readRegistry(DEVID_AD);
Serial.println(result);
readRegistry(DEVID_MST);
Serial.println(result);
readRegistry(PARTID);
Serial.println(result);
readRegistry(REVID);
Serial.println(result);
}

void loop() {
int axisAddresses[] = {XDATA1, XDATA2, XDATA3, YDATA1, YDATA2, YDATA3, ZDATA1, ZDATA2, ZDATA3};
int axisMeasures[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
int dataSize = 9;

// Read accelerometer data
readMultipleData(axisAddresses, dataSize, axisMeasures);

// Split data
int xdata = (axisMeasures[0] >> 4) + (axisMeasures[1] << 4) + (axisMeasures[2] << 12);
int ydata = (axisMeasures[3] >> 4) + (axisMeasures[4] << 4) + (axisMeasures[5] << 12);
int zdata = (axisMeasures[6] >> 4) + (axisMeasures[7] << 4) + (axisMeasures[8] << 12);

// Apply two complement
if (xdata >= 0x80000) {
xdata = ~xdata + 1;
}
if (ydata >= 0x80000) {
ydata = ~ydata + 1;
}
if (zdata >= 0x80000) {
zdata = ~zdata + 1;
}

float calib = 1.0/(double) 12800;
float xdata1=(xdata * calib);
float ydata1=(ydata * calib);
float zdata1=(zdata * calib);

// Print axis
Serial.print("X=");
Serial.print(xdata1);
Serial.print(", ");

Serial.print("Y=");
Serial.print(ydata1);
Serial.print(", ");

Serial.print("Z=");
Serial.println(zdata1);

// Next data in 100 milliseconds
delay(100);
}

/*
* Write registry in specific device address
*/
void writeRegister(byte thisRegister, byte thisValue) {
byte dataToSend = (thisRegister << 1) | WRITE_BYTE;
digitalWrite(CHIP_SELECT_PIN, LOW);
SPI.transfer(dataToSend);
SPI.transfer(thisValue);
digitalWrite(CHIP_SELECT_PIN, HIGH);
}

/*
* Read registry in specific device address
*/
unsigned int readRegistry(byte thisRegister) {
result = 0;
byte dataToSend = (thisRegister << 1) | READ_BYTE;

digitalWrite(CHIP_SELECT_PIN, LOW);
SPI.transfer(dataToSend);
result = SPI.transfer(0x00);
digitalWrite(CHIP_SELECT_PIN, HIGH);
return result;
}

/*
* Read multiple registries
*/
void readMultipleData(int *addresses, int dataSize, int *readedData) {
digitalWrite(CHIP_SELECT_PIN, LOW);
for(int i = 0; i < dataSize; i = i + 1) {
byte dataToSend = (addresses[i] << 1)| READ_BYTE;
SPI.transfer(dataToSend);
readedData[i] = SPI.transfer(0x00);
}
digitalWrite(CHIP_SELECT_PIN, HIGH);
}