A basic library for the MMA8652 accelerometer, provides data in either floating point G's or as a signed 16 bit integer.
Dependents: microbit_mouse_BLE_daybreak_version
Fork of MMA8652 by
MMA8652.cpp@1:ff30cc4759b4, 2014-04-19 (annotated)
- Committer:
- JimCarver
- Date:
- Sat Apr 19 01:28:26 2014 +0000
- Revision:
- 1:ff30cc4759b4
- Parent:
- 0:3ae1e808e61c
Added features
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JimCarver | 1:ff30cc4759b4 | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
JimCarver | 1:ff30cc4759b4 | 2 | * |
JimCarver | 1:ff30cc4759b4 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
JimCarver | 1:ff30cc4759b4 | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
JimCarver | 1:ff30cc4759b4 | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
JimCarver | 1:ff30cc4759b4 | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
JimCarver | 1:ff30cc4759b4 | 7 | * Software is furnished to do so, subject to the following conditions: |
JimCarver | 1:ff30cc4759b4 | 8 | * |
JimCarver | 1:ff30cc4759b4 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
JimCarver | 1:ff30cc4759b4 | 10 | * substantial portions of the Software. |
JimCarver | 1:ff30cc4759b4 | 11 | * |
JimCarver | 1:ff30cc4759b4 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
JimCarver | 1:ff30cc4759b4 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
JimCarver | 1:ff30cc4759b4 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
JimCarver | 1:ff30cc4759b4 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
JimCarver | 1:ff30cc4759b4 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
JimCarver | 1:ff30cc4759b4 | 17 | */ |
JimCarver | 1:ff30cc4759b4 | 18 | |
JimCarver | 1:ff30cc4759b4 | 19 | #include "MMA8652.h" |
JimCarver | 1:ff30cc4759b4 | 20 | #define UINT14_MAX 16383 |
JimCarver | 1:ff30cc4759b4 | 21 | |
JimCarver | 0:3ae1e808e61c | 22 | MMA8652::MMA8652(PinName sda, PinName scl) : _i2c(sda, scl) { |
JimCarver | 0:3ae1e808e61c | 23 | |
JimCarver | 0:3ae1e808e61c | 24 | begin(); |
JimCarver | 0:3ae1e808e61c | 25 | } |
JimCarver | 1:ff30cc4759b4 | 26 | |
JimCarver | 1:ff30cc4759b4 | 27 | |
JimCarver | 1:ff30cc4759b4 | 28 | MMA8652::~MMA8652() { } |
JimCarver | 1:ff30cc4759b4 | 29 | |
JimCarver | 0:3ae1e808e61c | 30 | |
JimCarver | 0:3ae1e808e61c | 31 | void MMA8652::RegRead( char reg, char * d, int len) |
JimCarver | 0:3ae1e808e61c | 32 | { |
JimCarver | 0:3ae1e808e61c | 33 | char cmd[1]; |
JimCarver | 0:3ae1e808e61c | 34 | cmd[0] = reg; |
JimCarver | 0:3ae1e808e61c | 35 | char i2c_addr = MMA8652_SLAVE_ADDR; |
JimCarver | 1:ff30cc4759b4 | 36 | _i2c.write( i2c_addr, cmd, 1, true); |
JimCarver | 0:3ae1e808e61c | 37 | _i2c.read ( i2c_addr, d, len); |
JimCarver | 0:3ae1e808e61c | 38 | } |
JimCarver | 0:3ae1e808e61c | 39 | |
JimCarver | 0:3ae1e808e61c | 40 | void MMA8652::begin(void) |
JimCarver | 0:3ae1e808e61c | 41 | { |
JimCarver | 0:3ae1e808e61c | 42 | char data[2]; |
JimCarver | 0:3ae1e808e61c | 43 | // write 0000 0000 = 0x00 to accelerometer control register 1 to place MMA8652 into |
JimCarver | 0:3ae1e808e61c | 44 | // standby |
JimCarver | 0:3ae1e808e61c | 45 | // [7-1] = 0000 000 |
JimCarver | 0:3ae1e808e61c | 46 | // [0]: active=0 |
JimCarver | 0:3ae1e808e61c | 47 | data[0] = MMA8652_CTRL_REG1; |
JimCarver | 0:3ae1e808e61c | 48 | data[1] = 0x00; |
JimCarver | 0:3ae1e808e61c | 49 | _i2c.write( MMA8652_SLAVE_ADDR, data, 2); |
JimCarver | 0:3ae1e808e61c | 50 | |
JimCarver | 0:3ae1e808e61c | 51 | // write 0000 0001= 0x01 to XYZ_DATA_CFG register |
JimCarver | 0:3ae1e808e61c | 52 | // [7]: reserved |
JimCarver | 0:3ae1e808e61c | 53 | // [6]: reserved |
JimCarver | 0:3ae1e808e61c | 54 | // [5]: reserved |
JimCarver | 0:3ae1e808e61c | 55 | // [4]: hpf_out=0 |
JimCarver | 0:3ae1e808e61c | 56 | // [3]: reserved |
JimCarver | 0:3ae1e808e61c | 57 | // [2]: reserved |
JimCarver | 0:3ae1e808e61c | 58 | // [1-0]: fs=00 for accelerometer range of +/-2g range with 0.244mg/LSB |
JimCarver | 0:3ae1e808e61c | 59 | data[0] = MMA8652_XYZ_DATA_CFG; |
JimCarver | 0:3ae1e808e61c | 60 | data[1] = 0x00; |
JimCarver | 0:3ae1e808e61c | 61 | _i2c.write( MMA8652_SLAVE_ADDR, data, 2); |
JimCarver | 0:3ae1e808e61c | 62 | |
JimCarver | 0:3ae1e808e61c | 63 | // write 0000 1101 = 0x0D to accelerometer control register 1 |
JimCarver | 0:3ae1e808e61c | 64 | // [7-6]: aslp_rate=00 |
JimCarver | 0:3ae1e808e61c | 65 | // [5-3]: dr=100 for 50Hz data rate |
JimCarver | 0:3ae1e808e61c | 66 | // [2]: 0 |
JimCarver | 0:3ae1e808e61c | 67 | // [1]: 0 |
JimCarver | 0:3ae1e808e61c | 68 | // [0]: active=1 to take the part out of standby and enable sampling |
JimCarver | 0:3ae1e808e61c | 69 | data[0] = MMA8652_CTRL_REG1; |
JimCarver | 0:3ae1e808e61c | 70 | data[1] = 0x21; |
JimCarver | 0:3ae1e808e61c | 71 | _i2c.write( MMA8652_SLAVE_ADDR, data, 2); |
JimCarver | 0:3ae1e808e61c | 72 | } |
JimCarver | 0:3ae1e808e61c | 73 | |
JimCarver | 1:ff30cc4759b4 | 74 | char MMA8652::getWhoAmI(void) |
JimCarver | 1:ff30cc4759b4 | 75 | { |
JimCarver | 1:ff30cc4759b4 | 76 | static char d; |
JimCarver | 1:ff30cc4759b4 | 77 | RegRead( /*MMA8652_WHOAMI*/ 0x0d, &d, 1); |
JimCarver | 1:ff30cc4759b4 | 78 | return(d); |
JimCarver | 1:ff30cc4759b4 | 79 | } |
JimCarver | 1:ff30cc4759b4 | 80 | |
JimCarver | 0:3ae1e808e61c | 81 | void MMA8652::ReadXYZ(float * a) |
JimCarver | 0:3ae1e808e61c | 82 | { |
JimCarver | 0:3ae1e808e61c | 83 | char d[7]; |
JimCarver | 0:3ae1e808e61c | 84 | int16_t t[6]; |
JimCarver | 0:3ae1e808e61c | 85 | |
JimCarver | 0:3ae1e808e61c | 86 | RegRead( MMA8652_STATUS, d, 7); |
JimCarver | 0:3ae1e808e61c | 87 | t[0] = ((d[1] * 256) + ((unsigned short) d[2])); |
JimCarver | 0:3ae1e808e61c | 88 | t[1] = ((d[3] * 256) + ((unsigned short) d[4])); |
JimCarver | 0:3ae1e808e61c | 89 | t[2] = ((d[5] * 256) + ((unsigned short) d[6])); |
JimCarver | 0:3ae1e808e61c | 90 | |
JimCarver | 0:3ae1e808e61c | 91 | a[0] = (float) t[0] / 16384.0; |
JimCarver | 0:3ae1e808e61c | 92 | a[1] = (float) t[1] / 16384.0; |
JimCarver | 0:3ae1e808e61c | 93 | a[2] = (float) t[2] / 16384.0; |
JimCarver | 0:3ae1e808e61c | 94 | |
JimCarver | 1:ff30cc4759b4 | 95 | } |
JimCarver | 1:ff30cc4759b4 | 96 | |
JimCarver | 1:ff30cc4759b4 | 97 | |
JimCarver | 1:ff30cc4759b4 | 98 | void MMA8652::ReadXYZraw(int16_t * d) |
JimCarver | 1:ff30cc4759b4 | 99 | { |
JimCarver | 1:ff30cc4759b4 | 100 | char res[6]; |
JimCarver | 1:ff30cc4759b4 | 101 | int16_t acc; |
JimCarver | 1:ff30cc4759b4 | 102 | RegRead( MMA8652_OUT_X_MSB, res, 6); |
JimCarver | 1:ff30cc4759b4 | 103 | |
JimCarver | 1:ff30cc4759b4 | 104 | acc = (res[0] << 6) | (res[1] >> 2); |
JimCarver | 1:ff30cc4759b4 | 105 | if (acc > UINT14_MAX/2) |
JimCarver | 1:ff30cc4759b4 | 106 | acc -= UINT14_MAX; |
JimCarver | 1:ff30cc4759b4 | 107 | d[0] = acc; |
JimCarver | 1:ff30cc4759b4 | 108 | acc = (res[2] << 6) | (res[3] >> 2); |
JimCarver | 1:ff30cc4759b4 | 109 | if (acc > UINT14_MAX/2) |
JimCarver | 1:ff30cc4759b4 | 110 | acc -= UINT14_MAX; |
JimCarver | 1:ff30cc4759b4 | 111 | d[1] = acc; |
JimCarver | 1:ff30cc4759b4 | 112 | acc = (res[4] << 6) | (res[5] >> 2); |
JimCarver | 1:ff30cc4759b4 | 113 | if (acc > UINT14_MAX/2) |
JimCarver | 1:ff30cc4759b4 | 114 | acc -= UINT14_MAX; |
JimCarver | 1:ff30cc4759b4 | 115 | d[2] = acc; |
JimCarver | 1:ff30cc4759b4 | 116 | } |
JimCarver | 1:ff30cc4759b4 | 117 | |
JimCarver | 1:ff30cc4759b4 | 118 |