ramiii

Dependencies:   mbed

Committer:
RAMIRORUBIO
Date:
Mon Nov 06 22:37:56 2017 +0000
Revision:
0:49465eeab179
rami;

Who changed what in which revision?

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