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.

Files at this revision

API Documentation at this revision

Comitter:
ashleymills
Date:
Wed Mar 05 15:36:03 2014 +0000
Parent:
14:0602b45ca70f
Child:
16:d6dde2318edc
Commit message:
Removed crap from comments.

Changed in this revision

MMA8452.h Show annotated file Show diff for this revision Revisions of this file
--- a/MMA8452.h	Wed Mar 05 15:09:21 2014 +0000
+++ b/MMA8452.h	Wed Mar 05 15:36:03 2014 +0000
@@ -1,3 +1,5 @@
+#pragma once
+
 // Authors: Ashley Mills, Nicholas Herriot
 /* Copyright (c) 2013 Vodafone, MIT License
  *
@@ -23,47 +25,8 @@
 
 #define DBG(...) pc.printf(__VA_ARGS__); pc.printf("\r\n");
 #define DBGX(...) pc.print(__VA_ARGS__);
-
-//#define SA0 1
-//#if SA0
-  //#define MMA8452_ADDRESS 0x3A  // SA0 is high, 0x1C if low - it should be 0x1D, but we shift now to save shifting in the code
-//#else
-  //#define MMA8452_ADDRESS 0x1C
-//#endif
-
-//#ifndef MBED_MMA8452
-
-//#define MBED_MMA8452
  
-#include "mbed.h"
- 
-/** Accelerometer MMA8452 class 
- *
- * Example:
- * @code
- * 
- * #include "mbed.h"
- * #include "MMA8452.h"
- * 
- * 
- * Accelerometer_MMA8452 Acc(p28, p27);
- * serial pc(USBTX, USBRX);
- *
- * int main() 
- * {
- *   Acc.init(); 
- *      while(1)
- *      {
- *          int x=0, y=0, z=0;
- *          Acc.read_Tilt(&x, &y, &z);
- *          pc.printf("Tilt x: %2.2f degree \n", x);                    // Print the tilt orientation of the X axis
- *          pc.printf("Tilt y: %2.2f degree \n", y);                    // Print the tilt orientation of the Y axis
- *          pc.printf("Tilt z: %2.2f degree \n", z);                    // Print the tilt orientation of the Z axis
- *          wait(1);       
- *      }
- * }
- * @endcode
- */ 
+#include "mbed.h" 
 
 // More info on MCU Master address can be found on section 5.10.1 of http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=MMA8452Q
 #define SA0 1
@@ -173,87 +136,45 @@
           RATE_UNKNOWN
        };
        
-       //void setDynamicRange(
         
-       /** Create an accelerometer object connected to the specified I2C object
+       /**
+        * Create an accelerometer object connected to the specified I2C pins.
         *
         * @param sda I2C data port
-        * @param scl I2C8452 clock port
+        * @param scl I2C clock port
+        * @param frequency 
         * 
         */ 
       MMA8452(PinName sda, PinName scl, int frequency);
-      //Accelerometer_MMA8452(PinName sda, PinName scl);
        
-       /** Destroys an MMA8452 object
-        *
-        */
+      /// Destructor
       ~MMA8452();
       
-      //int setOperationMode(OperationMode m);
-      
-      
-      /** Activate the MMA8452 (required)
-        *   returns 0 for success in activating the chip
-        *   returns 1 for failure in activating the chip
-        *   -currrently no retries or waiting is done, this method tries 1 time the exits.
-        *
-        *   This will set the device 'active' even if it's already active. It's just a way to force that state.
-      */
+      /**
+       * Puts the MMA8452 in active mode.
+       * @return 0 on success, 1 on failure.
+       */
       int activate();
  
- 
-       /** Standby the MMA8452 (not required)
-        *   returns 0 for success in activating the chip
-        *   returns 1 for failure in activating the chip
-        *   -currrently no retries or waiting is done, this method tries 1 time the exits.
-        *
-        *   This will set the device 'standby' even if it's already in standby. It's just a way to force that state.
-      */
-      int standby();
-
- 
-       /** get_CTRL_Reg1 the MMA8452 (not required)
-        *   returns 0 for success in activating the chip
-        *   returns 1 for failure in activating the chip
-        *   -currrently no retries or waiting is done, this method tries 1 time the exits.
-        *
-        *   This will return the state of the control register 1. This holds and sets values for auto wake, sleep mode
-        *   output data rate, fast read mode and active/standby. More info on 6.7 of pdf for MMA8452 Freescale doc.
-      */
-      int get_CTRL_Reg1(char* dst);
-      
+      /**
+       * Puts the MMA8452 in standby.
+       * @return 0 on success, 1 on failure.
+       */
+      int standby();      
       
        /** Initialization of device MMA8452 (required)
         */
       void init();
     
-       /** Read the device ID from the accelerometer
+       /**
+        * Read the device ID from the accelerometer (should be 0x2a)
         *
-        * @param *deviceID Value of device - is should be 0x2A
-        * return 0 for success or
-        * return 1 for failure.
+        * @param pointer to store the ID
+        * @return 0 on success, 1 on failure.
         */        
       int getDeviceID(char* dst);  
       
       int getStatus(char* dst);  
-
-      /** Read the x register of the MMA8452
-        *
-        * @returns The value of x acceleration
-        */
-      int readRawX(char *dst);
-      
-      /** Read the y register of the MMA8452
-       * @param dst The destination buffer
-       * @returns The value of y acceleration
-       */
-      int readRawY(char *dst);
-      
-      /** Read the z register of the MMA8452
-        *
-        * @returns The value of z acceleration
-        */
-       int readRawZ(char * zaxis);
       
       /** 
        * Read the x,y, and z registers of the MMA8452.
@@ -262,7 +183,11 @@
        * BIT_DEPTH_8 and 6 bytes for BIT_DEPTH_12. It is upto the caller to ensure this.
        * @return 0 for success, and 1 for failure
        */ 
-      int readXYZRaw(char *dst); 
+      int readXYZRaw(char *dst);
+      
+      /**
+       * Read the x,y, and z counts of the MMA7452.
+       */
       int readXYZCounts(int *x, int *y, int *z);
       int readXYZGravity(double *x, double *y, double *z);
       
@@ -271,7 +196,8 @@
       int isZReady();
       int isXYZReady();
             
-      /** Read from specified MMA8452 register
+      /** 
+       * Read from specified MMA8452 register.
        *
        * @param addr The internal registeraddress of the MMA8452
        * @return The value of the register
@@ -304,6 +230,7 @@
       int setDynamicRange(DynamicRange range, int toggleActivation=1);
       int setBitDepth(BitDepth depth, int toggleActivation=1);
       int setDataRate(DataRateHz dataRate, int toggleActivation=1);
+      
       DynamicRange getDynamicRange();
       DataRateHz getDataRate();
       BitDepth getBitDepth();
@@ -326,6 +253,4 @@
       BitDepth _bitDepth;
       DynamicRange _dynamicRange;
          
-};
-
-//#endif 
+};
\ No newline at end of file