test

Fork of MMA8652 by Jim Carver

Files at this revision

API Documentation at this revision

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

Changed in this revision

MMA8652.cpp Show annotated file Show diff for this revision Revisions of this file
MMA8652.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MMA8652.cpp	Mon Apr 07 00:59:06 2014 +0000
@@ -0,0 +1,65 @@
+ #include "MMA8652.h"
+ 
+MMA8652::MMA8652(PinName sda, PinName scl) : _i2c(sda, scl) {
+ 
+    begin();
+}
+              
+void MMA8652::RegRead( char reg, char * d, int len)
+{
+    char cmd[1];
+    cmd[0] = reg;
+char i2c_addr = MMA8652_SLAVE_ADDR;
+_i2c.write( i2c_addr, cmd, 1);
+_i2c.read ( i2c_addr, d, len);
+}
+
+void MMA8652::begin(void)
+{
+    char data[2];
+    // write 0000 0000 = 0x00 to accelerometer control register 1 to place MMA8652 into
+    // standby
+    // [7-1] = 0000 000
+    // [0]: active=0
+    data[0] = MMA8652_CTRL_REG1;
+    data[1] = 0x00;
+    _i2c.write( MMA8652_SLAVE_ADDR, data, 2);
+    
+    // write 0000 0001= 0x01 to XYZ_DATA_CFG register
+    // [7]: reserved
+    // [6]: reserved
+    // [5]: reserved
+    // [4]: hpf_out=0
+    // [3]: reserved
+    // [2]: reserved
+    // [1-0]: fs=00 for accelerometer range of +/-2g range with 0.244mg/LSB
+    data[0] = MMA8652_XYZ_DATA_CFG;
+    data[1] = 0x00;
+    _i2c.write( MMA8652_SLAVE_ADDR, data, 2);
+
+    // write 0000 1101 = 0x0D to accelerometer control register 1
+    // [7-6]: aslp_rate=00
+    // [5-3]: dr=100 for 50Hz data rate
+    // [2]: 0
+    // [1]: 0
+    // [0]: active=1 to take the part out of standby and enable sampling
+    data[0] = MMA8652_CTRL_REG1;
+    data[1] = 0x21;
+    _i2c.write( MMA8652_SLAVE_ADDR, data, 2);
+}
+
+void MMA8652::ReadXYZ(float * a)
+{
+    char d[7];
+    int16_t t[6];
+
+    RegRead( MMA8652_STATUS, d, 7);
+    t[0] = ((d[1] * 256) + ((unsigned short) d[2]));
+    t[1] = ((d[3] * 256) + ((unsigned short) d[4]));
+    t[2] = ((d[5] * 256) + ((unsigned short) d[6]));
+
+    a[0] = (float) t[0] / 16384.0;
+    a[1] = (float) t[1] / 16384.0;
+    a[2] = (float) t[2] / 16384.0;
+  
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MMA8652.h	Mon Apr 07 00:59:06 2014 +0000
@@ -0,0 +1,65 @@
+#ifndef MMA8652_H
+#define MMA8652_H
+
+#include "mbed.h"
+
+
+// MMA8652 Slave Address
+#define MMA8652_SLAVE_ADDR 0x3A
+
+// MMA8652 internal register addresses
+#define MMA8652_STATUS 0x00
+#define MMA8652_WHOAMI 0x0D
+#define MMA8652_XYZ_DATA_CFG 0x0E
+#define MMA8652_CTRL_REG1 0x2A
+#define MMA8652_WHOAMI_VAL 0x4A
+
+class MMA8652
+{
+public:
+    /**
+    * MPL3115A2 constructor
+    *
+    * @param sda SDA pin
+    * @param sdl SCL pin
+    * @param addr addr of the I2C peripheral
+    */
+    MMA8652(PinName sda, PinName scl);
+    
+    /**
+    * Get the value of the WHO_AM_I register
+    *
+    * @returns DEVICE_ID value == ??
+    */
+    //uint8_t getDeviceID();
+    
+    /**
+    * Return the STATUS register value
+    *
+    * @returns STATUS register value
+    */
+    //unsigned char getStatus( void);
+    
+    /**
+    * Get the Accelerometer & Magnetometer values
+    * Accelerometer data is in G's
+    * MAgnetometer Data is in microteslas
+    *
+    * @returns altimeter value as float
+    */
+    void ReadXYZ(float * a);
+      
+
+private:
+
+    I2C _i2c;
+    /** Set the device in active mode
+    */
+    void begin( void);
+    
+    void RegRead( char reg, char * d, int len);
+
+
+};
+
+#endif