Added function to retrieve raw data from sensor

Dependencies:   MotionSensor

Dependents:   KL46_eCompass KL46_eCompass_TiltCompensed_Acel-Mag Ragnarok_2ejes compass_acc ... more

Fork of MMA8451Q by Emilio Monti

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MMA8451Q.cpp Source File

MMA8451Q.cpp

00001 /* Copyright (c) 2010-2011 mbed.org, MIT License
00002 *
00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004 * and associated documentation files (the "Software"), to deal in the Software without
00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
00007 * Software is furnished to do so, subject to the following conditions:
00008 *
00009 * The above copyright notice and this permission notice shall be included in all copies or
00010 * substantial portions of the Software.
00011 *
00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014 * 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 #include "MMA8451Q.h"
00020 #include "MotionSensor.h"
00021 
00022 #define REG_WHOAMI      0x0D
00023 #define REG_CTRL_REG_1    0x2A
00024 #define REG_DR_STATUS     0x00
00025 #define REG_OUT_X_MSB     0x01
00026 #define REG_OUT_Y_MSB     0x03
00027 #define REG_OUT_Z_MSB     0x05
00028 
00029 #define UINT14_MAX        16383
00030 
00031 float countsperG = 4096.0f; // default 2G range
00032 
00033 MMA8451Q::MMA8451Q(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) {
00034     // activate the peripheral
00035     uint8_t data[2] = {REG_CTRL_REG_1, 0x00};
00036     writeRegs(data, 2);
00037 }
00038 
00039 MMA8451Q::~MMA8451Q() { }
00040 
00041 void MMA8451Q::enable(void) {
00042     uint8_t data[2];
00043     readRegs( REG_CTRL_REG_1, &data[1], 1);
00044     data[1] |= 0x01;
00045     data[0] = REG_CTRL_REG_1;
00046     writeRegs(data, 2);
00047 }
00048 
00049 void MMA8451Q::disable(void) {
00050     uint8_t data[2];
00051     readRegs( REG_CTRL_REG_1, &data[1], 1);
00052     data[1] &= 0xFE;
00053     data[0] = REG_CTRL_REG_1;
00054     writeRegs(data, 2);
00055 }
00056 
00057 uint32_t MMA8451Q::whoAmI() {
00058     uint8_t who_am_i = 0;
00059     readRegs(REG_WHOAMI, &who_am_i, 1);
00060     return (uint32_t) who_am_i;
00061 }
00062 
00063 uint32_t MMA8451Q::dataReady(void) {
00064     uint8_t stat = 0;
00065     readRegs(REG_DR_STATUS, &stat, 1);
00066     return (uint32_t) stat;
00067 }
00068 
00069 uint32_t MMA8451Q::sampleRate(uint32_t f) {
00070     return(50); // for now sample rate is fixed at 50Hz
00071 }
00072 
00073 void MMA8451Q::getX(float * x) {
00074     *x = (float(getAccAxis(REG_OUT_X_MSB)) / countsperG);
00075 }
00076 
00077 void MMA8451Q::getY(float * y) {
00078     *y = (float(getAccAxis(REG_OUT_Y_MSB)) / countsperG);
00079 }
00080 
00081 void MMA8451Q::getZ(float * z) {
00082     *z = (float(getAccAxis(REG_OUT_Z_MSB)) / countsperG);
00083 }
00084 
00085 void MMA8451Q::getX(int16_t * d) {
00086     *d = getAccAxis(REG_OUT_X_MSB);
00087 }
00088 
00089 void MMA8451Q::getY(int16_t * d) {
00090     *d = getAccAxis(REG_OUT_Y_MSB);
00091 }
00092 
00093 void MMA8451Q::getZ(int16_t * d) {
00094     *d = getAccAxis(REG_OUT_Z_MSB);
00095 }
00096 
00097 int16_t MMA8451Q::getAccAxis(uint8_t addr) {
00098     int16_t acc;
00099     uint8_t res[2];
00100     readRegs(addr, res, 2);
00101 
00102     acc = (res[0] << 8) | res[1];
00103     acc = acc >> 2;
00104     return acc;
00105 }
00106 
00107 
00108 void MMA8451Q::getAxis(MotionSensorDataUnits &data) {
00109     int16_t t[3];
00110     uint8_t res[6];
00111 
00112     readRegs(REG_OUT_X_MSB, res, 6);
00113     t[0] = (res[0] << 8) | res[1];
00114     t[1] = (res[2] << 8) | res[3];
00115     t[2] = (res[4] << 8) | res[5];
00116     data.x = ((float) (t[0] >> 2))  / countsperG;
00117     data.y = ((float) (t[1] >> 2))  / countsperG;
00118     data.z = ((float) (t[2] >> 2))  / countsperG;
00119 }
00120 
00121 
00122 void MMA8451Q::getAxis(MotionSensorDataCounts &data) {
00123     int16_t acc;
00124     uint8_t res[6];
00125     readRegs(REG_OUT_X_MSB, res, 6);
00126 
00127 
00128     acc = (res[0] << 8) | res[1];
00129     data.x = acc >> 2;
00130     acc = (res[2] << 8) | res[3];
00131     data.y = acc >> 2;
00132     acc = (res[4] << 8) | res[5];
00133     data.z = acc >> 2;
00134 }
00135 
00136 void MMA8451Q::readRegs(int addr, uint8_t * data, int len) {
00137     char t[1] = {addr};
00138     m_i2c.write(m_addr, t, 1, true);
00139     m_i2c.read(m_addr, (char *)data, len);
00140 }
00141 
00142 void MMA8451Q::writeRegs(uint8_t * data, int len) {
00143     m_i2c.write(m_addr, (char *)data, len);
00144 }