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

Committer:
JimCarver
Date:
Mon Apr 07 21:03:37 2014 +0000
Revision:
5:b8512e0de86b
Parent:
3:db7126dbd63f
Child:
6:d3f7851ff32e
Added function to retrieve raw data from sensor

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 1:d2630136d51e 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
samux 1:d2630136d51e 2 *
samux 1:d2630136d51e 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
samux 1:d2630136d51e 4 * and associated documentation files (the "Software"), to deal in the Software without
samux 1:d2630136d51e 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
samux 1:d2630136d51e 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
samux 1:d2630136d51e 7 * Software is furnished to do so, subject to the following conditions:
samux 1:d2630136d51e 8 *
samux 1:d2630136d51e 9 * The above copyright notice and this permission notice shall be included in all copies or
samux 1:d2630136d51e 10 * substantial portions of the Software.
samux 1:d2630136d51e 11 *
samux 1:d2630136d51e 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
samux 1:d2630136d51e 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
samux 1:d2630136d51e 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
samux 1:d2630136d51e 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
samux 1:d2630136d51e 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
samux 1:d2630136d51e 17 */
samux 1:d2630136d51e 18
emilmont 0:6149091f755d 19 #include "MMA8451Q.h"
emilmont 0:6149091f755d 20
samux 1:d2630136d51e 21 #define REG_WHO_AM_I 0x0D
samux 1:d2630136d51e 22 #define REG_CTRL_REG_1 0x2A
emilmont 0:6149091f755d 23 #define REG_OUT_X_MSB 0x01
emilmont 0:6149091f755d 24 #define REG_OUT_Y_MSB 0x03
emilmont 0:6149091f755d 25 #define REG_OUT_Z_MSB 0x05
emilmont 0:6149091f755d 26
samux 1:d2630136d51e 27 #define UINT14_MAX 16383
emilmont 0:6149091f755d 28
emilmont 0:6149091f755d 29 MMA8451Q::MMA8451Q(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) {
emilmont 0:6149091f755d 30 // activate the peripheral
emilmont 0:6149091f755d 31 uint8_t data[2] = {REG_CTRL_REG_1, 0x01};
samux 1:d2630136d51e 32 writeRegs(data, 2);
emilmont 0:6149091f755d 33 }
emilmont 0:6149091f755d 34
JimCarver 5:b8512e0de86b 35
emilmont 0:6149091f755d 36 MMA8451Q::~MMA8451Q() { }
emilmont 0:6149091f755d 37
emilmont 0:6149091f755d 38 uint8_t MMA8451Q::getWhoAmI() {
emilmont 0:6149091f755d 39 uint8_t who_am_i = 0;
samux 1:d2630136d51e 40 readRegs(REG_WHO_AM_I, &who_am_i, 1);
emilmont 0:6149091f755d 41 return who_am_i;
emilmont 0:6149091f755d 42 }
emilmont 0:6149091f755d 43
chris 3:db7126dbd63f 44 float MMA8451Q::getAccX() {
chris 3:db7126dbd63f 45 return (float(getAccAxis(REG_OUT_X_MSB))/4096.0);
emilmont 0:6149091f755d 46 }
emilmont 0:6149091f755d 47
chris 3:db7126dbd63f 48 float MMA8451Q::getAccY() {
chris 3:db7126dbd63f 49 return (float(getAccAxis(REG_OUT_Y_MSB))/4096.0);
emilmont 0:6149091f755d 50 }
emilmont 0:6149091f755d 51
chris 3:db7126dbd63f 52 float MMA8451Q::getAccZ() {
chris 3:db7126dbd63f 53 return (float(getAccAxis(REG_OUT_Z_MSB))/4096.0);
emilmont 0:6149091f755d 54 }
emilmont 0:6149091f755d 55
JimCarver 5:b8512e0de86b 56
chris 3:db7126dbd63f 57 void MMA8451Q::getAccAllAxis(float * res) {
emilmont 0:6149091f755d 58 res[0] = getAccX();
emilmont 0:6149091f755d 59 res[1] = getAccY();
emilmont 0:6149091f755d 60 res[2] = getAccZ();
emilmont 0:6149091f755d 61 }
emilmont 0:6149091f755d 62
JimCarver 5:b8512e0de86b 63 void MMA8451Q::getAccXYZraw(int16_t * d) {
JimCarver 5:b8512e0de86b 64 int16_t acc;
JimCarver 5:b8512e0de86b 65 uint8_t res[6];
JimCarver 5:b8512e0de86b 66 readRegs(REG_OUT_X_MSB, res, 6);
JimCarver 5:b8512e0de86b 67
JimCarver 5:b8512e0de86b 68 acc = (res[0] << 6) | (res[1] >> 2);
JimCarver 5:b8512e0de86b 69 if (acc > UINT14_MAX/2)
JimCarver 5:b8512e0de86b 70 acc -= UINT14_MAX;
JimCarver 5:b8512e0de86b 71 d[0] = acc;
JimCarver 5:b8512e0de86b 72 acc = (res[2] << 6) | (res[3] >> 2);
JimCarver 5:b8512e0de86b 73 if (acc > UINT14_MAX/2)
JimCarver 5:b8512e0de86b 74 acc -= UINT14_MAX;
JimCarver 5:b8512e0de86b 75 d[1] = acc;
JimCarver 5:b8512e0de86b 76 acc = (res[4] << 6) | (res[5] >> 2);
JimCarver 5:b8512e0de86b 77 if (acc > UINT14_MAX/2)
JimCarver 5:b8512e0de86b 78 acc -= UINT14_MAX;
JimCarver 5:b8512e0de86b 79 d[2] = acc;
JimCarver 5:b8512e0de86b 80 }
JimCarver 5:b8512e0de86b 81
emilmont 0:6149091f755d 82 int16_t MMA8451Q::getAccAxis(uint8_t addr) {
emilmont 0:6149091f755d 83 int16_t acc;
emilmont 0:6149091f755d 84 uint8_t res[2];
samux 1:d2630136d51e 85 readRegs(addr, res, 2);
emilmont 0:6149091f755d 86
emilmont 0:6149091f755d 87 acc = (res[0] << 6) | (res[1] >> 2);
emilmont 0:6149091f755d 88 if (acc > UINT14_MAX/2)
emilmont 0:6149091f755d 89 acc -= UINT14_MAX;
emilmont 0:6149091f755d 90
emilmont 0:6149091f755d 91 return acc;
emilmont 0:6149091f755d 92 }
emilmont 0:6149091f755d 93
samux 1:d2630136d51e 94 void MMA8451Q::readRegs(int addr, uint8_t * data, int len) {
emilmont 0:6149091f755d 95 char t[1] = {addr};
emilmont 0:6149091f755d 96 m_i2c.write(m_addr, t, 1, true);
emilmont 0:6149091f755d 97 m_i2c.read(m_addr, (char *)data, len);
emilmont 0:6149091f755d 98 }
emilmont 0:6149091f755d 99
samux 1:d2630136d51e 100 void MMA8451Q::writeRegs(uint8_t * data, int len) {
emilmont 0:6149091f755d 101 m_i2c.write(m_addr, (char *)data, len);
emilmont 0:6149091f755d 102 }