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:
10:ca9ba7ad4e94
Parent:
9:dfb0f6a7a455
Child:
11:dfd1e0afcb7b
--- a/MMA8452.h	Thu Oct 17 10:21:37 2013 +0000
+++ b/MMA8452.h	Tue Mar 04 11:14:34 2014 +0000
@@ -1,4 +1,4 @@
-// Author: Nicholas Herriot
+// Author: Nicholas Herriot, Ashley Mills
 /* Copyright (c) 2013 Vodafone, MIT License
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
@@ -116,10 +116,21 @@
 #define PDET_STATUS 0x09        // Tap/Pulse Detection Register (Read/Write)
 #define PD_STATUS 0xA           // Tap/Pulse Debounce Count Register (Read/Write)
  
-class Accelerometer_MMA8452         
+class MMA8452         
 {        
     public:
+    
+       enum OperationalMode {
+           MODE_STANDBY,
+           MODE_ACTIVE_2G,
+           MODE_ACTIVE_4G,
+           MODE_ACTIVE_8G
+       };
         
+       enum BitDepth {
+           BIT_DEPTH_8,
+           BIT_DEPTH_12
+       };
         
        /** Create an accelerometer object connected to the specified I2C object
         *
@@ -127,13 +138,13 @@
         * @param scl I2C8452 clock port
         * 
         */ 
-      Accelerometer_MMA8452(PinName sda, PinName scl, int frequency);
+      MMA8452(PinName sda, PinName scl, int frequency);
       //Accelerometer_MMA8452(PinName sda, PinName scl);
        
        /** Destroys an MMA8452 object
         *
         */
-      ~Accelerometer_MMA8452();
+      ~MMA8452();
             
       /** Get system mode of the MMA8452 (not required)
         *   returns 0 for success in reading the system mode of the chip
@@ -142,8 +153,7 @@
         *
         *   This method is used to find out the system mode of the chip ACTIVE = 0x00 or STANDBY = 0x01
       */
-      int get_SystemMode(int *dst);
-      
+      int getSystemMode(int *dst);    
       
       /** Get status of the MMA8452 (not required)
         *   returns 0 for success in reading the status of the chip
@@ -154,7 +164,7 @@
         *   x,y and z values have been overwritten before they have been read since a change happened.
         *   
       */
-      int get_Status(int *dst);
+      int getStatus(int *dst);
       
       
       /** Activate the MMA8452 (required)
@@ -198,7 +208,7 @@
         * return 0 for success or
         * return 1 for failure.
         */        
-      int get_DeviceID(int *dst);  
+      int getDeviceID(int *dst);  
       
       int read_y();
       
@@ -208,19 +218,19 @@
         *
         * @returns The value of x acceleration
         */
-      int read_x_raw(char *xaxis);
+      int readRawX(char *xaxis);
       //int read_x(int& xaxisLSB);
       /** Read the y register of the MMA8452
         *
         * @returns The value of y acceleration
         */
-      int read_y_raw(char *yaxis);
+      int readRawY(char *yaxis);
       
       /** Read the z register of the MMA8452
         *
         * @returns The value of z acceleration
         */
-       int read_z_raw(char * zaxis);
+       int readRawZ(char * zaxis);
       
       /** Read the x,y and z registers of the MMA8452
        *
@@ -229,25 +239,27 @@
        * returns 0 for success, and 1 for failure
        *
        */ 
-      int read_xyz(char *x, char *y, char *z); 
+      int readRawXYZ(char *x, char *y, char *z); 
             
         /** Read from specified MMA8452 register
          *
          * @param addr The internal registeraddress of the MMA8452
          * @returns The value of the register
          */
-      int read_reg(char addr, int *dst);
+      int readRegister(char addr, int *dst);
         
         /** Write to specified MMA8452 register
         *
         * @param addr The internal registeraddress of the MMA8452
         * @param data New value of the register
         */    
-      void write_reg(char addr, char data); 
+      void writeRegister(char addr, char data);
       
+      int setOperationalMode(OperationalMode m);
+      int setBitDepth(BitDepth b);
    
     private:
-      int read_raw(char src, char *dst, int len);
+      int readRaw(char src, char *dst, int len);
     
     
       I2C m_i2c;