Adafruit LSM303 Accelerometer Library
Adafruit_LSM303_Accel.h
Go to the documentation of this file.
1 
6 #ifndef LSM303_ACCEL_H
7 #define LSM303_ACCEL_H
8 
9 #if (ARDUINO >= 100)
10 #include "Arduino.h"
11 #else
12 #include "WProgram.h"
13 #endif
14 
15 #include <Adafruit_BusIO_Register.h>
16 #include <Adafruit_I2CDevice.h>
17 #include <Adafruit_Sensor.h>
18 #include <Wire.h>
19 
20 #define LSM303_ADDRESS_ACCEL (0x32 >> 1)
21 
22 
25 typedef enum { // DEFAULT TYPE
26  LSM303_REGISTER_ACCEL_WHO_AM_I = 0x0F, // 00000111 rw
27  LSM303_REGISTER_ACCEL_CTRL_REG1_A = 0x20, // 00000111 rw
28  LSM303_REGISTER_ACCEL_CTRL_REG2_A = 0x21, // 00000000 rw
29  LSM303_REGISTER_ACCEL_CTRL_REG3_A = 0x22, // 00000000 rw
30  LSM303_REGISTER_ACCEL_CTRL_REG4_A = 0x23, // 00000000 rw
31  LSM303_REGISTER_ACCEL_CTRL_REG5_A = 0x24, // 00000000 rw
32  LSM303_REGISTER_ACCEL_CTRL_REG6_A = 0x25, // 00000000 rw
33  LSM303_REGISTER_ACCEL_REFERENCE_A = 0x26, // 00000000 r
34  LSM303_REGISTER_ACCEL_STATUS_REG_A = 0x27, // 00000000 r
35  LSM303_REGISTER_ACCEL_OUT_X_L_A = 0x28,
36  LSM303_REGISTER_ACCEL_OUT_X_H_A = 0x29,
37  LSM303_REGISTER_ACCEL_OUT_Y_L_A = 0x2A,
38  LSM303_REGISTER_ACCEL_OUT_Y_H_A = 0x2B,
39  LSM303_REGISTER_ACCEL_OUT_Z_L_A = 0x2C,
40  LSM303_REGISTER_ACCEL_OUT_Z_H_A = 0x2D,
41  LSM303_REGISTER_ACCEL_FIFO_CTRL_REG_A = 0x2E,
42  LSM303_REGISTER_ACCEL_FIFO_SRC_REG_A = 0x2F,
43  LSM303_REGISTER_ACCEL_INT1_CFG_A = 0x30,
44  LSM303_REGISTER_ACCEL_INT1_SOURCE_A = 0x31,
45  LSM303_REGISTER_ACCEL_INT1_THS_A = 0x32,
46  LSM303_REGISTER_ACCEL_INT1_DURATION_A = 0x33,
47  LSM303_REGISTER_ACCEL_INT2_CFG_A = 0x34,
48  LSM303_REGISTER_ACCEL_INT2_SOURCE_A = 0x35,
49  LSM303_REGISTER_ACCEL_INT2_THS_A = 0x36,
50  LSM303_REGISTER_ACCEL_INT2_DURATION_A = 0x37,
51  LSM303_REGISTER_ACCEL_CLICK_CFG_A = 0x38,
52  LSM303_REGISTER_ACCEL_CLICK_SRC_A = 0x39,
53  LSM303_REGISTER_ACCEL_CLICK_THS_A = 0x3A,
54  LSM303_REGISTER_ACCEL_TIME_LIMIT_A = 0x3B,
55  LSM303_REGISTER_ACCEL_TIME_LATENCY_A = 0x3C,
56  LSM303_REGISTER_ACCEL_TIME_WINDOW_A = 0x3D
58 
59 /*=========================================================================*/
60 
61 /**************************************************************************/
65 /**************************************************************************/
66 typedef struct lsm303AccelData_s {
67  int16_t x;
68  int16_t y;
69  int16_t z;
71 /*=========================================================================*/
72 
76 typedef enum range {
82 
87 typedef enum mode {
92 
93 #define LSM303_ID (0b11010100)
94 
95 
98 class Adafruit_LSM303_Accel_Unified : public Adafruit_Sensor {
99 public:
100  Adafruit_LSM303_Accel_Unified(int32_t sensorID = -1);
101 
102  bool begin(uint8_t i2c_addr = LSM303_ADDRESS_ACCEL, TwoWire *wire = &Wire);
103  bool getEvent(sensors_event_t *);
104  void getSensor(sensor_t *);
105 
106  void setRange(lsm303_accel_range_t);
107  lsm303_accel_range_t getRange(void);
108 
109  lsm303_accel_mode_t getMode(void);
110  void setMode(lsm303_accel_mode_t);
111 
112  void interruptsActiveHigh(bool);
113 
114 private:
115  int32_t _sensorID;
116 
117  lsm303AccelData raw; // Last read accelerometer data will be available here
118  float getLSB(lsm303_accel_mode_t);
119  uint8_t getShift(lsm303_accel_mode_t);
120 
121  void readRawData(void);
122 
123  Adafruit_I2CDevice *i2c_dev;
124 };
125 
126 #endif
High resolution mode; 12-bit.
Definition: Adafruit_LSM303_Accel.h:89
Measurement range from +2G to -2G (19.61 m/s^2)
Definition: Adafruit_LSM303_Accel.h:77
int16_t z
z-axis data
Definition: Adafruit_LSM303_Accel.h:69
int16_t x
x-axis data
Definition: Adafruit_LSM303_Accel.h:67
mode
Set of different modes that can be used. Normal, high resolution, and low power.
Definition: Adafruit_LSM303_Accel.h:87
Measurement range from +8G to -8G (78.45 m/s^2)
Definition: Adafruit_LSM303_Accel.h:79
lsm303AccelRegisters_t
Registers.
Definition: Adafruit_LSM303_Accel.h:25
Normal measurement mode; 10-bit.
Definition: Adafruit_LSM303_Accel.h:88
Measurement range from +16G to -16G (156.9 m/s^2)
Definition: Adafruit_LSM303_Accel.h:80
enum mode lsm303_accel_mode_t
Set of different modes that can be used. Normal, high resolution, and low power.
Low power mode; 8-bit.
Definition: Adafruit_LSM303_Accel.h:90
Unified sensor driver for the accelerometer.
Definition: Adafruit_LSM303_Accel.h:98
Measurement range from +4G to -4G (39.22 m/s^2)
Definition: Adafruit_LSM303_Accel.h:78
INTERNAL ACCELERATION DATA TYPE.
Definition: Adafruit_LSM303_Accel.h:66
struct lsm303AccelData_s lsm303AccelData
INTERNAL ACCELERATION DATA TYPE.
int16_t y
y-axis data
Definition: Adafruit_LSM303_Accel.h:68
range
Set of linear acceleration measurement ranges.
Definition: Adafruit_LSM303_Accel.h:76
#define LSM303_ADDRESS_ACCEL
I2C address/bits, 0011001x.
Definition: Adafruit_LSM303_Accel.h:20
enum range lsm303_accel_range_t
Set of linear acceleration measurement ranges.