I got the structure of the code but did not get every details right

Committer:
hzsun
Date:
Fri Mar 06 02:13:24 2020 +0000
Revision:
7:884fb008cee8
Parent:
5:392ac6e02d80
zxzx

Who changed what in which revision?

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