0

I'm having trouble getting information off the accelerometer I'm trying to use. What code should I use to read the X,Y, and Z axis? I've read the data sheet but I don't have any experience with SPI communication.

Spec sheet

0

1 Answer 1

2

The basic SPI operations looks like this. First read single register:

uint8_t readRegister(int index)
{
  digitalWrite(CS, LOW);
  SPI.beginTransaction(SPI_SETTING);
  SPI.transfer(READ | index);
  uint8_t res = SPI.transfer(0);
  SPI.endTransaction();
  digitalWrite(CS, HIGH);
  return (res);
}

And second write single register:

void writeRegister(int index, uint8_t data)
{
  digitalWrite(CS, LOW);
  SPI.beginTransaction(SPI_SETTING);
  SPI.transfer(WRITE | index);
  SPI.transfer(data);
  SPI.endTransaction();
  digitalWrite(CS, HIGH);
}

Some more details:

const uint8_t WRITE = 0x00;
const uint8_t READ = 0x80;

It is also possible to read/write a sequence of registers by providing the register index with the MS set. This will auto-increment the register index.

SPI_SETTING is left as an exercise (to be updated).

Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.