# How to get data from a sensor's FIRST IN FIRST OUT FIFO register (from two's complement)

I want to get acceleration data from a ADXL355 sensor.

My code:

``````void readFIFO (){
long data[3];
byte data2 [9];

digitalWrite(chipSelectPin, LOW);
SPI.transfer( (FIFO<<1) | 1);
for (int i = 0 ; i < 9; i++){
data2[i] = SPI.transfer(0x00);
}
digitalWrite(chipSelectPin, HIGH);

Serial.println("Byte data in hex and binary");
for (int j = 0 ; j < 9 ; j++){
Serial.print("element "+String(j) +": ");
Serial.print(data2[j],HEX);
Serial.print(" ");
Serial.println(data2[j],BIN);
}
for (int z = 0 ; z < 3; z++){
data[z] = ((data2[z*3]<<12) | (data2[z*3 +1] << 4) | (data2[z*3+2]  >>4));
}
Serial.println("concatenated values: ");
for (int i = 0 ; i < 3 ; i++){
Serial.println(data[i]);
}
}
``````

Sample output:

``````Byte data in hex and binary
element 0: F9 11111001
element 1: 1B 11011
element 2: 51 1010001
element 3: 0 0
element 4: FD 11111101
element 5: 90 10010000
element 6: C3 11000011
element 7: 2A 101010
element 8: 20 100000
concatenated values:
-28235
4057
12962
``````

The values are two's complement. Are these the correct values? If not what am I doing wrong?

You need to sign extend the results.

e.g. If the value is -1 your 3 byte values will be: 0xff ff f0
Combining these in to a single 32 bit value as you do gives you: 0x00 0f ff ff

Which is not -1.

What you need to do is check the most significant bit of the value and if it is set then set the top 12 bits.

``````for (int z = 0 ; z < 3; z++){
data[z] = (((long)data2[z*3]<<12) | ((long)data2[z*3 +1] << 4) | (data2[z*3+2]  >>4));
if (data2[z*3] & 0x80)
data[z] |= 0xfff00000;
}
``````

Since you've had no response to this I'll add my experiences with this chip. I'm not sure why you are reading the FIFO buffer. The chip provides three registers for each of the three axes. I simply read those in a loop. You can find my (still very crude) Python to read these at https://github.com/markrad/ADXL355. I hope it's some help to you.

• FIFO buffers are extremely useful for IC's with an ADC so you can poll the sensor for the data less frequently and get all samples that were collected in the interim. This prevents you from having to have a very tight sampling loop if you have many sensors to communicate with. Commented Jul 27, 2021 at 15:41