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:
Thu Oct 17 10:21:37 2013 +0000
Parent:
8:89272163f395
Child:
10:ca9ba7ad4e94
Commit message:
super clean

Changed in this revision

MMA8452.cpp Show annotated file Show diff for this revision Revisions of this file
MMA8452.h Show annotated file Show diff for this revision Revisions of this file
--- 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;
     
 }
 
--- a/MMA8452.h	Thu Oct 17 10:08:51 2013 +0000
+++ b/MMA8452.h	Thu Oct 17 10:21:37 2013 +0000
@@ -142,7 +142,7 @@
         *
         *   This method is used to find out the system mode of the chip ACTIVE = 0x00 or STANDBY = 0x01
       */
-      int get_SystemMode(int& deviceSystemMode);
+      int get_SystemMode(int *dst);
       
       
       /** Get status of the MMA8452 (not required)
@@ -154,7 +154,7 @@
         *   x,y and z values have been overwritten before they have been read since a change happened.
         *   
       */
-      int get_Status(int& deviceStatus);
+      int get_Status(int *dst);
       
       
       /** Activate the MMA8452 (required)
@@ -185,7 +185,7 @@
         *   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(int* CTRL_Reg);
+      int get_CTRL_Reg1(int* dst);
       
       
        /** Initialization of device MMA8452 (required)
@@ -198,7 +198,7 @@
         * return 0 for success or
         * return 1 for failure.
         */        
-      int get_DeviceID(int& deviceID);  
+      int get_DeviceID(int *dst);  
       
       int read_y();
       
@@ -236,7 +236,7 @@
          * @param addr The internal registeraddress of the MMA8452
          * @returns The value of the register
          */
-      char read_reg(char addr);
+      int read_reg(char addr, int *dst);
         
         /** Write to specified MMA8452 register
         *