KBrat-SSD645-HW-8_1_MMA8451Q8

Fork of MMA8451Q8 by Keisha Brathwaite

Committer:
tisbrat
Date:
Wed Mar 08 03:59:56 2017 +0000
Revision:
9:1e7f9d65ad29
Parent:
8:65da360cf088
KBrat-SSD645-HW-8_1_lib

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 */
scohennm 5:be042e8c1756 18 // Modify to change full scale gravity range 141207 sc
samux 1:d2630136d51e 19
scohennm 5:be042e8c1756 20
scohennm 5:be042e8c1756 21 #include "MMA8451Q8.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
scohennm 5:be042e8c1756 28 #define XYZ_DATA_CFG 0x0E
emilmont 0:6149091f755d 29
samux 1:d2630136d51e 30 #define UINT14_MAX 16383
emilmont 0:6149091f755d 31
scohennm 5:be042e8c1756 32 #define MAX_2G 0x00
scohennm 5:be042e8c1756 33 #define MAX_4G 0x01
scohennm 6:c29386367dcf 34 #define MAX_8G 0x02
scohennm 5:be042e8c1756 35
scohennm 5:be042e8c1756 36 #define GSCALING 1024.0
tisbrat 9:1e7f9d65ad29 37 //#define GSCALING 4096.0
tisbrat 9:1e7f9d65ad29 38 #define NUM_DATA 2
tisbrat 9:1e7f9d65ad29 39 #define ADDRESS_INDEX 0
tisbrat 9:1e7f9d65ad29 40 #define DATA_INDEX 1
tisbrat 9:1e7f9d65ad29 41
tisbrat 9:1e7f9d65ad29 42
tisbrat 9:1e7f9d65ad29 43
tisbrat 9:1e7f9d65ad29 44 float gScaling[3] = {4095.0, 2048.0,1024.0}; //scaling for acceleration of gravity values
scohennm 5:be042e8c1756 45
emilmont 0:6149091f755d 46 MMA8451Q::MMA8451Q(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) {
emilmont 0:6149091f755d 47 // activate the peripheral
emilmont 0:6149091f755d 48 uint8_t data[2] = {REG_CTRL_REG_1, 0x01};
samux 1:d2630136d51e 49 writeRegs(data, 2);
emilmont 0:6149091f755d 50 }
emilmont 0:6149091f755d 51
emilmont 0:6149091f755d 52 MMA8451Q::~MMA8451Q() { }
emilmont 0:6149091f755d 53
emilmont 0:6149091f755d 54 uint8_t MMA8451Q::getWhoAmI() {
emilmont 0:6149091f755d 55 uint8_t who_am_i = 0;
samux 1:d2630136d51e 56 readRegs(REG_WHO_AM_I, &who_am_i, 1);
emilmont 0:6149091f755d 57 return who_am_i;
emilmont 0:6149091f755d 58 }
tisbrat 8:65da360cf088 59
tisbrat 9:1e7f9d65ad29 60 void MMA8451Q::standbyMode(){
tisbrat 9:1e7f9d65ad29 61 #define ACTIVEMASK 0x01
tisbrat 9:1e7f9d65ad29 62 uint8_t registerData[1];
tisbrat 9:1e7f9d65ad29 63 uint8_t data[NUM_DATA] = {REG_CTRL_REG_1, 0x00};
tisbrat 9:1e7f9d65ad29 64
tisbrat 9:1e7f9d65ad29 65 readRegs(REG_CTRL_REG_1, registerData, 1);
tisbrat 9:1e7f9d65ad29 66 data[1] = registerData[0] & ~ACTIVEMASK;
tisbrat 9:1e7f9d65ad29 67 writeRegs(data, NUM_DATA); //Standby Mode
tisbrat 8:65da360cf088 68
tisbrat 8:65da360cf088 69 }
tisbrat 8:65da360cf088 70
tisbrat 8:65da360cf088 71 void MMA8451Q::activeMode(){
tisbrat 9:1e7f9d65ad29 72 #define ACTIVEMASK 0x01
tisbrat 9:1e7f9d65ad29 73 uint8_t registerData[1];
tisbrat 9:1e7f9d65ad29 74 uint8_t data[NUM_DATA] = {REG_CTRL_REG_1, 0x00};
tisbrat 9:1e7f9d65ad29 75
tisbrat 9:1e7f9d65ad29 76 readRegs(REG_CTRL_REG_1, registerData, 1);
tisbrat 8:65da360cf088 77 data[0] = REG_CTRL_REG_1;
tisbrat 9:1e7f9d65ad29 78 data[1] = registerData[0] | ACTIVEMASK;
tisbrat 9:1e7f9d65ad29 79 writeRegs(data, NUM_DATA); //Active Mode
tisbrat 8:65da360cf088 80 }
tisbrat 8:65da360cf088 81
tisbrat 9:1e7f9d65ad29 82 void MMA8451Q::setGLimit(int gSelect) {
tisbrat 9:1e7f9d65ad29 83 uint8_t data[NUM_DATA] = {REG_CTRL_REG_1, 0x00};
tisbrat 9:1e7f9d65ad29 84 gChosen = gSelect;
tisbrat 8:65da360cf088 85 standbyMode();
tisbrat 9:1e7f9d65ad29 86 data[ADDRESS_INDEX] = XYZ_DATA_CFG;
tisbrat 9:1e7f9d65ad29 87 data[DATA_INDEX] = gChosen;
tisbrat 9:1e7f9d65ad29 88 writeRegs(data, 2);//change GLimit
tisbrat 8:65da360cf088 89 activeMode();
tisbrat 8:65da360cf088 90 }
tisbrat 8:65da360cf088 91
tisbrat 8:65da360cf088 92 /*void MMA8451Q::setGLimit() {
scohennm 5:be042e8c1756 93 uint8_t data[2] = {REG_CTRL_REG_1, 0x00};
scohennm 5:be042e8c1756 94 writeRegs(data, 2); // put in standby
scohennm 5:be042e8c1756 95 data[0] = XYZ_DATA_CFG;
scohennm 7:7812354ef684 96 data[1] = MAX_8G;
scohennm 5:be042e8c1756 97 writeRegs(data, 2);// change g limit
scohennm 5:be042e8c1756 98 data[0] = REG_CTRL_REG_1;
scohennm 5:be042e8c1756 99 data[1] = 0x01;
scohennm 5:be042e8c1756 100 writeRegs(data, 2); // make active
tisbrat 8:65da360cf088 101 }*/
emilmont 0:6149091f755d 102
chris 3:db7126dbd63f 103 float MMA8451Q::getAccX() {
scohennm 5:be042e8c1756 104 return (float(getAccAxis(REG_OUT_X_MSB))/GSCALING);
emilmont 0:6149091f755d 105 }
emilmont 0:6149091f755d 106
chris 3:db7126dbd63f 107 float MMA8451Q::getAccY() {
scohennm 5:be042e8c1756 108 return (float(getAccAxis(REG_OUT_Y_MSB))/GSCALING);
emilmont 0:6149091f755d 109 }
emilmont 0:6149091f755d 110
chris 3:db7126dbd63f 111 float MMA8451Q::getAccZ() {
scohennm 5:be042e8c1756 112 return (float(getAccAxis(REG_OUT_Z_MSB))/GSCALING);
emilmont 0:6149091f755d 113 }
emilmont 0:6149091f755d 114
tisbrat 9:1e7f9d65ad29 115
chris 3:db7126dbd63f 116 void MMA8451Q::getAccAllAxis(float * res) {
emilmont 0:6149091f755d 117 res[0] = getAccX();
emilmont 0:6149091f755d 118 res[1] = getAccY();
emilmont 0:6149091f755d 119 res[2] = getAccZ();
emilmont 0:6149091f755d 120 }
emilmont 0:6149091f755d 121
emilmont 0:6149091f755d 122 int16_t MMA8451Q::getAccAxis(uint8_t addr) {
emilmont 0:6149091f755d 123 int16_t acc;
emilmont 0:6149091f755d 124 uint8_t res[2];
samux 1:d2630136d51e 125 readRegs(addr, res, 2);
emilmont 0:6149091f755d 126
emilmont 0:6149091f755d 127 acc = (res[0] << 6) | (res[1] >> 2);
emilmont 0:6149091f755d 128 if (acc > UINT14_MAX/2)
emilmont 0:6149091f755d 129 acc -= UINT14_MAX;
emilmont 0:6149091f755d 130
emilmont 0:6149091f755d 131 return acc;
emilmont 0:6149091f755d 132 }
emilmont 0:6149091f755d 133
samux 1:d2630136d51e 134 void MMA8451Q::readRegs(int addr, uint8_t * data, int len) {
emilmont 0:6149091f755d 135 char t[1] = {addr};
emilmont 0:6149091f755d 136 m_i2c.write(m_addr, t, 1, true);
emilmont 0:6149091f755d 137 m_i2c.read(m_addr, (char *)data, len);
emilmont 0:6149091f755d 138 }
emilmont 0:6149091f755d 139
samux 1:d2630136d51e 140 void MMA8451Q::writeRegs(uint8_t * data, int len) {
emilmont 0:6149091f755d 141 m_i2c.write(m_addr, (char *)data, len);
emilmont 0:6149091f755d 142 }