Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of MMA8451Q by
MMA8451Q.cpp
00001 /* Copyright (c) 2010-2011 mbed.org, MIT License 00002 * Copyright (c) 2014 Antonio Quevedo, UNICAMP 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00005 * and associated documentation files (the "Software"), to deal in the Software without 00006 * restriction, including without limitation the rights to use, copy, modify, merge, publish, 00007 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the 00008 * Software is furnished to do so, subject to the following conditions: 00009 * 00010 * The above copyright notice and this permission notice shall be included in all copies or 00011 * substantial portions of the Software. 00012 * 00013 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00014 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00015 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00016 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00017 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00018 */ 00019 00020 #include "MMA8451Q.h" 00021 00022 #define ACCEL_I2C_ADDRESS 0x1D<<1 00023 #define REG_STATUS 0x00 00024 #define REG_XYZ_DATA_CFG 0x0E 00025 #define REG_WHO_AM_I 0x0D 00026 #define REG_CTRL_REG1 0x2A 00027 #define REG_CTRL_REG2 0x2B 00028 #define REG_CTRL_REG3 0x2C 00029 #define REG_CTRL_REG4 0x2D 00030 #define REG_CTRL_REG5 0x2E 00031 #define REG_OUT_X_MSB 0x01 00032 #define REG_OUT_Y_MSB 0x03 00033 #define REG_OUT_Z_MSB 0x05 00034 00035 MMA8451Q::MMA8451Q(PinName sda, PinName scl) : m_i2c(sda, scl) { 00036 uint8_t data[2] = {REG_CTRL_REG1, 0x00}; // Puts acc in standby for configuring 00037 writeRegs(data, 2); 00038 data[0] = REG_XYZ_DATA_CFG; // Writing 00 turns off high-pass filter and sets full scale range to 2g 00039 // data[1] = 0x01; for 4g 00040 // data[1] = 0x02; for 8g 00041 writeRegs(data, 2); 00042 data[0] = REG_CTRL_REG2; 00043 data[1] = 0x00; // Disable self-test, software reset and auto-sleep; operates in normal mode 00044 writeRegs(data, 2); 00045 data[0] = REG_CTRL_REG3; // Interrupt polarity low, push-pull output 00046 writeRegs(data, 2); 00047 data[0] = REG_CTRL_REG4; 00048 data[1] = 0x01; // Enables interrupt for data Ready 00049 writeRegs(data, 2); 00050 data[0] = REG_CTRL_REG5; 00051 writeRegs(data, 2); // Routes Data Ready interrupt to INT1 00052 data[0] = REG_CTRL_REG1; 00053 data[1] = 0x09; // Data rate is 800Hz 00054 writeRegs(data, 2); 00055 } 00056 00057 MMA8451Q::~MMA8451Q() { } 00058 00059 void MMA8451Q::getAccAllAxis(int16_t * res) { 00060 uint8_t temp[6]; 00061 readRegs(REG_OUT_X_MSB, temp, 6); 00062 res[0] = (temp[0] * 256) + temp[1]; 00063 res[1] = (temp[2] * 256) + temp[3]; 00064 res[2] = (temp[4] * 256) + temp[5]; 00065 } 00066 00067 void MMA8451Q::readRegs(int addr, uint8_t * data, int len) { 00068 char t[1] = {addr}; 00069 m_i2c.write(ACCEL_I2C_ADDRESS, t, 1, true); 00070 m_i2c.read(ACCEL_I2C_ADDRESS, (char *)data, len); 00071 } 00072 00073 void MMA8451Q::writeRegs(uint8_t * data, int len) { 00074 m_i2c.write(ACCEL_I2C_ADDRESS, (char *)data, len); 00075 }
Generated on Tue Aug 23 2022 22:38:29 by
1.7.2
