Adafruit LSM303 Accelerometer Library
Adafruit_LSM303_Accel.h
1 /***************************************************************************
2  This is a library for the LSM303 Accelerometer and magnentometer/compass
3 
4  Designed specifically to work with the Adafruit LSM303DLHC Breakout
5 
6  These displays use I2C to communicate, 2 pins are required to interface.
7 
8  Adafruit invests time and resources providing this open source code,
9  please support Adafruit andopen-source hardware by purchasing products
10  from Adafruit!
11 
12  Written by Bryan Siepert for Adafruit Industries.
13  BSD license, all text above must be included in any redistribution
14  ***************************************************************************/
15 #ifndef LSM303_ACCEL_H
16 #define LSM303_ACCEL_H
17 
18 #if (ARDUINO >= 100)
19 #include "Arduino.h"
20 #else
21 #include "WProgram.h"
22 #endif
23 
24 #include <Adafruit_BusIO_Register.h>
25 #include <Adafruit_I2CDevice.h>
26 #include <Adafruit_Sensor.h>
27 #include <Wire.h>
28 
29 /*=========================================================================
30 I2C ADDRESS/BITS
31 -----------------------------------------------------------------------*/
32 #define LSM303_ADDRESS_ACCEL (0x32 >> 1) // 0011001x
33 /*=========================================================================*/
34 
35 /*=========================================================================
36 REGISTERS
37 -----------------------------------------------------------------------*/
38 typedef enum { // DEFAULT TYPE
39  LSM303_REGISTER_ACCEL_WHO_AM_I = 0x0F, // 00000111 rw
40  LSM303_REGISTER_ACCEL_CTRL_REG1_A = 0x20, // 00000111 rw
41  LSM303_REGISTER_ACCEL_CTRL_REG2_A = 0x21, // 00000000 rw
42  LSM303_REGISTER_ACCEL_CTRL_REG3_A = 0x22, // 00000000 rw
43  LSM303_REGISTER_ACCEL_CTRL_REG4_A = 0x23, // 00000000 rw
44  LSM303_REGISTER_ACCEL_CTRL_REG5_A = 0x24, // 00000000 rw
45  LSM303_REGISTER_ACCEL_CTRL_REG6_A = 0x25, // 00000000 rw
46  LSM303_REGISTER_ACCEL_REFERENCE_A = 0x26, // 00000000 r
47  LSM303_REGISTER_ACCEL_STATUS_REG_A = 0x27, // 00000000 r
48  LSM303_REGISTER_ACCEL_OUT_X_L_A = 0x28,
49  LSM303_REGISTER_ACCEL_OUT_X_H_A = 0x29,
50  LSM303_REGISTER_ACCEL_OUT_Y_L_A = 0x2A,
51  LSM303_REGISTER_ACCEL_OUT_Y_H_A = 0x2B,
52  LSM303_REGISTER_ACCEL_OUT_Z_L_A = 0x2C,
53  LSM303_REGISTER_ACCEL_OUT_Z_H_A = 0x2D,
54  LSM303_REGISTER_ACCEL_FIFO_CTRL_REG_A = 0x2E,
55  LSM303_REGISTER_ACCEL_FIFO_SRC_REG_A = 0x2F,
56  LSM303_REGISTER_ACCEL_INT1_CFG_A = 0x30,
57  LSM303_REGISTER_ACCEL_INT1_SOURCE_A = 0x31,
58  LSM303_REGISTER_ACCEL_INT1_THS_A = 0x32,
59  LSM303_REGISTER_ACCEL_INT1_DURATION_A = 0x33,
60  LSM303_REGISTER_ACCEL_INT2_CFG_A = 0x34,
61  LSM303_REGISTER_ACCEL_INT2_SOURCE_A = 0x35,
62  LSM303_REGISTER_ACCEL_INT2_THS_A = 0x36,
63  LSM303_REGISTER_ACCEL_INT2_DURATION_A = 0x37,
64  LSM303_REGISTER_ACCEL_CLICK_CFG_A = 0x38,
65  LSM303_REGISTER_ACCEL_CLICK_SRC_A = 0x39,
66  LSM303_REGISTER_ACCEL_CLICK_THS_A = 0x3A,
67  LSM303_REGISTER_ACCEL_TIME_LIMIT_A = 0x3B,
68  LSM303_REGISTER_ACCEL_TIME_LATENCY_A = 0x3C,
69  LSM303_REGISTER_ACCEL_TIME_WINDOW_A = 0x3D
70 } lsm303AccelRegisters_t;
71 
72 /*=========================================================================*/
73 
74 /**************************************************************************/
78 /**************************************************************************/
79 typedef struct lsm303AccelData_s {
80  int16_t x;
81  int16_t y;
82  int16_t z;
84 /*=========================================================================*/
85 
86 typedef enum range {
87  LSM303_RANGE_2G,
88  LSM303_RANGE_4G,
89  LSM303_RANGE_8G,
90  LSM303_RANGE_16G,
91 } lsm303_accel_range_t;
92 
93 typedef enum mode {
94  LSM303_MODE_NORMAL,
95  LSM303_MODE_HIGH_RESOLUTION,
96  LSM303_MODE_LOW_POWER,
97 } lsm303_accel_mode_t;
98 /*=========================================================================
99  CHIP ID
100  -----------------------------------------------------------------------*/
101 #define LSM303_ID (0b11010100)
102 /*=========================================================================*/
103 
107 class Adafruit_LSM303_Accel_Unified : public Adafruit_Sensor {
108 public:
109  Adafruit_LSM303_Accel_Unified(int32_t sensorID = -1);
110 
111  bool begin(uint8_t i2c_addr = LSM303_ADDRESS_ACCEL, TwoWire *wire = &Wire);
112  bool getEvent(sensors_event_t *);
113  void getSensor(sensor_t *);
114 
115  void setRange(lsm303_accel_range_t);
116  lsm303_accel_range_t getRange(void);
117 
118  lsm303_accel_mode_t getMode(void);
119  void setMode(lsm303_accel_mode_t);
120 
121  void interruptsActiveHigh(bool);
122 
123 private:
124  int32_t _sensorID;
125 
126  lsm303AccelData raw; // Last read accelerometer data will be available here
127  float getLSB(lsm303_accel_mode_t);
128  uint8_t getShift(lsm303_accel_mode_t);
129 
130  void readRawData(void);
131 
132  Adafruit_I2CDevice *i2c_dev;
133 };
134 
135 #endif
int16_t z
z-axis data
Definition: Adafruit_LSM303_Accel.h:82
int16_t x
x-axis data
Definition: Adafruit_LSM303_Accel.h:80
Unified sensor driver for the accelerometer.
Definition: Adafruit_LSM303_Accel.h:107
INTERNAL ACCELERATION DATA TYPE.
Definition: Adafruit_LSM303_Accel.h:79
int16_t y
y-axis data
Definition: Adafruit_LSM303_Accel.h:81