Skip to content

Conversation

@petedl
Copy link

@petedl petedl commented Jun 24, 2021

fixed sign extension from 20 to 32 bits and fixed multiple byte reads by putting chip select toggle in between reads

@rbp9802
Copy link

rbp9802 commented Nov 20, 2021

Hi Pete, i've been having some issues reading X an Z small accelerations, they tend to be the same when there are small movements. I test the same device on an python library and works well so i tried to copy what i think was making the issue. I'll just copy the code here.

I changed de readMultipleData function to a single multiple read instead of a multiple single read. It is faster and i pressume this was the issue because it set the CS pin HIGH after reading 1 byte maybe caussing trouble with the FIFO.

Original code:

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

New code:

void readMultipleData(int addresses, int dataSize, int *readedData) {
  digitalWrite(ADXL355_CS_PIN, LOW);
  byte dataToSend = (addresses << 1) | READ_BYTE;
  SPI1.transfer(dataToSend);

  for(int i = 0; i < dataSize; i++) {
    readedData[i] = SPI1.transfer(0x00);}

  digitalWrite(ADXL355_CS_PIN, HIGH);
}

So you 9 registers from the first register XDATA3 = 0x09. Note that the original code started reading from the third register XDATA1 = 0x0A so you have to change axisMeasures index when calculating xdata, ydata and zdata as you read registers from MSB to LSB.

void ADXL355_get3V(){
  int Address = XDATA3;
  int axisMeasures[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
  int dataSize = 9;

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

  // Split data
  xdata = (axisMeasures[2] >> 4) + (axisMeasures[1] << 4) + (axisMeasures[0] << 12);
  ydata = (axisMeasures[5] >> 4) + (axisMeasures[4] << 4) + (axisMeasures[3] << 12);
  zdata = (axisMeasures[8] >> 4) + (axisMeasures[7] << 4) + (axisMeasures[6] << 12);
  //                    ^                        ^                        ^     change index
  
  // extend the sign bit from bit 19 to bit 31
  xdata = ((xdata<<12)>>12);
  ydata = ((ydata<<12)>>12); 
  zdata = ((zdata<<12)>>12); 
}

you can also do functions like:

int ADXL355_getZ(){
  int axisAddresses = ZDATA3;
  int axisMeasures[] = {0, 0, 0};
  int dataSize = 3;

  readMultipleData(axisAddresses, dataSize, axisMeasures); // Read accelerometer data
  int zdata = (axisMeasures[2] >> 4) + (axisMeasures[1] << 4) + (axisMeasures[1] << 12); // Split data
  
  zdata = ((zdata<<12)>>12);  // extend the sign bit from bit 19 to bit 31
  return zdata;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants