Francesco Franzinelli / LIS3DH_spi

Dependents:   SimpleBLE-ObCP_ENSMM_V2019_Test_BLE SimpleBLE-ObCP_ENSMM_V2019_Test_BLE_S Roller_catcher_tests_fonctionnel SimpleBLE-ObCp_test-BLE_envoi ... 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 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: December  12th, 2015
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
00013  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
00014  * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015  * DAMAGES OR OTHER  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018 
00019 #ifndef LIS3DH_H
00020 #define LIS3DH_H
00021 
00022 #include "mbed.h"
00023 
00024 //  revision 6 have two bugs, (1) read_mg_data, (2) divided by 15 (16 is coorect value)
00025 #define OLD_REV             0       // KEEP 0!! (If you set 1, work as old revision)
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 class LIS3DH
00125 {
00126 public:
00127     /** Configure data pin
00128       * @param data MISO MOSI SCLK and CS pins
00129       * @param output data rate selection, power down mode, 1Hz to 5KHz
00130       * @param full scale selection, +/-2g to +/-16g
00131       */
00132     LIS3DH(PinName p_mosi, PinName p_miso, PinName p_sclk, PinName p_ssel, uint8_t data_rate, uint8_t fullscale);
00133 
00134     /** Configure data pin
00135       * @param data MISO MOSI SCLK and CS pins
00136       * @default output data rate selection = 50Hz
00137       * @default full scale selection = +/-8g
00138       */
00139     LIS3DH (PinName p_mosi, PinName p_miso, PinName p_sclk, PinName p_ssel);
00140     
00141     /** Read a float type data from acc
00142       * @param float type of three arry's address, e.g. float dt_usr[3];
00143       * @return acc motion data unit: m/s/s(m/s2)
00144       * @return dt_usr[0]->x, dt_usr[1]->y, dt_usr[2]->z
00145       */
00146     void read_data(float *dt_usr);
00147 
00148     /** Read a float type data from acc
00149       * @param float type of three arry's address, e.g. float dt_usr[3];
00150       * @return acc motion data unit: mg
00151       * @return dt_usr[0]->x, dt_usr[1]->y, dt_usr[2]->z
00152       */
00153     void read_mg_data(float *dt_usr);
00154 
00155     /** Read a acc ID number
00156       * @param none
00157       * @return if STM MEMS acc, it should be I_AM_ LIS3DH(0x33)
00158       */
00159     uint8_t read_id();
00160 
00161     /** Read Data Ready flag
00162       * @param none
00163       * @return 1 = Ready
00164       */
00165     uint8_t data_ready();
00166 
00167     /** Set I2C clock frequency
00168       * @param freq.
00169       * @return none
00170       */
00171     void set_frequency(int hz);
00172 
00173     /** Read register (general purpose)
00174       * @param register's address
00175       * @return register data
00176       */
00177     uint8_t read_reg(uint8_t addr);
00178 
00179     /** Write register (general purpose)
00180       * @param register's address
00181       * @param data
00182       * @return none
00183       */
00184     void write_reg(uint8_t addr, uint8_t data);
00185     
00186 protected:
00187     void readRegs(uint8_t addr, uint8_t * data, int len);
00188     void writeRegs(uint8_t * data, int len);
00189     void write16(uint8_t addr, uint16_t data16);
00190     uint16_t read16(uint8_t addr);
00191     void init(uint8_t data_rate, uint8_t fullscale);
00192 
00193     void read_reg_data(char *data);
00194     SPI _spi;
00195     DigitalOut _cs ;
00196 
00197 private:
00198 #if OLD_REV
00199     float   fs_factor;  // full scale factor
00200 #else
00201     uint8_t fs_factor;  // full scale factor
00202 #endif
00203     char    dt[2];      // working buffer
00204     uint8_t acc_id;     // acc ID
00205     uint8_t acc_ready;  // acc is on I2C line = 1, not = 0
00206 };
00207 
00208 #endif      // LIS3DH_H
00209 
00210 
00211 
00212