LIS3DH / STMicroelectronics / MEMS motion sensor, 3-axis accelerometer library

Dependents:   STM32_teste_5

Committer:
kenjiArai
Date:
Fri Aug 29 13:27:20 2014 +0000
Revision:
0:5d5aac272642
Child:
1:d4d569952436
New creation: LIS3DH / STMicroelectronics / MEMS motion sensor, 3-axis accelerometer library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenjiArai 0:5d5aac272642 1 /*
kenjiArai 0:5d5aac272642 2 * mbed library program
kenjiArai 0:5d5aac272642 3 * LIS3DH MEMS motion sensor: 3-axis "nano" accelerometer, made by STMicroelectronics
kenjiArai 0:5d5aac272642 4 * http://www.st-japan.co.jp/web/jp/catalog/sense_power/FM89/SC444/PF250725
kenjiArai 0:5d5aac272642 5 *
kenjiArai 0:5d5aac272642 6 * Copyright (c) 2014 Kenji Arai / JH1PJL
kenjiArai 0:5d5aac272642 7 * http://www.page.sannet.ne.jp/kenjia/index.html
kenjiArai 0:5d5aac272642 8 * http://mbed.org/users/kenjiArai/
kenjiArai 0:5d5aac272642 9 * Created: July 14th, 2014
kenjiArai 0:5d5aac272642 10 * Revised: August 29th, 2014
kenjiArai 0:5d5aac272642 11 *
kenjiArai 0:5d5aac272642 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
kenjiArai 0:5d5aac272642 13 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
kenjiArai 0:5d5aac272642 14 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
kenjiArai 0:5d5aac272642 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
kenjiArai 0:5d5aac272642 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
kenjiArai 0:5d5aac272642 17 */
kenjiArai 0:5d5aac272642 18
kenjiArai 0:5d5aac272642 19 #ifndef LIS3DH_H
kenjiArai 0:5d5aac272642 20 #define LIS3DH_H
kenjiArai 0:5d5aac272642 21
kenjiArai 0:5d5aac272642 22 #include "mbed.h"
kenjiArai 0:5d5aac272642 23
kenjiArai 0:5d5aac272642 24 // LIS3DH Address
kenjiArai 0:5d5aac272642 25 // 7bit address = 0b001100x(0x18 or 0x19 depends on SA0/SDO)
kenjiArai 0:5d5aac272642 26 // -> 8bit = 0b001100x0(0x30,0x32) -> 0x31,0x33(Read) or 0x30,0x32(Write)
kenjiArai 0:5d5aac272642 27 #define LIS3DH_G_CHIP_ADDR 0x30 // SA0(=SDO pin) = Ground
kenjiArai 0:5d5aac272642 28 #define LIS3DH_V_CHIP_ADDR 0x32 // SA0(=SDO pin) = Vdd
kenjiArai 0:5d5aac272642 29
kenjiArai 0:5d5aac272642 30
kenjiArai 0:5d5aac272642 31 // LIS3DH ID
kenjiArai 0:5d5aac272642 32 #define I_AM_LIS3DH 0x33
kenjiArai 0:5d5aac272642 33
kenjiArai 0:5d5aac272642 34 // Register's definition
kenjiArai 0:5d5aac272642 35 #define LIS3DH_STATUS_REG_AUX 0x07
kenjiArai 0:5d5aac272642 36 #define LIS3DH_OUT_ADC1_L 0x08
kenjiArai 0:5d5aac272642 37 #define LIS3DH_OUT_ADC1_H 0x09
kenjiArai 0:5d5aac272642 38 #define LIS3DH_OUT_ADC2_L 0x0a
kenjiArai 0:5d5aac272642 39 #define LIS3DH_OUT_ADC2_H 0x0b
kenjiArai 0:5d5aac272642 40 #define LIS3DH_OUT_ADC3_L 0x0c
kenjiArai 0:5d5aac272642 41 #define LIS3DH_OUT_ADC3_H 0x0d
kenjiArai 0:5d5aac272642 42 #define LIS3DH_INT_COUNTER_REG 0x0e
kenjiArai 0:5d5aac272642 43 #define LIS3DH_WHO_AM_I 0x0f
kenjiArai 0:5d5aac272642 44 #define LIS3DH_TEMP_CFG_REG 0x1f
kenjiArai 0:5d5aac272642 45 #define LIS3DH_CTRL_REG1 0x20
kenjiArai 0:5d5aac272642 46 #define LIS3DH_CTRL_REG2 0x21
kenjiArai 0:5d5aac272642 47 #define LIS3DH_CTRL_REG3 0x22
kenjiArai 0:5d5aac272642 48 #define LIS3DH_CTRL_REG4 0x23
kenjiArai 0:5d5aac272642 49 #define LIS3DH_CTRL_REG5 0x24
kenjiArai 0:5d5aac272642 50 #define LIS3DH_CTRL_REG6 0x25
kenjiArai 0:5d5aac272642 51 #define LIS3DH_REFERENCE 0x26
kenjiArai 0:5d5aac272642 52 #define LIS3DH_STATUS_REG2 0x27
kenjiArai 0:5d5aac272642 53 #define LIS3DH_OUT_X_L 0x28
kenjiArai 0:5d5aac272642 54 #define LIS3DH_OUT_X_H 0x29
kenjiArai 0:5d5aac272642 55 #define LIS3DH_OUT_Y_L 0x2a
kenjiArai 0:5d5aac272642 56 #define LIS3DH_OUT_Y_H 0x2b
kenjiArai 0:5d5aac272642 57 #define LIS3DH_OUT_Z_L 0x2c
kenjiArai 0:5d5aac272642 58 #define LIS3DH_OUT_Z_H 0x2d
kenjiArai 0:5d5aac272642 59 #define LIS3DH_FIFO_CTRL_REG 0x2e
kenjiArai 0:5d5aac272642 60 #define LIS3DH_FIFO_SRC_REG 0x2f
kenjiArai 0:5d5aac272642 61 #define LIS3DH_INT1_CFG 0x30
kenjiArai 0:5d5aac272642 62 #define LIS3DH_INT1_SOURCE 0x31
kenjiArai 0:5d5aac272642 63 #define LIS3DH_INT1_THS 0x32
kenjiArai 0:5d5aac272642 64 #define LIS3DH_INT1_DURATION 0x33
kenjiArai 0:5d5aac272642 65 #define LIS3DH_CLICK_CFG 0x38
kenjiArai 0:5d5aac272642 66 #define LIS3DH_CLICK_SRC 0x39
kenjiArai 0:5d5aac272642 67 #define LIS3DH_CLICK_THS 0x3a
kenjiArai 0:5d5aac272642 68 #define LIS3DH_TIME_LIMIT 0x3b
kenjiArai 0:5d5aac272642 69 #define LIS3DH_TIME_LATENCY 0x3c
kenjiArai 0:5d5aac272642 70 #define LIS3DH_TIME_WINDOW 0x3d
kenjiArai 0:5d5aac272642 71
kenjiArai 0:5d5aac272642 72 // Output Data Rate (ODR)
kenjiArai 0:5d5aac272642 73 #define LIS3DH_DR_PWRDWN 0
kenjiArai 0:5d5aac272642 74 #define LIS3DH_DR_NR_LP_1HZ 1
kenjiArai 0:5d5aac272642 75 #define LIS3DH_DR_NR_LP_10HZ 2
kenjiArai 0:5d5aac272642 76 #define LIS3DH_DR_NR_LP_25HZ 3
kenjiArai 0:5d5aac272642 77 #define LIS3DH_DR_NR_LP_50HZ 4
kenjiArai 0:5d5aac272642 78 #define LIS3DH_DR_NR_LP_100HZ 5
kenjiArai 0:5d5aac272642 79 #define LIS3DH_DR_NR_LP_200HZ 6
kenjiArai 0:5d5aac272642 80 #define LIS3DH_DR_NR_LP_400HZ 7
kenjiArai 0:5d5aac272642 81 #define LIS3DH_DR_LP_1R6KHZ 8
kenjiArai 0:5d5aac272642 82 #define LIS3DH_DR_NR_1R25KHZ 9
kenjiArai 0:5d5aac272642 83
kenjiArai 0:5d5aac272642 84 // Bandwidth (Low pass)
kenjiArai 0:5d5aac272642 85 #define LIS3DH_BW_LOW 0
kenjiArai 0:5d5aac272642 86 #define LIS3DH_BW_M_LOW 1
kenjiArai 0:5d5aac272642 87 #define LIS3DH_BW_M_HI 2
kenjiArai 0:5d5aac272642 88 #define LIS3DH_BW_HI 3
kenjiArai 0:5d5aac272642 89
kenjiArai 0:5d5aac272642 90 // Low power mode enable/disable
kenjiArai 0:5d5aac272642 91 #define LIS3DH_LP_EN 0
kenjiArai 0:5d5aac272642 92 #define LIS3DH_LP_DIS 1
kenjiArai 0:5d5aac272642 93
kenjiArai 0:5d5aac272642 94 // Axis control
kenjiArai 0:5d5aac272642 95 #define LIS3DH_X_EN 1
kenjiArai 0:5d5aac272642 96 #define LIS3DH_X_DIS 0
kenjiArai 0:5d5aac272642 97 #define LIS3DH_Y_EN 1
kenjiArai 0:5d5aac272642 98 #define LIS3DH_Y_DIS 0
kenjiArai 0:5d5aac272642 99 #define LIS3DH_Z_EN 1
kenjiArai 0:5d5aac272642 100 #define LIS3DH_Z_DIS 0
kenjiArai 0:5d5aac272642 101
kenjiArai 0:5d5aac272642 102 // Full Scale
kenjiArai 0:5d5aac272642 103 #define LIS3DH_FS_2G 0
kenjiArai 0:5d5aac272642 104 #define LIS3DH_FS_4G 1
kenjiArai 0:5d5aac272642 105 #define LIS3DH_FS_8G 2
kenjiArai 0:5d5aac272642 106 #define LIS3DH_FS_16G 3
kenjiArai 0:5d5aac272642 107
kenjiArai 0:5d5aac272642 108 // definition for Nomalization
kenjiArai 0:5d5aac272642 109 #define LIS3DH_SENSITIVITY_2G (0.001F)
kenjiArai 0:5d5aac272642 110 #define LIS3DH_SENSITIVITY_4G (0.002F)
kenjiArai 0:5d5aac272642 111 #define LIS3DH_SENSITIVITY_8G (0.004F)
kenjiArai 0:5d5aac272642 112 #define LIS3DH_SENSITIVITY_16G (0.012F)
kenjiArai 0:5d5aac272642 113
kenjiArai 0:5d5aac272642 114 //Gravity at Earth's surface in m/s/s
kenjiArai 0:5d5aac272642 115 #define GRAVITY (9.80665F)
kenjiArai 0:5d5aac272642 116
kenjiArai 0:5d5aac272642 117 /** Interface for STMicronics MEMS motion sensor: 3-axis "nano" accelerometer
kenjiArai 0:5d5aac272642 118 * Chip: LIS3DH
kenjiArai 0:5d5aac272642 119 *
kenjiArai 0:5d5aac272642 120 * @code
kenjiArai 0:5d5aac272642 121 * #include "mbed.h"
kenjiArai 0:5d5aac272642 122 *
kenjiArai 0:5d5aac272642 123 * // I2C Communication
kenjiArai 0:5d5aac272642 124 * LIS3DH acc(p_sda, p_scl, chip_addr, datarate, bandwidth, fullscale);
kenjiArai 0:5d5aac272642 125 * // If you connected I2C line not only this device but also other devices,
kenjiArai 0:5d5aac272642 126 * // you need to declare following method.
kenjiArai 0:5d5aac272642 127 * I2C i2c(dp5,dp27); // SDA, SCL
kenjiArai 0:5d5aac272642 128 * LIS3DH acc(i2c, chip_addr, datarate, bandwidth, fullscale);
kenjiArai 0:5d5aac272642 129 *
kenjiArai 0:5d5aac272642 130 * int main() {
kenjiArai 0:5d5aac272642 131 * float f[3];
kenjiArai 0:5d5aac272642 132 *
kenjiArai 0:5d5aac272642 133 * if (acc.read_id() == I_AM_LIS3DH){
kenjiArai 0:5d5aac272642 134 * acc.read_data(f);
kenjiArai 0:5d5aac272642 135 * }
kenjiArai 0:5d5aac272642 136 * }
kenjiArai 0:5d5aac272642 137 * @endcode
kenjiArai 0:5d5aac272642 138 */
kenjiArai 0:5d5aac272642 139
kenjiArai 0:5d5aac272642 140 class LIS3DH{
kenjiArai 0:5d5aac272642 141 public:
kenjiArai 0:5d5aac272642 142 /** Configure data pin
kenjiArai 0:5d5aac272642 143 * @param data SDA and SCL pins
kenjiArai 0:5d5aac272642 144 * @param device address LIS3DH(SA0=0 or 1), LIS3DH_G_CHIP_ADDR or LIS3DH_V_CHIP_ADDR
kenjiArai 0:5d5aac272642 145 * @param output data rate selection, power down mode, 1Hz to 5KHz
kenjiArai 0:5d5aac272642 146 * @param full scale selection, +/-2g to +/-16g
kenjiArai 0:5d5aac272642 147 */
kenjiArai 0:5d5aac272642 148 LIS3DH(PinName p_sda, PinName p_scl,
kenjiArai 0:5d5aac272642 149 uint8_t addr, uint8_t data_rate, uint8_t fullscale);
kenjiArai 0:5d5aac272642 150
kenjiArai 0:5d5aac272642 151 /** Configure data pin (with other devices on I2C line)
kenjiArai 0:5d5aac272642 152 * @param I2C previous definition
kenjiArai 0:5d5aac272642 153 * @param other parameters -> please see LIS3DH(PinName p_sda, PinName p_scl,...)
kenjiArai 0:5d5aac272642 154 */
kenjiArai 0:5d5aac272642 155 LIS3DH(I2C& p_i2c,
kenjiArai 0:5d5aac272642 156 uint8_t addr, uint8_t data_rate, uint8_t fullscale);
kenjiArai 0:5d5aac272642 157
kenjiArai 0:5d5aac272642 158 /** Read a float type data from acc
kenjiArai 0:5d5aac272642 159 * @param float type of three arry's address, e.g. float dt[3];
kenjiArai 0:5d5aac272642 160 * @return acc motion data unit: m/s/s(m/s2)
kenjiArai 0:5d5aac272642 161 * @return dt[0]->x, dt[1]->y, dt[2]->z
kenjiArai 0:5d5aac272642 162 */
kenjiArai 0:5d5aac272642 163 void read_data(float *dt);
kenjiArai 0:5d5aac272642 164
kenjiArai 0:5d5aac272642 165 /** Read a float type data from acc
kenjiArai 0:5d5aac272642 166 * @param float type of three arry's address, e.g. float dt[3];
kenjiArai 0:5d5aac272642 167 * @return acc motion data unit: mg
kenjiArai 0:5d5aac272642 168 * @return dt[0]->x, dt[1]->y, dt[2]->z
kenjiArai 0:5d5aac272642 169 */
kenjiArai 0:5d5aac272642 170 void read_mg_data(float *dt);
kenjiArai 0:5d5aac272642 171
kenjiArai 0:5d5aac272642 172 /** Read a acc ID number
kenjiArai 0:5d5aac272642 173 * @param none
kenjiArai 0:5d5aac272642 174 * @return if STM MEMS acc, it should be I_AM_ LIS3DH(0x33)
kenjiArai 0:5d5aac272642 175 */
kenjiArai 0:5d5aac272642 176 uint8_t read_id();
kenjiArai 0:5d5aac272642 177
kenjiArai 0:5d5aac272642 178 /** Read Data Ready flag
kenjiArai 0:5d5aac272642 179 * @param none
kenjiArai 0:5d5aac272642 180 * @return 1 = Ready
kenjiArai 0:5d5aac272642 181 */
kenjiArai 0:5d5aac272642 182 uint8_t data_ready();
kenjiArai 0:5d5aac272642 183
kenjiArai 0:5d5aac272642 184 /** Read register (general purpose)
kenjiArai 0:5d5aac272642 185 * @param register's address
kenjiArai 0:5d5aac272642 186 * @return register data
kenjiArai 0:5d5aac272642 187 */
kenjiArai 0:5d5aac272642 188 uint8_t read_reg(uint8_t addr);
kenjiArai 0:5d5aac272642 189
kenjiArai 0:5d5aac272642 190 /** Write register (general purpose)
kenjiArai 0:5d5aac272642 191 * @param register's address
kenjiArai 0:5d5aac272642 192 * @param data
kenjiArai 0:5d5aac272642 193 * @return none
kenjiArai 0:5d5aac272642 194 */
kenjiArai 0:5d5aac272642 195 void write_reg(uint8_t addr, uint8_t data);
kenjiArai 0:5d5aac272642 196
kenjiArai 0:5d5aac272642 197 protected:
kenjiArai 0:5d5aac272642 198 void initialize(uint8_t, uint8_t, uint8_t);
kenjiArai 0:5d5aac272642 199 void read_reg_data(char *data);
kenjiArai 0:5d5aac272642 200
kenjiArai 0:5d5aac272642 201 I2C i2c;
kenjiArai 0:5d5aac272642 202
kenjiArai 0:5d5aac272642 203 private:
kenjiArai 0:5d5aac272642 204 float fs_factor; // full scale factor
kenjiArai 0:5d5aac272642 205 char dbf[2]; // working buffer
kenjiArai 0:5d5aac272642 206 uint8_t acc_addr; // acc sensor address
kenjiArai 0:5d5aac272642 207 uint8_t acc_id; // acc ID
kenjiArai 0:5d5aac272642 208 uint8_t acc_ready; // acc is on I2C line = 1, not = 0
kenjiArai 0:5d5aac272642 209 };
kenjiArai 0:5d5aac272642 210
kenjiArai 0:5d5aac272642 211 #endif // LIS3DH_H