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