AnsweredAssumed Answered

BRKOUT-FXLS8471Q using SPI

Question asked by Inge Kalsbeek on Apr 2, 2015
Latest reply on Jun 7, 2016 by Tomas Vaverka

Hi, I've connected the FXLS8471Q breakout board via its SPI connection and a bi-directional level shifter to a Redboard ('same' as Arduino Uno) but get only -1 as output values. I checked the pins with an oscilloscope and it is clear that the Arduino is communicating to the CS_B, SCL and MOSI pins, but the MISO pin isn't to the Arduino.

 

Is there something I forgot to get the sensor 'active'? I attached the code I'm using.

 

I powered the breakout board by the VDD connection, but in the data sheet there is also something mentioned about the interface power. Is this already connected in the board or does it need additional wiring?

 

Here the .cpp file:

// FXLS8471Q registers
#define FXLS8471Q_STATUS 0x00
#define FXLS8471Q_WHOAMI 0x0D
#define FXLS8471Q_XYZ_DATA_CFG 0x0E
#define FXLS8471Q_CTRL_REG1 0x2A

int16_t slaveSelectPin = 10;

FXLS8471Q::FXLS8471Q() {
}

void FXLS8471Q::begin(int16_t chipSelectPin) {
    slaveSelectPin = chipSelectPin;
    pinMode(slaveSelectPin, OUTPUT);
    SPI.begin();
    SPI.setClockDivider(SPI_CLOCK_DIV16);                                    // 16 MHz / 16 = 1 MHz
    SPI.setDataMode(SPI_MODE0);    //CPHA = CPOL = 0    MODE = 0
    SPI.setBitOrder(MSBFIRST);
    delay(1000);
   
    // reset sensor
    SPIwriteOneRegister(0x2B, 0x40);                        // set RST in CTRL_REG2 register
    delay(100);
   
    // set range to 4g
    SPIwriteOneRegister(FXLS8471Q_XYZ_DATA_CFG, 0x01); // Write to FXLS8471Q_XYZ_DATA_CFG
   
    // set data rate to 200 Hz and enable sampling
    SPIwriteOneRegister(FXLS8471Q_CTRL_REG1, 0x15); //

void FXLS8471Q::readXYZData(int16_t &XData, int16_t &YData, int16_t &ZData){
    
      digitalWrite(slaveSelectPin, LOW);
      delay(10);
      SPI.transfer(0x01);  // send read instruction (RW=0, address=0x01)
      SPI.transfer(0x00);  // send second byte
     
      XData = SPI.transfer(0x00) << 8;      //Most significant bit(MSB) first, then LSB
      XData = XData + SPI.transfer(0x00);
      XData = XData >> 2;

      YData = SPI.transfer(0x00) << 8;
      YData = YData + SPI.transfer(0x00);
      YData = YData >> 2;
     
      ZData = SPI.transfer(0x00) << 8;
      ZData = ZData + SPI.transfer(0x00);
      ZData = ZData >> 2;

      digitalWrite(slaveSelectPin, HIGH);
}

 

And the header file:

#include "Arduino.h"
#ifndef FXLS8471Q_h
#define FXLS8471Q_h

class FXLS8471Q
{
public:

    FXLS8471Q();
    // Basic Device control and readback functions
    void begin(int16_t chipSelectPin = 10);
    void FXLS8471Q::readXYZData(int16_t &XData, int16_t &YData, int16_t &ZData)
};

#endif

 

The code I'm running

#include <SPI.h>
#include <FXLS8471Q.h>

FXLS8471Q xl;

int16_t XValue, YValue, ZValue;

void setup(){
  Serial.begin(9600);
  xl.begin(10);                   // Setup SPI protocol, issue device soft reset
}

void loop(){
   
  // read all three axis in burst to ensure all measurements correspond to same sample time
  xl.readXYZData(XValue, YValue, ZValue); 
  Serial.print("XVALUE=");
  Serial.print(XValue);    
  Serial.print("\tYVALUE=");
  Serial.print(YValue);    
  Serial.print("\tZVALUE=");
  Serial.print(ZValue);
  Serial.println();     
  delay(100);                // Arbitrary delay to make serial monitor easier to observe
}

Outcomes