HW-6.1

Fork of MMA8451Q by Emilio Monti

Committer:
mireyarod23
Date:
Wed Feb 22 05:45:05 2017 +0000
Revision:
13:0155b9a9851e
Parent:
12:e54564bbb2eb
Stuck

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
mireyarod23 7:2a107e6e0df4 19
mireyarod23 7:2a107e6e0df4 20
emilmont 0:6149091f755d 21 #include "MMA8451Q.h"
emilmont 0:6149091f755d 22
samux 1:d2630136d51e 23 #define REG_WHO_AM_I 0x0D
samux 1:d2630136d51e 24 #define REG_CTRL_REG_1 0x2A
emilmont 0:6149091f755d 25 #define REG_OUT_X_MSB 0x01
emilmont 0:6149091f755d 26 #define REG_OUT_Y_MSB 0x03
emilmont 0:6149091f755d 27 #define REG_OUT_Z_MSB 0x05
mireyarod23 5:8c00640cc051 28 #define XYZ_DATA_CFG 0x0E
emilmont 0:6149091f755d 29
samux 1:d2630136d51e 30 #define UINT14_MAX 16383
mireyarod23 7:2a107e6e0df4 31
mireyarod23 5:8c00640cc051 32 #define MAX_2G 0x00
mireyarod23 5:8c00640cc051 33 #define MAX_4G 0X01
mireyarod23 7:2a107e6e0df4 34 #define MAX_8G 0X10
mireyarod23 5:8c00640cc051 35
mireyarod23 5:8c00640cc051 36 #define GSCALING 1024.0
emilmont 0:6149091f755d 37
emilmont 0:6149091f755d 38 MMA8451Q::MMA8451Q(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) {
emilmont 0:6149091f755d 39 // activate the peripheral
emilmont 0:6149091f755d 40 uint8_t data[2] = {REG_CTRL_REG_1, 0x01};
samux 1:d2630136d51e 41 writeRegs(data, 2);
emilmont 0:6149091f755d 42 }
emilmont 0:6149091f755d 43
mireyarod23 5:8c00640cc051 44 void MMA8451Q::setGLImit()
mireyarod23 5:8c00640cc051 45 {
mireyarod23 8:c6f68b403325 46 uint8_t data[2] = {REG_CTRL_REG_1, 0x00};
mireyarod23 5:8c00640cc051 47 writeRegs(data, 2); //put in standby
mireyarod23 5:8c00640cc051 48 data[0] = XYZ_DATA_CFG;
mireyarod23 5:8c00640cc051 49 data[1] = 0x02;
mireyarod23 5:8c00640cc051 50 writeRegs(data, 2);
mireyarod23 5:8c00640cc051 51 data[0] = REG_CTRL_REG_1;
mireyarod23 5:8c00640cc051 52 data[1] = 0x01;
mireyarod23 5:8c00640cc051 53 writeRegs(data, 2); //make active
mireyarod23 5:8c00640cc051 54 }
mireyarod23 5:8c00640cc051 55
emilmont 0:6149091f755d 56 MMA8451Q::~MMA8451Q() { }
emilmont 0:6149091f755d 57
mireyarod23 13:0155b9a9851e 58 //uint8_t MMA8451Q::getGLimit(uint8_t regData)
mireyarod23 13:0155b9a9851e 59 //{
mireyarod23 13:0155b9a9851e 60 // acc.getGLimit();
mireyarod23 13:0155b9a9851e 61 // acc.readRegs(XYZ_DATA_CFG, &regData, 1);
mireyarod23 13:0155b9a9851e 62 // sprintf(lcdData, "%d", regData);
mireyarod23 13:0155b9a9851e 63 // LCDMess(lcdData, BLINKTIME);
mireyarod23 13:0155b9a9851e 64 // acc.readRegs(REG_WHO_AM_I, &regData, 1);
mireyarod23 13:0155b9a9851e 65 // springf(lcdData, "%d", regData);
mireyarod23 13:0155b9a9851e 66 // LCDMess(lcdData, BLINKTIME);
mireyarod23 13:0155b9a9851e 67 //
mireyarod23 13:0155b9a9851e 68 // return regDate
mireyarod23 13:0155b9a9851e 69 //}
mireyarod23 12:e54564bbb2eb 70
emilmont 0:6149091f755d 71 uint8_t MMA8451Q::getWhoAmI() {
emilmont 0:6149091f755d 72 uint8_t who_am_i = 0;
samux 1:d2630136d51e 73 readRegs(REG_WHO_AM_I, &who_am_i, 1);
emilmont 0:6149091f755d 74 return who_am_i;
emilmont 0:6149091f755d 75 }
emilmont 0:6149091f755d 76
chris 3:db7126dbd63f 77 float MMA8451Q::getAccX() {
chris 3:db7126dbd63f 78 return (float(getAccAxis(REG_OUT_X_MSB))/4096.0);
emilmont 0:6149091f755d 79 }
emilmont 0:6149091f755d 80
chris 3:db7126dbd63f 81 float MMA8451Q::getAccY() {
chris 3:db7126dbd63f 82 return (float(getAccAxis(REG_OUT_Y_MSB))/4096.0);
emilmont 0:6149091f755d 83 }
emilmont 0:6149091f755d 84
chris 3:db7126dbd63f 85 float MMA8451Q::getAccZ() {
chris 3:db7126dbd63f 86 return (float(getAccAxis(REG_OUT_Z_MSB))/4096.0);
emilmont 0:6149091f755d 87 }
emilmont 0:6149091f755d 88
chris 3:db7126dbd63f 89 void MMA8451Q::getAccAllAxis(float * res) {
emilmont 0:6149091f755d 90 res[0] = getAccX();
emilmont 0:6149091f755d 91 res[1] = getAccY();
emilmont 0:6149091f755d 92 res[2] = getAccZ();
emilmont 0:6149091f755d 93 }
emilmont 0:6149091f755d 94
emilmont 0:6149091f755d 95 int16_t MMA8451Q::getAccAxis(uint8_t addr) {
emilmont 0:6149091f755d 96 int16_t acc;
emilmont 0:6149091f755d 97 uint8_t res[2];
samux 1:d2630136d51e 98 readRegs(addr, res, 2);
emilmont 0:6149091f755d 99
mireyarod23 6:167e50d4be75 100 acc = (res[0] << 6) | (res[1] >> 2);
emilmont 0:6149091f755d 101 if (acc > UINT14_MAX/2)
emilmont 0:6149091f755d 102 acc -= UINT14_MAX;
emilmont 0:6149091f755d 103
emilmont 0:6149091f755d 104 return acc;
emilmont 0:6149091f755d 105 }
emilmont 0:6149091f755d 106
samux 1:d2630136d51e 107 void MMA8451Q::readRegs(int addr, uint8_t * data, int len) {
emilmont 0:6149091f755d 108 char t[1] = {addr};
emilmont 0:6149091f755d 109 m_i2c.write(m_addr, t, 1, true);
emilmont 0:6149091f755d 110 m_i2c.read(m_addr, (char *)data, len);
emilmont 0:6149091f755d 111 }
emilmont 0:6149091f755d 112
samux 1:d2630136d51e 113 void MMA8451Q::writeRegs(uint8_t * data, int len) {
emilmont 0:6149091f755d 114 m_i2c.write(m_addr, (char *)data, len);
emilmont 0:6149091f755d 115 }