Go Down

Topic: SPI and LIS3DH accelerometer on an M0 Adalogger (Read 983 times) previous topic - next topic

7elEvan

Hello,

I'm working on a project involving the LIS3DH accelerometer and the M0 Adalogger from Adafruit.  However, what I need it to do is beyond what I can figure out with their libraries; namely, I need to enable the FIFO and the high-pass filter.  I've written a code that works with the UNO for basic SPI communication (just reading the WHO_AM_I register), but it doesn't work with the Adalogger.  If I can get the basic SPI to work, I should be able to get the rest of the program going.  Thanks for the help!

Code: [Select]
/*
 * WHO AM I?
 * The ultimate question for an LIS3DH...
 */

#include <SPI.h>

#define DATAOUT 11      //MOSI
#define DATAIN 12       //MISO
#define SPICLOCK 6      //sck
#define SLAVESELECT 10  //ss

void setup() {
 
  while (!Serial)
  Serial.begin(9600);

  SPI.begin();
 
  // Set SPI pin modes
  pinMode(SPICLOCK, OUTPUT);
  pinMode(DATAOUT, OUTPUT);
  pinMode(DATAIN, INPUT);
  pinMode(SLAVESELECT,OUTPUT);

  // Initialize SPI bus settings
  SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE3));
 
  digitalWrite(SLAVESELECT, HIGH); //disable device

  // Write to CTRL_REG1 to take out of power down mode
  digitalWrite(SLAVESELECT, LOW); //enable device
  SPI.transfer(0x20);
  SPI.transfer(0x8F);
  digitalWrite(SLAVESELECT, HIGH);

  // Read and print WHO_AM_I register
  digitalWrite(SLAVESELECT, LOW); //enable device
  // WHO_AM_I address with bit 0 set to "1" as the LIS3DH read command
  SPI.transfer(0x8F);
  uint8_t deviceid = SPI.transfer(0x00);
  digitalWrite(SLAVESELECT, HIGH);

  SPI.endTransaction();

  Serial.println(deviceid, HEX);
 
}

void loop() {
  // put your main code here, to run repeatedly:

}


The datasheet and application note for the LIS3DH can be found here:
https://cdn-shop.adafruit.com/datasheets/LIS3DH.pdf
https://cdn-shop.adafruit.com/datasheets/LIS3DHappnote.pdf

7elEvan

So, I realize I should have mentioned this before, but regardless or what register I try to read OR even if I comment out the entire section on writing to CTRL_REG1 (the one that takes is out of powerdown mode and sets the data rate), 0xFF is returned.  I'm starting to think the LIS3DH is not properly coming out of powerdown mode.  Again, this is pretty odd considering 0x33 is returned when using an UNO with the same code.

dlabun

I don't think you're wired up correctly or initiating the SPI bus right...Are you connected to the pins labeled for the SPI port on the Feather?

 

Go Up