KLUJA-SSD447-Hw8.1
Fork of MMA8451Q8 by
MMA8451Q8.cpp@8:be66db51aa76, 2017-03-08 (annotated)
- Committer:
- kennylujan42
- Date:
- Wed Mar 08 17:46:27 2017 +0000
- Revision:
- 8:be66db51aa76
- Parent:
- 7:7812354ef684
KLUJ-SSD447-HW8.1
Who changed what in which revision?
User | Revision | Line number | New 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 | |
kennylujan42 | 8:be66db51aa76 | 36 | //#define GSCALING 1024.0 |
kennylujan42 | 8:be66db51aa76 | 37 | |
kennylujan42 | 8:be66db51aa76 | 38 | #define NUM_DATA 2 |
kennylujan42 | 8:be66db51aa76 | 39 | #define GSCALING 1024.0 |
kennylujan42 | 8:be66db51aa76 | 40 | #define ADDRESS_INDEX 0 |
kennylujan42 | 8:be66db51aa76 | 41 | #define DATA_INDEX 1 |
kennylujan42 | 8:be66db51aa76 | 42 | |
kennylujan42 | 8:be66db51aa76 | 43 | float gScaling[3] = {4095.0,2048.0,1024.0};/////////////// |
scohennm | 5:be042e8c1756 | 44 | |
emilmont | 0:6149091f755d | 45 | MMA8451Q::MMA8451Q(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) { |
emilmont | 0:6149091f755d | 46 | // activate the peripheral |
emilmont | 0:6149091f755d | 47 | uint8_t data[2] = {REG_CTRL_REG_1, 0x01}; |
samux | 1:d2630136d51e | 48 | writeRegs(data, 2); |
emilmont | 0:6149091f755d | 49 | } |
emilmont | 0:6149091f755d | 50 | |
emilmont | 0:6149091f755d | 51 | MMA8451Q::~MMA8451Q() { } |
emilmont | 0:6149091f755d | 52 | |
kennylujan42 | 8:be66db51aa76 | 53 | void MMA8451Q::setStandbyMode(){ |
kennylujan42 | 8:be66db51aa76 | 54 | #define ACTIVEMASK 0x01 |
kennylujan42 | 8:be66db51aa76 | 55 | uint8_t registerData[1]; |
kennylujan42 | 8:be66db51aa76 | 56 | uint8_t data[NUM_DATA] = {REG_CTRL_REG_1, 0x00}; |
kennylujan42 | 8:be66db51aa76 | 57 | |
kennylujan42 | 8:be66db51aa76 | 58 | readRegs(REG_CTRL_REG_1, registerData, 1); |
kennylujan42 | 8:be66db51aa76 | 59 | data[1] = registerData[0] & ~ACTIVEMASK; |
kennylujan42 | 8:be66db51aa76 | 60 | writeRegs(data, NUM_DATA); |
kennylujan42 | 8:be66db51aa76 | 61 | } |
kennylujan42 | 8:be66db51aa76 | 62 | void MMA8451Q::setActiveMode(){ |
kennylujan42 | 8:be66db51aa76 | 63 | #define ACTIVEMASK 0x01 |
kennylujan42 | 8:be66db51aa76 | 64 | uint8_t registerData[1]; |
kennylujan42 | 8:be66db51aa76 | 65 | uint8_t data[NUM_DATA] = {REG_CTRL_REG_1, 0x00}; |
kennylujan42 | 8:be66db51aa76 | 66 | |
kennylujan42 | 8:be66db51aa76 | 67 | readRegs(REG_CTRL_REG_1, registerData, 1); |
kennylujan42 | 8:be66db51aa76 | 68 | data[1] = registerData[0] | ACTIVEMASK; |
kennylujan42 | 8:be66db51aa76 | 69 | writeRegs(data, NUM_DATA); |
kennylujan42 | 8:be66db51aa76 | 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 | } |
kennylujan42 | 8:be66db51aa76 | 76 | /* |
scohennm | 5:be042e8c1756 | 77 | void MMA8451Q::setGLimit() { |
scohennm | 5:be042e8c1756 | 78 | uint8_t data[2] = {REG_CTRL_REG_1, 0x00}; |
scohennm | 5:be042e8c1756 | 79 | writeRegs(data, 2); // put in standby |
scohennm | 5:be042e8c1756 | 80 | data[0] = XYZ_DATA_CFG; |
scohennm | 7:7812354ef684 | 81 | data[1] = MAX_8G; |
scohennm | 5:be042e8c1756 | 82 | writeRegs(data, 2);// change g limit |
scohennm | 5:be042e8c1756 | 83 | data[0] = REG_CTRL_REG_1; |
scohennm | 5:be042e8c1756 | 84 | data[1] = 0x01; |
scohennm | 5:be042e8c1756 | 85 | writeRegs(data, 2); // make active |
kennylujan42 | 8:be66db51aa76 | 86 | }*/ |
kennylujan42 | 8:be66db51aa76 | 87 | void MMA8451Q::setGLimit(int gSelect){ |
kennylujan42 | 8:be66db51aa76 | 88 | |
kennylujan42 | 8:be66db51aa76 | 89 | uint8_t data[NUM_DATA] = {REG_CTRL_REG_1, 0x00}; |
kennylujan42 | 8:be66db51aa76 | 90 | gChosen = gSelect; |
kennylujan42 | 8:be66db51aa76 | 91 | setStandbyMode(); |
kennylujan42 | 8:be66db51aa76 | 92 | data[ADDRESS_INDEX] = XYZ_DATA_CFG; |
kennylujan42 | 8:be66db51aa76 | 93 | data[DATA_INDEX] = gChosen; |
kennylujan42 | 8:be66db51aa76 | 94 | writeRegs(data, 2); |
kennylujan42 | 8:be66db51aa76 | 95 | setActiveMode(); |
kennylujan42 | 8:be66db51aa76 | 96 | |
scohennm | 5:be042e8c1756 | 97 | } |
emilmont | 0:6149091f755d | 98 | |
kennylujan42 | 8:be66db51aa76 | 99 | int16_t MMA8451Q::getAccAxis(uint8_t addr){ |
kennylujan42 | 8:be66db51aa76 | 100 | int16_t acc; |
kennylujan42 | 8:be66db51aa76 | 101 | uint8_t res[2]; |
kennylujan42 | 8:be66db51aa76 | 102 | readRegs(addr, res, 2); |
kennylujan42 | 8:be66db51aa76 | 103 | |
kennylujan42 | 8:be66db51aa76 | 104 | acc = (res[0] << 6) | (res[1] >> 2); |
kennylujan42 | 8:be66db51aa76 | 105 | if (acc > UINT14_MAX/2) |
kennylujan42 | 8:be66db51aa76 | 106 | acc -= UINT14_MAX; |
kennylujan42 | 8:be66db51aa76 | 107 | |
kennylujan42 | 8:be66db51aa76 | 108 | return acc; |
kennylujan42 | 8:be66db51aa76 | 109 | } |
chris | 3:db7126dbd63f | 110 | float MMA8451Q::getAccX() { |
scohennm | 5:be042e8c1756 | 111 | return (float(getAccAxis(REG_OUT_X_MSB))/GSCALING); |
emilmont | 0:6149091f755d | 112 | } |
emilmont | 0:6149091f755d | 113 | |
chris | 3:db7126dbd63f | 114 | float MMA8451Q::getAccY() { |
scohennm | 5:be042e8c1756 | 115 | return (float(getAccAxis(REG_OUT_Y_MSB))/GSCALING); |
emilmont | 0:6149091f755d | 116 | } |
emilmont | 0:6149091f755d | 117 | |
chris | 3:db7126dbd63f | 118 | float MMA8451Q::getAccZ() { |
scohennm | 5:be042e8c1756 | 119 | return (float(getAccAxis(REG_OUT_Z_MSB))/GSCALING); |
emilmont | 0:6149091f755d | 120 | } |
emilmont | 0:6149091f755d | 121 | |
chris | 3:db7126dbd63f | 122 | void MMA8451Q::getAccAllAxis(float * res) { |
emilmont | 0:6149091f755d | 123 | res[0] = getAccX(); |
emilmont | 0:6149091f755d | 124 | res[1] = getAccY(); |
emilmont | 0:6149091f755d | 125 | res[2] = getAccZ(); |
emilmont | 0:6149091f755d | 126 | } |
kennylujan42 | 8:be66db51aa76 | 127 | /* |
emilmont | 0:6149091f755d | 128 | int16_t MMA8451Q::getAccAxis(uint8_t addr) { |
emilmont | 0:6149091f755d | 129 | int16_t acc; |
emilmont | 0:6149091f755d | 130 | uint8_t res[2]; |
samux | 1:d2630136d51e | 131 | readRegs(addr, res, 2); |
emilmont | 0:6149091f755d | 132 | |
emilmont | 0:6149091f755d | 133 | acc = (res[0] << 6) | (res[1] >> 2); |
emilmont | 0:6149091f755d | 134 | if (acc > UINT14_MAX/2) |
emilmont | 0:6149091f755d | 135 | acc -= UINT14_MAX; |
emilmont | 0:6149091f755d | 136 | |
emilmont | 0:6149091f755d | 137 | return acc; |
emilmont | 0:6149091f755d | 138 | } |
kennylujan42 | 8:be66db51aa76 | 139 | */ |
samux | 1:d2630136d51e | 140 | void MMA8451Q::readRegs(int addr, uint8_t * data, int len) { |
emilmont | 0:6149091f755d | 141 | char t[1] = {addr}; |
emilmont | 0:6149091f755d | 142 | m_i2c.write(m_addr, t, 1, true); |
emilmont | 0:6149091f755d | 143 | m_i2c.read(m_addr, (char *)data, len); |
emilmont | 0:6149091f755d | 144 | } |
emilmont | 0:6149091f755d | 145 | |
samux | 1:d2630136d51e | 146 | void MMA8451Q::writeRegs(uint8_t * data, int len) { |
emilmont | 0:6149091f755d | 147 | m_i2c.write(m_addr, (char *)data, len); |
emilmont | 0:6149091f755d | 148 | } |