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

asked Sep 5, 2016 at 18:39
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!

answered Sep 5, 2016 at 21:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.