Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>;
- const int slaveSelectPin = 10;
- const int FIFO_CTRL = 0x2E; //FIFO control register
- const int CTRL_REG1 = 0x20; //FIFO control register 1
- const int CTRL_REG4 = 0x23; //FIFO control register 4
- const int INT1_CTRL = 0x21; //INT1 pin control register
- const int STATUS = 0x27; //Status register
- const int WHO_AM_I = 0x0F; //
- const int OUT_X_L = 0x28; // OUT_X_L
- const int OUT_X_H = 0x29; // OUT_X_H
- const int OUT_Y_L = 0x2A; // OUT_Y_L
- const int OUT_Y_H = 0x2B; // OUT_Y_H
- const int OUT_Z_L = 0x2C; // OUT_Z_L
- const int OUT_Z_H = 0x2D; // OUT_Z_H
- void setup() {
- Serial.begin(115200);
- SPI.beginTransaction(SPISettings(14000000, MSBFIRST, SPI_MODE0))
- delay(2000);
- SPI.begin();
- pinMode (slaveSelectPin, OUTPUT);
- digitalWrite(slaveSelectPin, HIGH);
- /* Initialization */
- writeRegister(CTRL_REG1, 0xC0); // enables Normal mode and auto-increment
- writeRegister(CTRL_REG4, 0x01); // enables embedded temperature compensation
- writeRegister(INT1_CTRL, 0x80); // Data-ready interrupt on INT1 pin
- writeRegister(FIFO_CTRL, 0xC0); // FIFO mode continuous: if the FIFO is full, the new sample overwrites the older sample */
- delay(100); //give the sensor time to set up
- }
- void loop() {
- readFIFO();
- delay(25);
- }
- void readFIFO (){
- //FIFO stores 3 bytes of data for each axis (OUT_X, OUT_Y, OUT_Z)
- /* retrieving data from the FIFO
- reading the output registers (28h to 2Dh) returns the oldest FIFO sample set
- 32 reads of 6 bytes
- */
- if (readRegister(STATUS) == 0x8) { //read status STATUS 00000000 (0x0) if 00001000 (0x8) then continue;
- readRegister(OUT_X_L);
- Serial.println(readRegister(OUT_X_H));
- //readRegister(OUT_Y_L);
- //readRegister(OUT_Y_H);
- //readRegister(OUT_Z_L);
- //readRegister(OUT_Z_H);
- }
- //Data processing
- //Goto read status
- SPI.endTransaction();
- }
- byte readRegister (byte thisRegister){
- byte inByte = 0;
- digitalWrite(slaveSelectPin, LOW);
- SPI.transfer((thisRegister << 1) | 1);
- inByte = SPI.transfer(0x00);
- digitalWrite(slaveSelectPin, HIGH);
- return inByte;
- }
- void writeRegister (byte thisRegister, byte value){
- digitalWrite(slaveSelectPin, LOW);
- SPI.transfer(thisRegister << 1);
- SPI.transfer(value);
- digitalWrite(slaveSelectPin, HIGH);
- }
Advertisement
Add Comment
Please, Sign In to add comment