hattori&ide

Dependencies:   mbed

Committer:
hattori_atsushi
Date:
Sun Dec 18 08:16:01 2022 +0000
Revision:
0:f77369cabd75
hattori

Who changed what in which revision?

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