This repository has been archived by the owner on Oct 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Adafruit_LSM303_U.h
211 lines (182 loc) · 8.51 KB
/
Adafruit_LSM303_U.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/***************************************************************************
This is a library for the LSM303 Accelerometer and magnentometer/compass
Designed specifically to work with the Adafruit LSM303DLHC Breakout
These displays use I2C to communicate, 2 pins are required to interface.
Adafruit invests time and resources providing this open source code,
please support Adafruit andopen-source hardware by purchasing products
from Adafruit!
Written by Kevin Townsend for Adafruit Industries.
BSD license, all text above must be included in any redistribution
***************************************************************************/
#ifndef __LSM303_H__
#define __LSM303_H__
#if (ARDUINO >= 100)
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Adafruit_Sensor.h>
#include <Wire.h>
/*=========================================================================
I2C ADDRESS/BITS
-----------------------------------------------------------------------*/
#define LSM303_ADDRESS_ACCEL (0x32 >> 1) // 0011001x
#define LSM303_ADDRESS_MAG (0x3C >> 1) // 0011110x
/*=========================================================================*/
/*=========================================================================
REGISTERS
-----------------------------------------------------------------------*/
typedef enum
{ // DEFAULT TYPE
LSM303_REGISTER_ACCEL_CTRL_REG1_A = 0x20, // 00000111 rw
LSM303_REGISTER_ACCEL_CTRL_REG2_A = 0x21, // 00000000 rw
LSM303_REGISTER_ACCEL_CTRL_REG3_A = 0x22, // 00000000 rw
LSM303_REGISTER_ACCEL_CTRL_REG4_A = 0x23, // 00000000 rw
LSM303_REGISTER_ACCEL_CTRL_REG5_A = 0x24, // 00000000 rw
LSM303_REGISTER_ACCEL_CTRL_REG6_A = 0x25, // 00000000 rw
LSM303_REGISTER_ACCEL_REFERENCE_A = 0x26, // 00000000 r
LSM303_REGISTER_ACCEL_STATUS_REG_A = 0x27, // 00000000 r
LSM303_REGISTER_ACCEL_OUT_X_L_A = 0x28,
LSM303_REGISTER_ACCEL_OUT_X_H_A = 0x29,
LSM303_REGISTER_ACCEL_OUT_Y_L_A = 0x2A,
LSM303_REGISTER_ACCEL_OUT_Y_H_A = 0x2B,
LSM303_REGISTER_ACCEL_OUT_Z_L_A = 0x2C,
LSM303_REGISTER_ACCEL_OUT_Z_H_A = 0x2D,
LSM303_REGISTER_ACCEL_FIFO_CTRL_REG_A = 0x2E,
LSM303_REGISTER_ACCEL_FIFO_SRC_REG_A = 0x2F,
LSM303_REGISTER_ACCEL_INT1_CFG_A = 0x30,
LSM303_REGISTER_ACCEL_INT1_SOURCE_A = 0x31,
LSM303_REGISTER_ACCEL_INT1_THS_A = 0x32,
LSM303_REGISTER_ACCEL_INT1_DURATION_A = 0x33,
LSM303_REGISTER_ACCEL_INT2_CFG_A = 0x34,
LSM303_REGISTER_ACCEL_INT2_SOURCE_A = 0x35,
LSM303_REGISTER_ACCEL_INT2_THS_A = 0x36,
LSM303_REGISTER_ACCEL_INT2_DURATION_A = 0x37,
LSM303_REGISTER_ACCEL_CLICK_CFG_A = 0x38,
LSM303_REGISTER_ACCEL_CLICK_SRC_A = 0x39,
LSM303_REGISTER_ACCEL_CLICK_THS_A = 0x3A,
LSM303_REGISTER_ACCEL_TIME_LIMIT_A = 0x3B,
LSM303_REGISTER_ACCEL_TIME_LATENCY_A = 0x3C,
LSM303_REGISTER_ACCEL_TIME_WINDOW_A = 0x3D
} lsm303AccelRegisters_t;
typedef enum
{
LSM303_REGISTER_MAG_CRA_REG_M = 0x00,
LSM303_REGISTER_MAG_CRB_REG_M = 0x01,
LSM303_REGISTER_MAG_MR_REG_M = 0x02,
LSM303_REGISTER_MAG_OUT_X_H_M = 0x03,
LSM303_REGISTER_MAG_OUT_X_L_M = 0x04,
LSM303_REGISTER_MAG_OUT_Z_H_M = 0x05,
LSM303_REGISTER_MAG_OUT_Z_L_M = 0x06,
LSM303_REGISTER_MAG_OUT_Y_H_M = 0x07,
LSM303_REGISTER_MAG_OUT_Y_L_M = 0x08,
LSM303_REGISTER_MAG_SR_REG_Mg = 0x09,
LSM303_REGISTER_MAG_IRA_REG_M = 0x0A,
LSM303_REGISTER_MAG_IRB_REG_M = 0x0B,
LSM303_REGISTER_MAG_IRC_REG_M = 0x0C,
LSM303_REGISTER_MAG_TEMP_OUT_H_M = 0x31,
LSM303_REGISTER_MAG_TEMP_OUT_L_M = 0x32
} lsm303MagRegisters_t;
/*=========================================================================*/
/*=========================================================================
MAGNETOMETER GAIN SETTINGS
-----------------------------------------------------------------------*/
typedef enum
{
LSM303_MAGGAIN_1_3 = 0x20, // +/- 1.3
LSM303_MAGGAIN_1_9 = 0x40, // +/- 1.9
LSM303_MAGGAIN_2_5 = 0x60, // +/- 2.5
LSM303_MAGGAIN_4_0 = 0x80, // +/- 4.0
LSM303_MAGGAIN_4_7 = 0xA0, // +/- 4.7
LSM303_MAGGAIN_5_6 = 0xC0, // +/- 5.6
LSM303_MAGGAIN_8_1 = 0xE0 // +/- 8.1
} lsm303MagGain;
/*=========================================================================*/
/*=========================================================================
MAGNETOMETER UPDATE RATE SETTINGS
-----------------------------------------------------------------------*/
typedef enum
{
LSM303_MAGRATE_0_7 = 0x00, // 0.75 Hz
LSM303_MAGRATE_1_5 = 0x01, // 1.5 Hz
LSM303_MAGRATE_3_0 = 0x62, // 3.0 Hz
LSM303_MAGRATE_7_5 = 0x03, // 7.5 Hz
LSM303_MAGRATE_15 = 0x04, // 15 Hz
LSM303_MAGRATE_30 = 0x05, // 30 Hz
LSM303_MAGRATE_75 = 0x06, // 75 Hz
LSM303_MAGRATE_220 = 0x07 // 200 Hz
} lsm303MagRate;
/*=========================================================================*/
/*=========================================================================
INTERNAL MAGNETOMETER DATA TYPE
-----------------------------------------------------------------------*/
typedef struct lsm303MagData_s
{
int16_t x;
int16_t y;
int16_t z;
} lsm303MagData;
/*=========================================================================*/
/*=========================================================================
INTERNAL ACCELERATION DATA TYPE
-----------------------------------------------------------------------*/
typedef struct lsm303AccelData_s
{
int16_t x;
int16_t y;
int16_t z;
} lsm303AccelData;
/*=========================================================================*/
/*=========================================================================
CHIP ID
-----------------------------------------------------------------------*/
#define LSM303_ID (0b11010100)
/*=========================================================================*/
/* Unified sensor driver for the accelerometer */
class Adafruit_LSM303_Accel_Unified : public Adafruit_Sensor
{
public:
Adafruit_LSM303_Accel_Unified(int32_t sensorID = -1);
bool begin(void);
bool getEvent(sensors_event_t*);
void getSensor(sensor_t*);
lsm303AccelData raw; // Last read accelerometer data will be available here
private:
int32_t _sensorID;
void write8(byte address, byte reg, byte value);
byte read8(byte address, byte reg);
void read(void);
};
/* Unified sensor driver for the magnetometer */
class Adafruit_LSM303_Mag_Unified : public Adafruit_Sensor
{
public:
Adafruit_LSM303_Mag_Unified(int32_t sensorID = -1);
bool begin(void);
void enableAutoRange(bool enable);
void setMagGain(lsm303MagGain gain);
void setMagRate(lsm303MagRate rate);
bool getEvent(sensors_event_t*);
void getSensor(sensor_t*);
lsm303MagData raw; // Last read magnetometer data will be available here
lsm303MagGain magGain;
bool autoRangeEnabled;
private:
int32_t _sensorID;
void write8(byte address, byte reg, byte value);
byte read8(byte address, byte reg);
void read(void);
};
/* Non Unified (old) driver for compatibility reasons */
class Adafruit_LSM303
{
public:
bool begin(void);
void read(void);
void setMagGain(lsm303MagGain gain);
lsm303AccelData accelData; // Last read accelerometer data will be available here
lsm303MagData magData; // Last read magnetometer data will be available here
void write8(byte address, byte reg, byte value);
byte read8(byte address, byte reg);
};
#endif