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:
6:f6bde04bf8be
Parent:
5:b3d0abd97e55
Child:
8:89272163f395
--- a/MMA8452.cpp	Wed Oct 16 18:55:16 2013 +0000
+++ b/MMA8452.cpp	Thu Oct 17 09:41:35 2013 +0000
@@ -19,20 +19,19 @@
 
 # include "MMA8452.h"
 
-
-
-
-
-
 // Connect module at I2C address using I2C port pins sda and scl
 Accelerometer_MMA8452::Accelerometer_MMA8452(PinName sda, PinName scl,int frequency) : m_i2c(sda, scl) , m_frequency(frequency)
 {
     //m_i2c.frequency(m_frequency);
+    
+    // setup read and write addresses to avoid duplication
+   _readAddress   = (MMA8452_ADDRESS<<1) | 0x01;
+   _writeAddress  = (MMA8452_ADDRESS<<1) & 0xFE;
 }
 
 
 // Destroys instance
-Accelerometer_MMA8452::~Accelerometer_MMA8452()
+Accelerometer_MMA8452::~Accelerometer_MMA8452() 
 {
 
 }
@@ -40,32 +39,22 @@
 // Setting the control register bit 1 to true to activate the MMA8452
 int Accelerometer_MMA8452::activate()
 {
-    char mcu_address = (MMA8452_ADDRESS<<1);
-    char init[2];
-    init[0] = CTRL_REG_1;                 // control register 1
-    init[1] = ACTIVE;                     // set to active
+    // set control register 1 to active
+    char init[2] = {CTRL_REG_1,ACTIVE};
     
-    if(m_i2c.write(mcu_address,init,2) == 0)
-    {
-         return 0;  // return 0 to indicate success
-    }
-    else
-    {
-        return 1;   // crumbs it failed!!!
-    }
-         
+    // perform write and return error code
+    return m_i2c.write(_writeAddress,init,2);
 }
 
 
 // Get 'Fast Read Mode' called F_READ. If bit 1 is set '1' then F_READ is active. Fast read will skip LSB when reading xyz
 // resisters from 0x01 to 0x06. When F_READ is '0' then all 6 registers will be read.
 
-int Accelerometer_MMA8452::get_CTRL_Reg1(int& CTRL_Reg)
-{
+int Accelerometer_MMA8452::get_CTRL_Reg1(int* CTRL_Reg)
 {
     char mcu_address = (MMA8452_ADDRESS<<1);
     m_i2c.start();
-    if( m_i2c.write( mcu_address & 0xFE) == 0)          // just good practice to force bit 1 to a '0' by ANDing with 0xFE
+    if( m_i2c.write(_writeAddress) == 0)
     {
         return 1;                                       // we failed to write the mcu address on the bus to initiate dialogue 
     }
@@ -78,16 +67,9 @@
     {
         return 1;                                       // we failed to request a read from that mcu - this really is just writing the mcu vaule on the bus
     }
-    CTRL_Reg  = m_i2c.read(0);
+    *CTRL_Reg  = m_i2c.read(0);
     m_i2c.stop();
     return 0;
-    
-
-}
-
-
-
-
 }
 
 // Setting the control register bit 1 to true to activate the MMA8452