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

Dependents:   GR-PEACH_test_wo_rtos GR-PEACH_test_on_rtos_works_well Skywire_Demo_3 Skywire_Kinetis_K64_demo ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LIS3DH.h Source File

LIS3DH.h

00001 /*
00002  * mbed library program
00003  *  LIS3DH MEMS motion sensor: 3-axis "nano" accelerometer, made by STMicroelectronics
00004  *      http://www.st-japan.co.jp/web/jp/catalog/sense_power/FM89/SC444/PF250725
00005  *
00006  * Copyright (c) 2014,'15,'17 Kenji Arai / JH1PJL
00007  *  http://www.page.sannet.ne.jp/kenjia/index.html
00008  *  http://mbed.org/users/kenjiArai/
00009  *      Created: July       14th, 2014
00010  *      Revised: August     23rd, 2017
00011  */
00012 
00013 #ifndef LIS3DH_H
00014 #define LIS3DH_H
00015 
00016 #include "mbed.h"
00017 
00018 //  revision 6 have two bugs, (1) read_mg_data, (2) divided by 15 (16 is coorect value)
00019 #define OLD_REV             0       // KEEP 0!! (If you set 1, work as old revision)
00020 
00021 //  LIS3DH Address
00022 //  7bit address = 0b001100x(0x18 or 0x19 depends on SA0/SDO)
00023 #define LIS3DH_G_CHIP_ADDR  (0x18 << 1)    // SA0(=SDO pin) = Ground
00024 #define LIS3DH_V_CHIP_ADDR  (0x19 << 1)    // SA0(=SDO pin) = Vdd
00025 
00026 
00027 //   LIS3DH ID
00028 #define I_AM_LIS3DH            0x33
00029 
00030 //  Register's definition
00031 #define LIS3DH_STATUS_REG_AUX  0x07
00032 #define LIS3DH_OUT_ADC1_L      0x08
00033 #define LIS3DH_OUT_ADC1_H      0x09
00034 #define LIS3DH_OUT_ADC2_L      0x0a
00035 #define LIS3DH_OUT_ADC2_H      0x0b
00036 #define LIS3DH_OUT_ADC3_L      0x0c
00037 #define LIS3DH_OUT_ADC3_H      0x0d
00038 #define LIS3DH_INT_COUNTER_REG 0x0e
00039 #define LIS3DH_WHO_AM_I        0x0f
00040 #define LIS3DH_TEMP_CFG_REG    0x1f
00041 #define LIS3DH_CTRL_REG1       0x20
00042 #define LIS3DH_CTRL_REG2       0x21
00043 #define LIS3DH_CTRL_REG3       0x22
00044 #define LIS3DH_CTRL_REG4       0x23
00045 #define LIS3DH_CTRL_REG5       0x24
00046 #define LIS3DH_CTRL_REG6       0x25
00047 #define LIS3DH_REFERENCE       0x26
00048 #define LIS3DH_STATUS_REG      0x27
00049 #define LIS3DH_OUT_X_L         0x28
00050 #define LIS3DH_OUT_X_H         0x29
00051 #define LIS3DH_OUT_Y_L         0x2a
00052 #define LIS3DH_OUT_Y_H         0x2b
00053 #define LIS3DH_OUT_Z_L         0x2c
00054 #define LIS3DH_OUT_Z_H         0x2d
00055 #define LIS3DH_FIFO_CTRL_REG   0x2e
00056 #define LIS3DH_FIFO_SRC_REG    0x2f
00057 #define LIS3DH_INT1_CFG        0x30
00058 #define LIS3DH_INT1_SOURCE     0x31
00059 #define LIS3DH_INT1_THS        0x32
00060 #define LIS3DH_INT1_DURATION   0x33
00061 #define LIS3DH_CLICK_CFG       0x38
00062 #define LIS3DH_CLICK_SRC       0x39
00063 #define LIS3DH_CLICK_THS       0x3a
00064 #define LIS3DH_TIME_LIMIT      0x3b
00065 #define LIS3DH_TIME_LATENCY    0x3c
00066 #define LIS3DH_TIME_WINDOW     0x3d
00067 
00068 // Output Data Rate (ODR)
00069 #define LIS3DH_DR_PWRDWN       0
00070 #define LIS3DH_DR_NR_LP_1HZ    1
00071 #define LIS3DH_DR_NR_LP_10HZ   2
00072 #define LIS3DH_DR_NR_LP_25HZ   3
00073 #define LIS3DH_DR_NR_LP_50HZ   4
00074 #define LIS3DH_DR_NR_LP_100HZ  5
00075 #define LIS3DH_DR_NR_LP_200HZ  6
00076 #define LIS3DH_DR_NR_LP_400HZ  7
00077 #define LIS3DH_DR_LP_1R6KHZ    8
00078 #define LIS3DH_DR_NR_1R25KHZ   9
00079 
00080 // Bandwidth (Low pass)
00081 #define LIS3DH_BW_LOW          0
00082 #define LIS3DH_BW_M_LOW        1
00083 #define LIS3DH_BW_M_HI         2
00084 #define LIS3DH_BW_HI           3
00085 
00086 // Low power mode enable/disable
00087 #define LIS3DH_LP_EN           0
00088 #define LIS3DH_LP_DIS          1
00089 
00090 // Axis control
00091 #define LIS3DH_X_EN            1
00092 #define LIS3DH_X_DIS           0
00093 #define LIS3DH_Y_EN            1
00094 #define LIS3DH_Y_DIS           0
00095 #define LIS3DH_Z_EN            1
00096 #define LIS3DH_Z_DIS           0
00097 
00098 // Full Scale
00099 #define LIS3DH_FS_2G           0
00100 #define LIS3DH_FS_4G           1
00101 #define LIS3DH_FS_8G           2
00102 #define LIS3DH_FS_16G          3
00103 
00104 // definition for Nomalization
00105 #if OLD_REV
00106 #define LIS3DH_SENSITIVITY_2G  (0.001F)
00107 #define LIS3DH_SENSITIVITY_4G  (0.002F)
00108 #define LIS3DH_SENSITIVITY_8G  (0.004F)
00109 #define LIS3DH_SENSITIVITY_16G (0.012F)
00110 #else
00111 #define LIS3DH_SENSITIVITY_2G  1
00112 #define LIS3DH_SENSITIVITY_4G  2
00113 #define LIS3DH_SENSITIVITY_8G  4
00114 #define LIS3DH_SENSITIVITY_16G 12
00115 #endif
00116 
00117 //Gravity at Earth's surface in m/s/s
00118 #if OLD_REV
00119 #define GRAVITY                (9.80665F)
00120 #else
00121 #define GRAVITY                (9.80665F / 1000)
00122 #endif
00123 
00124 /** Interface for STMicronics MEMS motion sensor: 3-axis "nano" accelerometer
00125  *      Chip: LIS3DH
00126  *
00127  * @code
00128  * #include "mbed.h"
00129  *
00130  * // I2C Communication
00131  * LIS3DH acc(p_sda, p_scl, chip_addr, datarate, bandwidth, fullscale);
00132  * // If you connected I2C line not only this device but also other devices,
00133  * //     you need to declare following method.
00134  * I2C i2c(dp5,dp27);              // SDA, SCL
00135  * LIS3DH acc(i2c, chip_addr, datarate, bandwidth, fullscale);
00136  *
00137  * int main() {
00138  * float f[3];
00139  *
00140  *   if (acc.read_id() == I_AM_LIS3DH){
00141  *      acc.read_data(f);
00142  *   }
00143  * }
00144  * @endcode
00145  */
00146 
00147 class LIS3DH
00148 {
00149 public:
00150     /** Configure data pin
00151       * @param data SDA and SCL pins
00152       * @param device address LIS3DH(SA0=0 or 1), LIS3DH_G_CHIP_ADDR or LIS3DH_V_CHIP_ADDR
00153       * @param output data rate selection, power down mode, 1Hz to 5KHz
00154       * @param full scale selection, +/-2g to +/-16g
00155       */
00156     LIS3DH(PinName p_sda, PinName p_scl,
00157            uint8_t addr, uint8_t data_rate, uint8_t fullscale);
00158 
00159     /** Configure data pin
00160       * @param data SDA and SCL pins
00161       * @param device address LIS3DH(SA0=0 or 1), LIS3DH_G_CHIP_ADDR or LIS3DH_V_CHIP_ADDR
00162       * @default output data rate selection = 50Hz
00163       * @default full scale selection = +/-8g
00164       */
00165     LIS3DH(PinName p_sda, PinName p_scl, uint8_t addr);
00166 
00167     /** Configure data pin (with other devices on I2C line)
00168       * @param I2C previous definition
00169       * @param other parameters -> please see LIS3DH(PinName p_sda, PinName p_scl,...)
00170       */
00171     LIS3DH(I2C& p_i2c,
00172            uint8_t addr, uint8_t data_rate, uint8_t fullscale);
00173 
00174     /** Configure data pin (with other devices on I2C line)
00175       * @param I2C previous definition
00176       * @param other parameters -> please see LIS3DH(PinName p_sda, PinName p_scl,...)
00177       * @default output data rate selection = 50Hz
00178       * @default full scale selection = +/-8g
00179       */
00180     LIS3DH(I2C& p_i2c, uint8_t addr);
00181 
00182     /** Read a float type data from acc
00183       * @param float type of three arry's address, e.g. float dt_usr[3];
00184       * @return acc motion data unit: m/s/s(m/s2)
00185       * @return dt_usr[0]->x, dt_usr[1]->y, dt_usr[2]->z
00186       */
00187     void read_data(float *dt_usr);
00188 
00189     /** Read a float type data from acc
00190       * @param float type of three arry's address, e.g. float dt_usr[3];
00191       * @return acc motion data unit: mg
00192       * @return dt_usr[0]->x, dt_usr[1]->y, dt_usr[2]->z
00193       */
00194     void read_mg_data(float *dt_usr);
00195 
00196     /** Read a acc ID number
00197       * @param none
00198       * @return if STM MEMS acc, it should be I_AM_ LIS3DH(0x33)
00199       */
00200     uint8_t read_id();
00201 
00202     /** Read Data Ready flag
00203       * @param none
00204       * @return 1 = Ready
00205       */
00206     uint8_t data_ready();
00207 
00208     /** Set I2C clock frequency
00209       * @param freq.
00210       * @return none
00211       */
00212     void frequency(int hz);
00213 
00214     /** Read register (general purpose)
00215       * @param register's address
00216       * @return register data
00217       */
00218     uint8_t read_reg(uint8_t addr);
00219 
00220     /** Write register (general purpose)
00221       * @param register's address
00222       * @param data
00223       * @return none
00224       */
00225     void write_reg(uint8_t addr, uint8_t data);
00226 
00227 protected:
00228     void initialize(uint8_t, uint8_t, uint8_t);
00229     void read_reg_data(char *data);
00230 
00231     I2C *_i2c_p;
00232     I2C &_i2c;
00233 
00234 private:
00235 #if OLD_REV
00236     float   fs_factor;  // full scale factor
00237 #else
00238     uint8_t fs_factor;  // full scale factor
00239 #endif
00240     char    dt[2];      // working buffer
00241     uint8_t acc_addr;   // acc sensor address
00242     uint8_t acc_id;     // acc ID
00243     uint8_t acc_ready;  // acc is on I2C line = 1, not = 0
00244 };
00245 
00246 #endif      // LIS3DH_H
00247