Guest User

IIS3DHHC

a guest
Apr 25th, 2019
638
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SPI.h>;
  2.  
  3. const int slaveSelectPin = 10;
  4. const int FIFO_CTRL = 0x2E; //FIFO control register
  5. const int CTRL_REG1 = 0x20; //FIFO control register 1
  6. const int CTRL_REG4 = 0x23; //FIFO control register 4
  7. const int INT1_CTRL = 0x21; //INT1 pin control register
  8. const int STATUS = 0x27; //Status register
  9. const int WHO_AM_I = 0x0F; //
  10. const int OUT_X_L = 0x28; // OUT_X_L
  11. const int OUT_X_H = 0x29; // OUT_X_H
  12. const int OUT_Y_L = 0x2A; // OUT_Y_L
  13. const int OUT_Y_H = 0x2B; // OUT_Y_H
  14. const int OUT_Z_L = 0x2C; // OUT_Z_L
  15. const int OUT_Z_H = 0x2D; // OUT_Z_H
  16.  
  17. void setup() {
  18.   Serial.begin(115200);
  19.   SPI.beginTransaction(SPISettings(14000000, MSBFIRST, SPI_MODE0))
  20.   delay(2000);
  21.   SPI.begin();
  22.   pinMode (slaveSelectPin, OUTPUT);
  23.   digitalWrite(slaveSelectPin, HIGH);
  24.  
  25.   /* Initialization */
  26.   writeRegister(CTRL_REG1, 0xC0); // enables Normal mode and auto-increment
  27.   writeRegister(CTRL_REG4, 0x01); // enables embedded temperature compensation
  28.   writeRegister(INT1_CTRL, 0x80); // Data-ready interrupt on INT1 pin
  29.   writeRegister(FIFO_CTRL, 0xC0); // FIFO mode continuous: if the FIFO is full, the new sample overwrites the older sample */
  30.  
  31.   delay(100); //give the sensor time to set up
  32. }
  33.  
  34. void loop() {
  35.   readFIFO();
  36.   delay(25);
  37. }
  38.  
  39. void readFIFO (){
  40.   //FIFO stores 3 bytes of data for each axis (OUT_X, OUT_Y, OUT_Z)
  41.   /* retrieving data from the FIFO
  42.   reading the output registers (28h to 2Dh) returns the oldest FIFO sample set
  43.   32 reads of 6 bytes
  44.    */
  45.   if (readRegister(STATUS) == 0x8) {   //read status STATUS 00000000 (0x0) if 00001000 (0x8) then continue;
  46.     readRegister(OUT_X_L);
  47.     Serial.println(readRegister(OUT_X_H));
  48.     //readRegister(OUT_Y_L);
  49.     //readRegister(OUT_Y_H);
  50.     //readRegister(OUT_Z_L);
  51.     //readRegister(OUT_Z_H);
  52.   }
  53.   //Data processing
  54.   //Goto read status
  55.   SPI.endTransaction();
  56. }
  57.  
  58. byte readRegister (byte thisRegister){
  59.     byte inByte = 0;
  60.     digitalWrite(slaveSelectPin, LOW);
  61.     SPI.transfer((thisRegister << 1) | 1);
  62.     inByte = SPI.transfer(0x00);
  63.     digitalWrite(slaveSelectPin, HIGH);
  64.     return inByte;
  65. }
  66.  
  67. void writeRegister (byte thisRegister, byte value){
  68.     digitalWrite(slaveSelectPin, LOW);
  69.     SPI.transfer(thisRegister << 1);
  70.     SPI.transfer(value);
  71.     digitalWrite(slaveSelectPin, HIGH);
  72. }
Advertisement
Add Comment
Please, Sign In to add comment