#include <SPI.h>
const int chipSelectPin = 10;
void setup() {
// put your setup code here, to run once:
pinMode(chipSelectPin, OUTPUT);
digitalWrite(chipSelectPin, HIGH);
Serial.begin(9600);
writeRegister(0x0D, 0x01); // Accelerometer data-ready interrupt on INT1
writeRegister(0x15, 0x00); // 3-axis mode enabled
writeRegister(0x10, 0xA0); // Enable accelerometer (ODR = 26.667 kHz, FS = +-2 g)
}
void loop() {
uint8_t temp = readRegister(0x0F);
Serial.println(temp, BIN);
delay(1000);
}
uint8_t readRegister(uint8_t reg) {
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE3));
digitalWrite(chipSelectPin, LOW);
SPI.transfer((reg & 0x7F) | 0x80);
uint8_t temp = SPI.transfer(0);
digitalWrite(chipSelectPin, HIGH);
SPI.endTransaction();
return temp;
}
void writeRegister(uint8_t reg, uint8_t value) {
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE3));
digitalWrite(chipSelectPin, LOW);
SPI.transfer(reg & 0x7F);
SPI.transfer(value);
digitalWrite(chipSelectPin, HIGH);
SPI.endTransaction();
}