LIS3DH / STMicroelectronics / MEMS motion sensor, 3-axis accelerometer library
Diff: LIS3DH.cpp
- Revision:
- 5:725df775f168
- Parent:
- 4:64dac49da306
- Child:
- 6:e269772dad35
diff -r 64dac49da306 -r 725df775f168 LIS3DH.cpp --- a/LIS3DH.cpp Sat Dec 27 07:43:57 2014 +0000 +++ b/LIS3DH.cpp Tue Feb 24 12:21:54 2015 +0000 @@ -3,11 +3,11 @@ * LIS3DH MEMS motion sensor: 3-axis "nano" accelerometer, made by STMicroelectronics * http://www.st-japan.co.jp/web/jp/catalog/sense_power/FM89/SC444/PF250725 * - * Copyright (c) 2014 Kenji Arai / JH1PJL + * Copyright (c) 2014,'15 Kenji Arai / JH1PJL * http://www.page.sannet.ne.jp/kenjia/index.html * http://mbed.org/users/kenjiArai/ * Created: July 14th, 2014 - * Revised: December 27th, 2014 + * Revised: Feburary 24th, 2015 * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE @@ -19,38 +19,49 @@ #include "LIS3DH.h" LIS3DH::LIS3DH (PinName p_sda, PinName p_scl, - uint8_t addr, uint8_t data_rate, uint8_t fullscale) : i2c(p_sda, p_scl) { + uint8_t addr, uint8_t data_rate, uint8_t fullscale) : _i2c(p_sda, p_scl) { + _i2c.frequency(400000); initialize (addr, data_rate, fullscale); } +LIS3DH::LIS3DH (PinName p_sda, PinName p_scl, uint8_t addr) : _i2c(p_sda, p_scl) { + _i2c.frequency(400000); + initialize (addr, LIS3DH_DR_NR_LP_50HZ, LIS3DH_FS_8G); +} + LIS3DH::LIS3DH (I2C& p_i2c, - uint8_t addr, uint8_t data_rate, uint8_t fullscale) : i2c(p_i2c) { - i2c.frequency(400000); + uint8_t addr, uint8_t data_rate, uint8_t fullscale) : _i2c(p_i2c) { + _i2c.frequency(400000); initialize (addr, data_rate, fullscale); } +LIS3DH::LIS3DH (I2C& p_i2c, uint8_t addr) : _i2c(p_i2c) { + _i2c.frequency(400000); + initialize (addr, LIS3DH_DR_NR_LP_50HZ, LIS3DH_FS_8G); +} + void LIS3DH::initialize (uint8_t addr, uint8_t data_rate, uint8_t fullscale) { // Check acc is available of not acc_addr = addr; - dbf[0] = LIS3DH_WHO_AM_I; - i2c.write(acc_addr, dbf, 1); - i2c.read(acc_addr, dbf, 1); - if (dbf[0] == I_AM_LIS3DH){ + dt[0] = LIS3DH_WHO_AM_I; + _i2c.write(acc_addr, dt, 1, true); + _i2c.read(acc_addr, dt, 1, false); + if (dt[0] == I_AM_LIS3DH){ acc_ready = 1; } else { acc_ready = 0; return; // acc chip is NOT on I2C line then terminate } // Reg.1 - dbf[0] = LIS3DH_CTRL_REG1; - dbf[1] = 0x07; - dbf[1] |= data_rate << 4; - i2c.write(acc_addr, dbf, 2); + dt[0] = LIS3DH_CTRL_REG1; + dt[1] = 0x07; + dt[1] |= data_rate << 4; + _i2c.write(acc_addr, dt, 2, false); // Reg.4 - dbf[0] = LIS3DH_CTRL_REG4; - dbf[1] = 0x08; // High resolution - dbf[1] |= fullscale << 4; - i2c.write(acc_addr, dbf, 2); + dt[0] = LIS3DH_CTRL_REG4; + dt[1] = 0x08; // High resolution + dt[1] |= fullscale << 4; + _i2c.write(acc_addr, dt, 2, false); switch (fullscale){ case LIS3DH_FS_2G: fs_factor = LIS3DH_SENSITIVITY_2G; @@ -76,56 +87,56 @@ // of the subaddress field. // In other words, SUB(7) must be equal to ‘1’ while SUB(6-0) represents the address // of the first register to be read. - dbf[0] = LIS3DH_OUT_X_L | 0x80; - i2c.write(acc_addr, dbf, 1, true); - i2c.read(acc_addr, data, 6, false); + dt[0] = LIS3DH_OUT_X_L | 0x80; + _i2c.write(acc_addr, dt, 1, true); + _i2c.read(acc_addr, data, 6, false); } -void LIS3DH::read_mg_data(float *dt) { +void LIS3DH::read_mg_data(float *dt_usr) { char data[6]; if (acc_ready == 0){ - dt[0] = 0; - dt[1] = 0; - dt[2] = 0; + dt_usr[0] = 0; + dt_usr[1] = 0; + dt_usr[2] = 0; return; } read_reg_data(data); // change data type - dt[0] = float(short((data[1] << 8) | data[0])) * fs_factor / 15; - dt[1] = float(short((data[3] << 8) | data[2])) * fs_factor / 15; - dt[2] = float(short((data[5] << 8) | data[4])) * fs_factor / 15; + dt_usr[0] = float(short((data[1] << 8) | data[0])) * fs_factor / 15; + dt_usr[1] = float(short((data[3] << 8) | data[2])) * fs_factor / 15; + dt_usr[2] = float(short((data[5] << 8) | data[4])) * fs_factor / 15; } -void LIS3DH::read_data(float *dt) { +void LIS3DH::read_data(float *dt_usr) { char data[6]; if (acc_ready == 0){ - dt[0] = 0; - dt[1] = 0; - dt[2] = 0; + dt_usr[0] = 0; + dt_usr[1] = 0; + dt_usr[2] = 0; return; } read_reg_data(data); // change data type - dt[0] = float(short((data[1] << 8) | data[0])) * fs_factor / 15 * GRAVITY; - dt[1] = float(short((data[3] << 8) | data[2])) * fs_factor / 15 * GRAVITY; - dt[2] = float(short((data[5] << 8) | data[4])) * fs_factor / 15 * GRAVITY; + dt_usr[0] = float(short((data[1] << 8) | data[0])) * fs_factor / 15 * GRAVITY; + dt_usr[1] = float(short((data[3] << 8) | data[2])) * fs_factor / 15 * GRAVITY; + dt_usr[2] = float(short((data[5] << 8) | data[4])) * fs_factor / 15 * GRAVITY; } uint8_t LIS3DH::read_id() { - dbf[0] = LIS3DH_WHO_AM_I; - i2c.write(acc_addr, dbf, 1); - i2c.read(acc_addr, dbf, 1); - return (uint8_t)dbf[0]; + dt[0] = LIS3DH_WHO_AM_I; + _i2c.write(acc_addr, dt, 1, true); + _i2c.read(acc_addr, dt, 1, false); + return (uint8_t)dt[0]; } uint8_t LIS3DH::data_ready() { if (acc_ready == 1){ - dbf[0] = LIS3DH_STATUS_REG_AUX; - i2c.write(acc_addr, dbf, 1); - i2c.read(acc_addr, dbf, 1); - if (!(dbf[0] & 0x01)){ + dt[0] = LIS3DH_STATUS_REG_AUX; + _i2c.write(acc_addr, dt, 1, true); + _i2c.read(acc_addr, dt, 1, false); + if (!(dt[0] & 0x01)){ return 0; } } @@ -133,24 +144,24 @@ } void LIS3DH::frequency(int hz) { - i2c.frequency(hz); + _i2c.frequency(hz); } uint8_t LIS3DH::read_reg(uint8_t addr) { if (acc_ready == 1){ - dbf[0] = addr; - i2c.write(acc_addr, dbf, 1); - i2c.read(acc_addr, dbf, 1); + dt[0] = addr; + _i2c.write(acc_addr, dt, 1, true); + _i2c.read(acc_addr, dt, 1, false); } else { - dbf[0] = 0xff; + dt[0] = 0xff; } - return (uint8_t)dbf[0]; + return (uint8_t)dt[0]; } void LIS3DH::write_reg(uint8_t addr, uint8_t data) { if (acc_ready == 1){ - dbf[0] = addr; - dbf[1] = data; - i2c.write(acc_addr, dbf, 2); + dt[0] = addr; + dt[1] = data; + _i2c.write(acc_addr, dt, 2, false); } }