Library for driving the MMA8452 accelerometer over I2C

Dependents:   MMA8452_Test MMA8452_Demo Dualing_Tanks IMU-Controlled_MP3_Player ... more

Here is a simple example:

#include "mbed.h"
#include "MMA8452.h"

int main() {
   Serial pc(USBTX,USBRX);
   pc.baud(115200);
   double x = 0, y = 0, z = 0;

   MMA8452 acc(p28, p27, 40000);
   acc.setBitDepth(MMA8452::BIT_DEPTH_12);
   acc.setDynamicRange(MMA8452::DYNAMIC_RANGE_4G);
   acc.setDataRate(MMA8452::RATE_100);
   
   while(1) {
      if(!acc.isXYZReady()) {
         wait(0.01);
         continue;
      }
      acc.readXYZGravity(&x,&y,&z);
      pc.printf("Gravities: %lf %lf %lf\r\n",x,y,z);
   }
}

An easy way to test that this actually works is to run the loop above and hold the MMA8452 parallel to the ground along the respective axis (and upsidedown in each axis). You will see 1G on the respective axis and 0G on the others.

Revision:
19:4d6cd7140a71
Parent:
18:27d839e6dc0e
Child:
20:d55e9d7eb17e
--- a/MMA8452.h	Wed Mar 05 17:04:04 2014 +0000
+++ b/MMA8452.h	Thu Mar 06 18:07:43 2014 +0000
@@ -186,15 +186,41 @@
        */ 
       int readXYZRaw(char *dst);
       
+      int readXRaw(char *dst);
+      int readYRaw(char *dst);
+      int readZRaw(char *dst);
+      
       /**
-       * Read the x,y, and z counts of the MMA7452.
+       * Read the x, y, and z counts of the MMA7452 axes.
+       * 
+       * Count resolution is either 8 bits or 12 bits, and the range is either +-2G, +-4G, or +-8G
+       * depending on settings.
+       * 
+       * This function queries the MMA8452 and returns the signed counts for each axes.
+       *
+       * @param x Pointer to integer to store x count
+       * @param y Pointer to integer to store y count
+       * @param z Pointer to integer to store z count
+       * @return 0 on success, 1 on failure
        */
       int readXYZCounts(int *x, int *y, int *z);
+      int readXCount(int *x);
+      int readYCount(int *y);
+      int readZCount(int *z);
+      
       int readXYZGravity(double *x, double *y, double *z);
       
+      
+      /// Returns 1 if data has been internally sampled (is available) for the x-axis since last read, 0 otherwise.
       int isXReady();
+      
+      /// Returns 1 if data has been internally sampled (is available) for the y-axis since last read, 0 otherwise.
       int isYReady();
+      
+      /// Returns 1 if data has been internally sampled (is available) for the z-axis since last read, 0 otherwise.
       int isZReady();
+      
+      /// Returns 1 if data has been internally sampled (is available) for all axes since last read, 0 otherwise.
       int isXYZReady();
             
       /**