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 Jim Carver

Committer:
JimCarver
Date:
Mon Apr 07 00:59:06 2014 +0000
Revision:
0:3ae1e808e61c
Child:
1:ff30cc4759b4
Rev 0.1 Simple operation of just X, Y, Z values in floating point G's

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JimCarver 0:3ae1e808e61c 1 #include "MMA8652.h"
JimCarver 0:3ae1e808e61c 2
JimCarver 0:3ae1e808e61c 3 MMA8652::MMA8652(PinName sda, PinName scl) : _i2c(sda, scl) {
JimCarver 0:3ae1e808e61c 4
JimCarver 0:3ae1e808e61c 5 begin();
JimCarver 0:3ae1e808e61c 6 }
JimCarver 0:3ae1e808e61c 7
JimCarver 0:3ae1e808e61c 8 void MMA8652::RegRead( char reg, char * d, int len)
JimCarver 0:3ae1e808e61c 9 {
JimCarver 0:3ae1e808e61c 10 char cmd[1];
JimCarver 0:3ae1e808e61c 11 cmd[0] = reg;
JimCarver 0:3ae1e808e61c 12 char i2c_addr = MMA8652_SLAVE_ADDR;
JimCarver 0:3ae1e808e61c 13 _i2c.write( i2c_addr, cmd, 1);
JimCarver 0:3ae1e808e61c 14 _i2c.read ( i2c_addr, d, len);
JimCarver 0:3ae1e808e61c 15 }
JimCarver 0:3ae1e808e61c 16
JimCarver 0:3ae1e808e61c 17 void MMA8652::begin(void)
JimCarver 0:3ae1e808e61c 18 {
JimCarver 0:3ae1e808e61c 19 char data[2];
JimCarver 0:3ae1e808e61c 20 // write 0000 0000 = 0x00 to accelerometer control register 1 to place MMA8652 into
JimCarver 0:3ae1e808e61c 21 // standby
JimCarver 0:3ae1e808e61c 22 // [7-1] = 0000 000
JimCarver 0:3ae1e808e61c 23 // [0]: active=0
JimCarver 0:3ae1e808e61c 24 data[0] = MMA8652_CTRL_REG1;
JimCarver 0:3ae1e808e61c 25 data[1] = 0x00;
JimCarver 0:3ae1e808e61c 26 _i2c.write( MMA8652_SLAVE_ADDR, data, 2);
JimCarver 0:3ae1e808e61c 27
JimCarver 0:3ae1e808e61c 28 // write 0000 0001= 0x01 to XYZ_DATA_CFG register
JimCarver 0:3ae1e808e61c 29 // [7]: reserved
JimCarver 0:3ae1e808e61c 30 // [6]: reserved
JimCarver 0:3ae1e808e61c 31 // [5]: reserved
JimCarver 0:3ae1e808e61c 32 // [4]: hpf_out=0
JimCarver 0:3ae1e808e61c 33 // [3]: reserved
JimCarver 0:3ae1e808e61c 34 // [2]: reserved
JimCarver 0:3ae1e808e61c 35 // [1-0]: fs=00 for accelerometer range of +/-2g range with 0.244mg/LSB
JimCarver 0:3ae1e808e61c 36 data[0] = MMA8652_XYZ_DATA_CFG;
JimCarver 0:3ae1e808e61c 37 data[1] = 0x00;
JimCarver 0:3ae1e808e61c 38 _i2c.write( MMA8652_SLAVE_ADDR, data, 2);
JimCarver 0:3ae1e808e61c 39
JimCarver 0:3ae1e808e61c 40 // write 0000 1101 = 0x0D to accelerometer control register 1
JimCarver 0:3ae1e808e61c 41 // [7-6]: aslp_rate=00
JimCarver 0:3ae1e808e61c 42 // [5-3]: dr=100 for 50Hz data rate
JimCarver 0:3ae1e808e61c 43 // [2]: 0
JimCarver 0:3ae1e808e61c 44 // [1]: 0
JimCarver 0:3ae1e808e61c 45 // [0]: active=1 to take the part out of standby and enable sampling
JimCarver 0:3ae1e808e61c 46 data[0] = MMA8652_CTRL_REG1;
JimCarver 0:3ae1e808e61c 47 data[1] = 0x21;
JimCarver 0:3ae1e808e61c 48 _i2c.write( MMA8652_SLAVE_ADDR, data, 2);
JimCarver 0:3ae1e808e61c 49 }
JimCarver 0:3ae1e808e61c 50
JimCarver 0:3ae1e808e61c 51 void MMA8652::ReadXYZ(float * a)
JimCarver 0:3ae1e808e61c 52 {
JimCarver 0:3ae1e808e61c 53 char d[7];
JimCarver 0:3ae1e808e61c 54 int16_t t[6];
JimCarver 0:3ae1e808e61c 55
JimCarver 0:3ae1e808e61c 56 RegRead( MMA8652_STATUS, d, 7);
JimCarver 0:3ae1e808e61c 57 t[0] = ((d[1] * 256) + ((unsigned short) d[2]));
JimCarver 0:3ae1e808e61c 58 t[1] = ((d[3] * 256) + ((unsigned short) d[4]));
JimCarver 0:3ae1e808e61c 59 t[2] = ((d[5] * 256) + ((unsigned short) d[6]));
JimCarver 0:3ae1e808e61c 60
JimCarver 0:3ae1e808e61c 61 a[0] = (float) t[0] / 16384.0;
JimCarver 0:3ae1e808e61c 62 a[1] = (float) t[1] / 16384.0;
JimCarver 0:3ae1e808e61c 63 a[2] = (float) t[2] / 16384.0;
JimCarver 0:3ae1e808e61c 64
JimCarver 0:3ae1e808e61c 65 }