A basic library for the MMA8652 accelerometer, provides data in either floating point G's or as a signed 16 bit integer.

Dependents:   Hello_MMA8652 Multi-Sensor Buddi_Blueband Freescale_Multi-Sensor_Shield ... more

Files at this revision

API Documentation at this revision

Comitter:
JimCarver
Date:
Sat Apr 19 01:28:26 2014 +0000
Parent:
0:3ae1e808e61c
Child:
2:29c2dd97ca95
Commit message:
Added features

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
--- a/MMA8652.cpp	Mon Apr 07 00:59:06 2014 +0000
+++ b/MMA8652.cpp	Sat Apr 19 01:28:26 2014 +0000
@@ -1,16 +1,39 @@
- #include "MMA8652.h"
- 
+/* Copyright (c) 2010-2011 mbed.org, MIT License
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+* and associated documentation files (the "Software"), to deal in the Software without
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in all copies or
+* substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include "MMA8652.h"
+#define UINT14_MAX        16383
+  
 MMA8652::MMA8652(PinName sda, PinName scl) : _i2c(sda, scl) {
  
     begin();
 }
+
+
+MMA8652::~MMA8652() { }
+
               
 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.write( i2c_addr, cmd, 1, true);
 _i2c.read ( i2c_addr, d, len);
 }
 
@@ -48,6 +71,13 @@
     _i2c.write( MMA8652_SLAVE_ADDR, data, 2);
 }
 
+char  MMA8652::getWhoAmI(void)
+{
+    static char d;
+    RegRead( /*MMA8652_WHOAMI*/ 0x0d, &d, 1);
+    return(d);
+}
+
 void MMA8652::ReadXYZ(float * a)
 {
     char d[7];
@@ -62,4 +92,27 @@
     a[1] = (float) t[1] / 16384.0;
     a[2] = (float) t[2] / 16384.0;
   
-}
\ No newline at end of file
+}
+
+
+void MMA8652::ReadXYZraw(int16_t * d)
+{
+char res[6];
+int16_t acc;
+    RegRead( MMA8652_OUT_X_MSB, res, 6);
+
+    acc = (res[0] << 6) | (res[1] >> 2);
+    if (acc > UINT14_MAX/2)
+        acc -= UINT14_MAX;
+    d[0] = acc;
+    acc = (res[2] << 6) | (res[3] >> 2);
+    if (acc > UINT14_MAX/2)
+        acc -= UINT14_MAX;
+    d[1] = acc;
+    acc = (res[4] << 6) | (res[5] >> 2);
+    if (acc > UINT14_MAX/2)
+        acc -= UINT14_MAX;
+    d[2] = acc;
+}
+  
+   
\ No newline at end of file
--- a/MMA8652.h	Mon Apr 07 00:59:06 2014 +0000
+++ b/MMA8652.h	Sat Apr 19 01:28:26 2014 +0000
@@ -1,3 +1,21 @@
+/* Copyright (c) 2010-2011 mbed.org, MIT License
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+* and associated documentation files (the "Software"), to deal in the Software without
+* restriction, including without limitation the rights to use, copy, modify, merge, publish,
+* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in all copies or
+* substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+
 #ifndef MMA8652_H
 #define MMA8652_H
 
@@ -9,6 +27,7 @@
 
 // MMA8652 internal register addresses
 #define MMA8652_STATUS 0x00
+#define MMA8652_OUT_X_MSB 0x01
 #define MMA8652_WHOAMI 0x0D
 #define MMA8652_XYZ_DATA_CFG 0x0E
 #define MMA8652_CTRL_REG1 0x2A
@@ -18,38 +37,33 @@
 {
 public:
     /**
-    * MPL3115A2 constructor
+    * MMA8652 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();
+
+  /**
+  * MMA8652 destructor
+  */
+  ~MMA8652();    
+  /**
+   * Get XYZ axis acceleration in floating point G's
+   *
+   * @param res array where acceleration data will be stored
+   */
+    void ReadXYZ(float * a);
     
-    /**
-    * Return the STATUS register value
-    *
-    * @returns STATUS register value
-    */
-    //unsigned char getStatus( void);
+   /**
+   * Get XYZ axis acceleration, signed 16 bit values
+   *
+   * @param res array where acceleration data will be stored
+   */    
+    void ReadXYZraw(int16_t * d);
+    char getWhoAmI(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;
@@ -59,7 +73,6 @@
     
     void RegRead( char reg, char * d, int len);
 
-
 };
 
 #endif