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:
9:dfb0f6a7a455
Parent:
8:89272163f395
Child:
10:ca9ba7ad4e94
--- a/MMA8452.cpp	Thu Oct 17 10:08:51 2013 +0000
+++ b/MMA8452.cpp	Thu Oct 17 10:21:37 2013 +0000
@@ -50,25 +50,9 @@
 // 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)
-{
-    m_i2c.start();
-    if( m_i2c.write(_writeAddress) == 0)
-    {
-        return 1;                                   // we failed to write the mcu address on the bus to initiate dialogue 
-    }
-    if( m_i2c.write( CTRL_REG_1) == 0) 
-    {
-        return 1;                                       // we failed to write 'status' to the chip
-    }
-    m_i2c.start();
-    if( m_i2c.write(_readAddress) == 0)          // this is asking to read the slave mcu address - even though it's a 'write' method!!! Crap API...
-    {
-        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);
-    m_i2c.stop();
-    return 0;
+int Accelerometer_MMA8452::get_CTRL_Reg1(int* dst)
+{   
+   return read_reg(CTRL_REG_1,dst); 
 }
 
 // Setting the control register bit 1 to true to activate the MMA8452
@@ -94,77 +78,24 @@
 }
 
 // Get real time status of device - it can be STANDBY, WAKE or SLEEP
-int Accelerometer_MMA8452::get_SystemMode(int& deviceSystemMode)
+int Accelerometer_MMA8452::get_SystemMode(int *dst)
 {
-    m_i2c.start();
-    if( m_i2c.write(_writeAddress) == 0)        
-    {
-        return 1;                                       // we failed to write the mcu address on the bus to initiate dialogue 
-    }
-    if( m_i2c.write( SYSMOD) == 0) 
-    {
-        return 1;                                       // we failed to write 'status' to the chip
-    }
-    m_i2c.start();
-    if( m_i2c.write(_readAddress) == 0)          // this is asking to read the slave mcu address - even though it's a 'write' method!!! Crap API...
-    {
-        return 1;                                       // we failed to request a read from that mcu - this really is just writing the mcu vaule on the bus
-    }
-    deviceSystemMode  = m_i2c.read(0);
-    m_i2c.stop();
-    return 0;
-    
-
+    return read_reg(SYSMOD,dst);
 }
 
 
 
 // Get real time status of device - it can be STANDBY, WAKE or SLEEP
-int Accelerometer_MMA8452::get_Status(int& deviceStatus)
+int Accelerometer_MMA8452::get_Status(int* dst)
 {
-    m_i2c.start();
-    if( m_i2c.write(_writeAddress) == 0) // tell the bus this is a write
-    {
-        return 1;                                       // we failed to write the mcu address on the bus to initiate dialogue 
-    }
-    if( m_i2c.write(STATUS) == 0)
-    {
-        return 1;                                       // we failed to write 'status' to the chip
-    }
-    m_i2c.start();
-    if( m_i2c.write(_readAddress) == 0)          // this is asking to read the slave mcu address - even though it's a 'write' method!!! Crap API...
-    {
-        return 1;                                       // we failed to request a read from that mcu - this really is just writing the mcu vaule on the bus
-    }
-    deviceStatus  = m_i2c.read(0);
-    m_i2c.stop();
-    return 0;
-    
-
+    return read_reg(STATUS,dst);
 }
 
 
 // Get device ID 
-int Accelerometer_MMA8452::get_DeviceID(int& deviceID)
+int Accelerometer_MMA8452::get_DeviceID(int *dst)
 {
-    m_i2c.start();
-    if( m_i2c.write(_writeAddress) == 0)          // just good practice to force bit 1 to a '0' by ANDing with 0xFE
-    {
-        return 1;                                       // we failed to write the mcu address on the bus to initiate dialogue 
-    }
-    if( m_i2c.write( WHO_AM_I) == 0) 
-    {
-        return 1;                                       // we failed to write 'who am i' to the chip
-    }
-    m_i2c.start();
-    if( m_i2c.write(_readAddress) == 0)          // this is asking to read the slave mcu address - even though it's a 'write' method!!! Crap API...
-    {
-        return 1;                                       // we failed to request a read from that mcu - this really is just writing the mcu vaule on the bus
-    }
-    deviceID  = m_i2c.read(0);
-    m_i2c.stop();
-    return 0;
-
+    return read_reg(WHO_AM_I,dst);
 }
 
 
@@ -220,22 +151,7 @@
 //int Accelerometer_MMA8452::read_x(int& xaxisLSB)
 int Accelerometer_MMA8452::read_x_raw(char *xaxis)
 {   
-    // this is the register we want to get data from               
-    char xaxis_register[1] = {OUT_X_MSB};
-    //signed short s = 0;
-    
-    if(m_i2c.write(_writeAddress,xaxis_register,1,true) == 0)
-    {
-        if(m_i2c.read(_readAddress,xaxis,2)==0)
-        {
-           return 0;
-        }
-    }
-    
-    // failure case
-    xaxis[0] = 0x00;                            // make sure the array is set to zero
-    xaxis[1] = 0x00;
-    return 1;                                   // failed to write and request the OUT_X_MSB bit    
+    return read_raw(OUT_X_MSB,xaxis,2);  
 }
 
 
@@ -353,7 +269,7 @@
 
 
         // Read from specified MMA7660FC register
-char Accelerometer_MMA8452::read_reg(char addr)
+int Accelerometer_MMA8452::read_reg(char addr, int *dst)
 {
     
     m_i2c.start();
@@ -361,7 +277,7 @@
     {
         return 1;                                   // we failed to write the mcu address on the bus to initiate dialogue 
     }
-    if( m_i2c.write( addr) == 0) 
+    if( m_i2c.write(addr) == 0) 
     {
         return 1;                                       // we failed to write 'status' to the chip
     }
@@ -370,10 +286,10 @@
     {
         return 1;                                       // we failed to request a read from that mcu - this really is just writing the mcu vaule on the bus
     }
-    char c  = m_i2c.read(0);
+    *dst = m_i2c.read(0);
     m_i2c.stop();      
  
-    return c;
+    return 0;
     
 }