Adafruit LIS3DH
Adafruit_LIS3DH.h
Go to the documentation of this file.
1 
27 #ifndef ADAFRUIT_LIS3DH_H
28 #define ADAFRUIT_LIS3DH_H
29 
30 #include "Arduino.h"
31 
32 #include <SPI.h>
33 #include <Wire.h>
34 
35 #include <Adafruit_BusIO_Register.h>
36 #include <Adafruit_I2CDevice.h>
37 #include <Adafruit_SPIDevice.h>
38 #include <Adafruit_Sensor.h>
39 
41 #define LIS3DH_DEFAULT_ADDRESS (0x18) // if SDO/SA0 is 3V, its 0x19
42 
62 #define LIS3DH_REG_STATUS1 0x07
63 #define LIS3DH_REG_OUTADC1_L 0x08
64 #define LIS3DH_REG_OUTADC1_H 0x09
65 #define LIS3DH_REG_OUTADC2_L 0x0A
66 #define LIS3DH_REG_OUTADC2_H 0x0B
67 #define LIS3DH_REG_OUTADC3_L 0x0C
68 #define LIS3DH_REG_OUTADC3_H 0x0D
69 #define LIS3DH_REG_INTCOUNT \
70  0x0E
71 #define LIS3DH_REG_WHOAMI \
72  0x0F
81 #define LIS3DH_REG_TEMPCFG 0x1F
82 
93 #define LIS3DH_REG_CTRL1 0x20
94 
108 #define LIS3DH_REG_CTRL2 0x21
109 
127 #define LIS3DH_REG_CTRL3 0x22
128 
144 #define LIS3DH_REG_CTRL4 0x23
145 
157 #define LIS3DH_REG_CTRL5 0x24
158 
163 #define LIS3DH_REG_CTRL6 0x25
164 #define LIS3DH_REG_REFERENCE 0x26
184 #define LIS3DH_REG_STATUS2 0x27
185 #define LIS3DH_REG_OUT_X_L 0x28
186 #define LIS3DH_REG_OUT_X_H 0x29
187 #define LIS3DH_REG_OUT_Y_L 0x2A
188 #define LIS3DH_REG_OUT_Y_H 0x2B
189 #define LIS3DH_REG_OUT_Z_L 0x2C
190 #define LIS3DH_REG_OUT_Z_H 0x2D
200 #define LIS3DH_REG_FIFOCTRL 0x2E
201 #define LIS3DH_REG_FIFOSRC \
202  0x2F
224 #define LIS3DH_REG_INT1CFG 0x30
243 #define LIS3DH_REG_INT1SRC 0x31
244 #define LIS3DH_REG_INT1THS \
245  0x32
246 #define LIS3DH_REG_INT1DUR \
247  0x33
268 #define LIS3DH_REG_CLICKCFG 0x38
286 #define LIS3DH_REG_CLICKSRC 0x39
292 #define LIS3DH_REG_CLICKTHS 0x3A
298 #define LIS3DH_REG_TIMELIMIT 0x3B
304 #define LIS3DH_REG_TIMELATENCY 0x3C
310 #define LIS3DH_REG_TIMEWINDOW 0x3D
312 #define LIS3DH_LSB16_TO_KILO_LSB10 \
313  64000
314 #define LIS3DH_LSB16_TO_KILO_LSB12 \
316  16000
317 #define LIS3DH_LSB16_TO_KILO_LSB8 \
319  256000
320 
322 #define LIS3DH_DEFAULT_SPIFREQ 500000
324 
325 typedef enum {
326  LIS3DH_RANGE_16_G = 0b11, // +/- 16g
327  LIS3DH_RANGE_8_G = 0b10, // +/- 8g
328  LIS3DH_RANGE_4_G = 0b01, // +/- 4g
329  LIS3DH_RANGE_2_G = 0b00 // +/- 2g (default value)
331 
333 typedef enum {
334  LIS3DH_AXIS_X = 0x0,
335  LIS3DH_AXIS_Y = 0x1,
336  LIS3DH_AXIS_Z = 0x2,
337 } lis3dh_axis_t;
338 
340 typedef enum {
341  LIS3DH_MODE_LOW_POWER = 0x0,
342  LIS3DH_MODE_NORMAL = 0x1,
343  LIS3DH_MODE_HIGH_RESOLUTION = 0x2,
344 } lis3dh_mode_t;
345 
350 typedef enum {
351  LIS3DH_DATARATE_400_HZ = 0b0111, // 400Hz
352  LIS3DH_DATARATE_200_HZ = 0b0110, // 200Hz
353  LIS3DH_DATARATE_100_HZ = 0b0101, // 100Hz
354  LIS3DH_DATARATE_50_HZ = 0b0100, // 50Hz
355  LIS3DH_DATARATE_25_HZ = 0b0011, // 25Hz
356  LIS3DH_DATARATE_10_HZ = 0b0010, // 10 Hz
357  LIS3DH_DATARATE_1_HZ = 0b0001, // 1 Hz
358  LIS3DH_DATARATE_POWERDOWN = 0,
359  LIS3DH_DATARATE_LOWPOWER_1K6HZ = 0b1000,
360  LIS3DH_DATARATE_LOWPOWER_5KHZ = 0b1001,
361 
363 
368 class Adafruit_LIS3DH : public Adafruit_Sensor {
369 public:
370  Adafruit_LIS3DH(TwoWire *Wi = &Wire);
371  Adafruit_LIS3DH(int8_t cspin, SPIClass *theSPI = &SPI,
372  uint32_t frequency = LIS3DH_DEFAULT_SPIFREQ);
373  Adafruit_LIS3DH(int8_t cspin, int8_t mosipin, int8_t misopin, int8_t sckpin,
374  uint32_t frequency = LIS3DH_DEFAULT_SPIFREQ);
375 
376  bool begin(uint8_t addr = LIS3DH_DEFAULT_ADDRESS, uint8_t nWAI = 0x33);
377 
378  uint8_t getDeviceID(void);
379  bool haveNewData(void);
380  bool enableDRDY(bool enable_drdy = true, uint8_t int_pin = 1);
381 
382  void read(void);
383  int16_t readADC(uint8_t a);
384 
387 
388  void setRange(lis3dh_range_t range);
389  lis3dh_range_t getRange(void);
390 
391  void setDataRate(lis3dh_dataRate_t dataRate);
393 
394  bool getEvent(sensors_event_t *event);
395  void getSensor(sensor_t *sensor);
396 
397  void setClick(uint8_t c, uint8_t clickthresh, uint8_t timelimit = 10,
398  uint8_t timelatency = 20, uint8_t timewindow = 255);
399  uint8_t getClick(void);
400 
401  uint8_t readAndClearInterrupt(void);
402 
403  int16_t x;
404  int16_t y;
405  int16_t z;
407  float x_g;
408  float y_g;
409  float z_g;
411 private:
412  TwoWire *I2Cinterface;
413  SPIClass *SPIinterface;
414 
415  Adafruit_I2CDevice *i2c_dev = NULL;
416  Adafruit_SPIDevice *spi_dev = NULL;
417 
418  uint8_t _wai;
419 
420  int8_t _cs, _mosi, _miso, _sck;
421 
422  int8_t _i2caddr;
423 
424  int32_t _sensorID;
425  uint32_t _frequency = LIS3DH_DEFAULT_SPIFREQ;
426 };
427 
428 #endif
lis3dh_mode_t getPerformanceMode(void)
Gets the performance mode for the LIS3DH.
Definition: Adafruit_LIS3DH.cpp:434
lis3dh_range_t
Definition: Adafruit_LIS3DH.h:326
Adafruit_LIS3DH(TwoWire *Wi=&Wire)
Instantiates a new LIS3DH class in I2C.
Definition: Adafruit_LIS3DH.cpp:41
float x_g
Definition: Adafruit_LIS3DH.h:408
lis3dh_axis_t
Definition: Adafruit_LIS3DH.h:334
uint8_t readAndClearInterrupt(void)
Get uint8_t for INT1 source and clear interrupt.
Definition: Adafruit_LIS3DH.cpp:356
void setDataRate(lis3dh_dataRate_t dataRate)
Sets the data rate for the LIS3DH (controls power consumption)
Definition: Adafruit_LIS3DH.cpp:490
lis3dh_dataRate_t getDataRate(void)
Gets the data rate for the LIS3DH (controls power consumption)
Definition: Adafruit_LIS3DH.cpp:503
uint8_t getDeviceID(void)
Get Device ID from LIS3DH_REG_WHOAMI.
Definition: Adafruit_LIS3DH.cpp:159
lis3dh_dataRate_t
Data rate selection Used with register 0x2A (LIS3DH_REG_CTRL_REG1) to set bandwidth.
Definition: Adafruit_LIS3DH.h:351
lis3dh_range_t getRange(void)
Gets the g range for the accelerometer.
Definition: Adafruit_LIS3DH.cpp:476
float z_g
Definition: Adafruit_LIS3DH.h:410
uint8_t getClick(void)
Get uint8_t for single or double click.
Definition: Adafruit_LIS3DH.cpp:345
bool begin(uint8_t addr=LIS3DH_DEFAULT_ADDRESS, uint8_t nWAI=0x33)
Setups the HW (reads coefficients values, etc.)
Definition: Adafruit_LIS3DH.cpp:97
#define LIS3DH_DEFAULT_SPIFREQ
SPI frequency for LIS3DH.
Definition: Adafruit_LIS3DH.h:323
bool enableDRDY(bool enable_drdy=true, uint8_t int_pin=1)
Enable or disable the Data Ready interupt.
Definition: Adafruit_LIS3DH.cpp:371
bool getEvent(sensors_event_t *event)
Gets the most recent sensor event.
Definition: Adafruit_LIS3DH.cpp:518
lis3dh_mode_t
Definition: Adafruit_LIS3DH.h:341
void getSensor(sensor_t *sensor)
Gets the sensor_t data.
Definition: Adafruit_LIS3DH.cpp:541
#define LIS3DH_DEFAULT_ADDRESS
Definition: Adafruit_LIS3DH.h:41
bool haveNewData(void)
Check to see if new data available.
Definition: Adafruit_LIS3DH.cpp:169
void read(void)
Reads x y z values at once.
Definition: Adafruit_LIS3DH.cpp:180
Class that stores state and functions for interacting with Adafruit_LIS3DH.
Definition: Adafruit_LIS3DH.h:369
void setPerformanceMode(lis3dh_mode_t mode)
Sets the performance mode for the LIS3DH.
Definition: Adafruit_LIS3DH.cpp:397
int16_t y
Definition: Adafruit_LIS3DH.h:405
float y_g
Definition: Adafruit_LIS3DH.h:409
int16_t readADC(uint8_t a)
Read the auxilary ADC.
Definition: Adafruit_LIS3DH.cpp:247
int16_t z
Definition: Adafruit_LIS3DH.h:406
void setRange(lis3dh_range_t range)
Sets the g range for the accelerometer.
Definition: Adafruit_LIS3DH.cpp:461
int16_t x
Definition: Adafruit_LIS3DH.h:404
void setClick(uint8_t c, uint8_t clickthresh, uint8_t timelimit=10, uint8_t timelatency=20, uint8_t timewindow=255)
Set INT to output for single or double click.
Definition: Adafruit_LIS3DH.cpp:290