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:08:51 2013 +0000
Parent:
7:8aa5123d403f
Parent:
6:f6bde04bf8be
Child:
9:dfb0f6a7a455
Commit message:
made some simplifications

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 09:40:10 2013 +0000
+++ b/MMA8452.cpp	Thu Oct 17 10:08:51 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,75 +39,46 @@
 // 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 
+        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( mcu_address | 0x01) == 0)          // this is asking to read the slave mcu address - even though it's a 'write' method!!! Crap API...
+    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);
+    *CTRL_Reg  = m_i2c.read(0);
     m_i2c.stop();
     return 0;
-    
-
-}
-
-
-
-
 }
 
 // Setting the control register bit 1 to true to activate the MMA8452
 int Accelerometer_MMA8452::standby()
 {
-    char mcu_address = (MMA8452_ADDRESS<<1);
-    char init[2];
-    init[0] = CTRL_REG_1;                 // control register 1
-    init[1] = STANDBY;                    // set to standby
+    // set control register 1 to standby
+    char init[2] = {CTRL_REG_1,STANDBY};
     
-    if(m_i2c.write(mcu_address,init,2) == 0)
-    {
-         // pc.printf("The initialisation worked");
-         return 0;  // return 0 to indicate success
-    }
-    else
-    {
-        // pc.printf("The initialisation failed");
-        return 1;   // crumbs it failed!!!
-    }
-         
+    // write to the register and return the error code
+    return m_i2c.write(_writeAddress,init,2);
 }
 
 
@@ -126,9 +96,8 @@
 // Get real time status of device - it can be STANDBY, WAKE or SLEEP
 int Accelerometer_MMA8452::get_SystemMode(int& deviceSystemMode)
 {
-    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 
     }
@@ -137,7 +106,7 @@
         return 1;                                       // we failed to write 'status' to the chip
     }
     m_i2c.start();
-    if( m_i2c.write( mcu_address | 0x01) == 0)          // this is asking to read the slave mcu address - even though it's a 'write' method!!! Crap API...
+    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
     }
@@ -153,18 +122,17 @@
 // Get real time status of device - it can be STANDBY, WAKE or SLEEP
 int Accelerometer_MMA8452::get_Status(int& deviceStatus)
 {
-    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) // 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) 
+    if( m_i2c.write(STATUS) == 0)
     {
         return 1;                                       // we failed to write 'status' to the chip
     }
     m_i2c.start();
-    if( m_i2c.write( mcu_address | 0x01) == 0)          // this is asking to read the slave mcu address - even though it's a 'write' method!!! Crap API...
+    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
     }
@@ -179,9 +147,8 @@
 // Get device ID 
 int Accelerometer_MMA8452::get_DeviceID(int& deviceID)
 {
-    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)          // 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 
     }
@@ -190,7 +157,7 @@
         return 1;                                       // we failed to write 'who am i' to the chip
     }
     m_i2c.start();
-    if( m_i2c.write( mcu_address | 0x01) == 0)          // this is asking to read the slave mcu address - even though it's a 'write' method!!! Crap API...
+    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
     }
@@ -226,6 +193,25 @@
 }
 */
 
+int Accelerometer_MMA8452::read_raw(char src, char *dst, int len) {
+    // this is the register we want to get data from               
+    char register_address[1];
+    register_address[0] = src;
+    
+    if(m_i2c.write(_writeAddress,register_address,1,true) == 0)
+    {
+        if(m_i2c.read(_readAddress,dst,len)==0)
+        {
+           return 0;
+        }
+    }
+    
+    // failure case, zero array and return error
+    for(int i=0; i<len; i++) {
+       dst[i] = 0x00;
+    }
+    return 1;   
+}
 
 // Reads x data. This method reads two registers containing the x-axis values from the accelerometer. 
 // It takes a 2 byte char array and copies the register values into the buffer. If it fails the char array
@@ -233,37 +219,23 @@
 // the raw data.
 //int Accelerometer_MMA8452::read_x(int& xaxisLSB)
 int Accelerometer_MMA8452::read_x_raw(char *xaxis)
-{
-    char mcu_address = (MMA8452_ADDRESS<<1);                // this is the slave address on the bus we want data from
-    char xaxis_buffer[2];                                   // this will contain data from that register
-    char xaxis_register[1];                     
-    xaxis_register[0] = OUT_X_MSB;                          // this is the register we want to get data from
+{   
+    // 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(mcu_address,xaxis_register,1,true) == 0)
+    if(m_i2c.write(_writeAddress,xaxis_register,1,true) == 0)
     {
-        if(m_i2c.read(mcu_address,xaxis_buffer,2) == 0)
+        if(m_i2c.read(_readAddress,xaxis,2)==0)
         {
-            //strcpy(xaxis, xaxis_buffer);
-            memcpy(xaxis, xaxis_buffer, 2);
-            //xaxis[0] = 0x00;                        // make sure the array is set to zero
-            //xaxis[1] = 0x00;
-            //s = *reinterpret_cast<short*>(&xaxis);
-            return 0;                               // great we got the two octets
-        }
-        else
-        {
-            xaxis[0] = 0x00;                        // make sure the array is set to zero
-            xaxis[1] = 0x00;
-            return 1;                               // failed to read the 12 bit x value
+           return 0;
         }
     }
-    else
-    {
-        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
-    }    
+    
+    // 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    
 }
 
 
@@ -272,38 +244,8 @@
 // is set to '0'. It returns '0' success '1' fail. This method does nothing to the registers - just returns
 // the raw data.
 
-int Accelerometer_MMA8452::read_y_raw(char *yaxis)
-{
-    char mcu_address = (MMA8452_ADDRESS<<1);                // this is the slave address on the bus we want data from
-    char yaxis_buffer[2];                                   // this will contain data from that register
-    char yaxis_register[1];                     
-    yaxis_register[0] = OUT_Y_MSB;                          // this is the register we want to get data from
-    //signed short s = 0;
-    
-    if(m_i2c.write(mcu_address,yaxis_register,1,true) == 0)
-    {
-        if(m_i2c.read(mcu_address,yaxis_buffer,2) == 0)
-        {
-            //strcpy(yaxis, yaxis_buffer);
-            memcpy(yaxis, yaxis_buffer, 2);
-            //yaxis[0] = 0x00;                        // make sure the array is set to zero
-            //yaxis[1] = 0x00;
-            //s = *reinterpret_cast<short*>(&xaxis);
-            return 0;                               // great we got the two octets
-        }
-        else
-        {
-            yaxis[0] = 0x00;                        // make sure the array is set to zero
-            yaxis[1] = 0x00;
-            return 1;                               // failed to read the 12 bit y value
-        }
-    }
-    else
-    {
-        yaxis[0] = 0x00;                            // make sure the array is set to zero
-        yaxis[1] = 0x00;
-        return 1;                                   // failed to write and request the OUT_Y_MSB bit
-    }    
+int Accelerometer_MMA8452::read_y_raw(char *yaxis) {
+   return read_raw(OUT_Y_MSB,yaxis,2);
 }
 
 
@@ -315,37 +257,7 @@
 
 int Accelerometer_MMA8452::read_z_raw(char *zaxis)
 {
-    char mcu_address = (MMA8452_ADDRESS<<1);                // this is the slave address on the bus we want data from
-    char zaxis_buffer[2];                                   // this will contain data from that register
-    char zaxis_register[1];                     
-    zaxis_register[0] = OUT_Z_MSB;                          // this is the register we want to get data from
-    //signed short s = 0;
-    
-    if(m_i2c.write(mcu_address,zaxis_register,1,true) == 0)
-    {
-        //if(m_i2c.read(mcu_address,zaxis_buffer,2) == 0)
-        if(m_i2c.read(mcu_address,zaxis,2) == 0)
-        {
-            //strcpy(yaxis, yaxis_buffer);
-            //memcpy(zaxis, zaxis_buffer, 2);
-            //yaxis[0] = 0x00;                        // make sure the array is set to zero
-            //yaxis[1] = 0x00;
-            //s = *reinterpret_cast<short*>(&xaxis);
-            return 0;                               // great we got the two octets
-        }
-        else
-        {
-            zaxis[0] = 0x00;                        // make sure the array is set to zero
-            zaxis[1] = 0x00;
-            return 1;                               // failed to read the 12 bit y value
-        }
-    }
-    else
-    {
-        zaxis[0] = 0x00;                            // make sure the array is set to zero
-        zaxis[1] = 0x00;
-        return 1;                                   // failed to write and request the OUT_Y_MSB bit
-    }    
+   return read_raw(OUT_Z_MSB,zaxis,2);
 }
 
 
@@ -444,13 +356,22 @@
 char Accelerometer_MMA8452::read_reg(char addr)
 {
     
-    m_i2c.start();                  // Start
-    m_i2c.write(0x98);              // A write to device 0x98
-    m_i2c.write(addr);              // Register to read
-    m_i2c.start();                  
-    m_i2c.write(0x99);              // Read from device 0x99
-    char c = m_i2c.read(0);         // Read the data
-    m_i2c.stop();                   
+    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( addr) == 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
+    }
+    char c  = m_i2c.read(0);
+    m_i2c.stop();      
  
     return c;
     
--- a/MMA8452.h	Thu Oct 17 09:40:10 2013 +0000
+++ b/MMA8452.h	Thu Oct 17 10:08:51 2013 +0000
@@ -115,11 +115,6 @@
 #define SR_STATUS 0x08          // Auto-Wake and Active Mode Portrait/Landscape Samples per Seconds Register (Read/Write)
 #define PDET_STATUS 0x09        // Tap/Pulse Detection Register (Read/Write)
 #define PD_STATUS 0xA           // Tap/Pulse Debounce Count Register (Read/Write)
-
-
-
-
-
  
 class Accelerometer_MMA8452         
 {        
@@ -139,9 +134,7 @@
         *
         */
       ~Accelerometer_MMA8452();
-      
-      
-      
+            
       /** Get system mode of the MMA8452 (not required)
         *   returns 0 for success in reading the system mode of the chip
         *   returns 1 for failure in reading the system mode of the chip
@@ -152,7 +145,6 @@
       int get_SystemMode(int& deviceSystemMode);
       
       
-      
       /** Get status of the MMA8452 (not required)
         *   returns 0 for success in reading the status of the chip
         *   returns 1 for failure in reading the status of  the chip
@@ -163,7 +155,6 @@
         *   
       */
       int get_Status(int& deviceStatus);
-     
       
       
       /** Activate the MMA8452 (required)
@@ -176,7 +167,6 @@
       int activate();
  
  
- 
        /** Standby the MMA8452 (not required)
         *   returns 0 for success in activating the chip
         *   returns 1 for failure in activating the chip
@@ -195,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* CTRL_Reg);
       
       
        /** Initialization of device MMA8452 (required)
@@ -257,8 +247,13 @@
       
    
     private:
+      int read_raw(char src, char *dst, int len);
+    
+    
       I2C m_i2c;
       int m_frequency;
+      int _readAddress;
+      int _writeAddress;
          
 };