LIS3DH / STMicroelectronics / MEMS motion sensor, 3-axis accelerometer library
Diff: LIS3DH.cpp
- Revision:
- 0:5d5aac272642
- Child:
- 1:d4d569952436
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LIS3DH.cpp Fri Aug 29 13:27:20 2014 +0000 @@ -0,0 +1,151 @@ +/* + * mbed library program + * 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 + * http://www.page.sannet.ne.jp/kenjia/index.html + * http://mbed.org/users/kenjiArai/ + * Created: July 14th, 2014 + * Revised: August 29th, 2014 + * + * 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 + * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#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) { + initialize (addr, data_rate, fullscale); +} + +LIS3DH::LIS3DH (I2C& p_i2c, + uint8_t addr, uint8_t data_rate, uint8_t fullscale) : i2c(p_i2c) { + initialize (addr, data_rate, fullscale); +} + +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){ + 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); + // Reg.4 + dbf[0] = LIS3DH_CTRL_REG4; + dbf[1] = 0x08; // High resolution + dbf[1] |= fullscale << 4; + i2c.write(acc_addr, dbf, 2); + switch (fullscale){ + case LIS3DH_FS_2G: + fs_factor = LIS3DH_SENSITIVITY_2G; + break; + case LIS3DH_FS_4G: + fs_factor = LIS3DH_SENSITIVITY_4G; + break; + case LIS3DH_FS_8G: + fs_factor = LIS3DH_SENSITIVITY_8G; + break; + case LIS3DH_FS_16G: + fs_factor = LIS3DH_SENSITIVITY_16G; + break; + default: + ; + } +} + +void LIS3DH::read_reg_data(char *data) { + // X,Y & Z + // manual said that + // In order to read multiple bytes, it is necessary to assert the most significant bit + // 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); +} + +void LIS3DH::read_mg_data(float *dt) { +char data[6]; + + if (acc_ready == 0){ + dt[0] = 0; + dt[1] = 0; + dt[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; +} + +void LIS3DH::read_data(float *dt) { +char data[6]; + + if (acc_ready == 0){ + dt[0] = 0; + dt[1] = 0; + dt[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; +} + +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]; +} + +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)){ + return 0; + } + } + return 1; +} + +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); + } else { + dbf[0] = 0xff; + } + return (uint8_t)dbf[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); + } +}