The main.cpp program below demonstrates a simple way to interface with the MMA8452 accelerometer. The function reads in the acceleration data in the X, Y, and Z directions and displays them through a serial com port on a PC. The Putty Output figure below shows the output of the accelerometer using Putty. The program also dims or brightens the mbed LEDs 1-3 based on whether or not they are at 'level'( 0 Gs) or above respectively. The video below previews the code in action.

Dependencies:   MMA8452 mbed

Fork of MMA8452_Test by Ashley Mills

Revision:
6:e3100f66ed6a
Parent:
5:756f9b157319
Child:
8:46eab8a51f91
--- a/main.cpp	Thu Mar 06 18:07:58 2014 +0000
+++ b/main.cpp	Fri Mar 07 12:02:48 2014 +0000
@@ -107,7 +107,7 @@
                   ); 
                   mismatchCount++;
                }
-               LOG("Sample %d of %d: %x %x %x %x %x %x",
+               LOG("12bit raw sample %d/%d: %x %x %x %x %x %x",
                   samples,nsamples,
                   bufferMulti[0],bufferMulti[1],
                   bufferMulti[2],bufferMulti[3],
@@ -120,7 +120,7 @@
                   LOG("Multi and single sampling mismatch");
                   mismatchCount++;
                }
-               LOG("Sample %d of %d: %x %x %x",
+               LOG("8 bit raw sample %d/%d: %x %x %x",
                   samples,nsamples,
                   bufferMulti[0],bufferMulti[1],bufferMulti[2]
                );
@@ -129,23 +129,40 @@
          case SAMPLE_COUNT:
             error = 0;
             error += acc->readXYZCounts(&xCountM,&yCountM,&zCountM);
+            error += acc->readXCount(&xCountS);
+            error += acc->readYCount(&yCountS);
+            error += acc->readZCount(&zCountS);
             if(error) {
                LOG("Error reading signed counts. Fail.");
                break;
             }
+            // check for equivlance (note this fails sometimes but this is expected)
+            if(xCountS!=xCountM || yCountS!=yCountM || zCountS!=zCountM) {
+               LOG("Multi and single sampling mismatch");
+               mismatchCount++;
+            }
+            LOG("Count sample %d/%d: %d %d %d",samples,nsamples,xCountM,yCountM,zCountM);
          break;
          case SAMPLE_GRAVITY:
             error = 0;
             error += acc->readXYZGravity(&xGravityM,&yGravityM,&zGravityM);
+            error += acc->readXGravity(&xGravityS);
+            error += acc->readYGravity(&yGravityS);
+            error += acc->readZGravity(&zGravityS);
             if(error) {
                LOG("Error reading gravities. Fail.");
                break;
             }
+            if(xGravityS!=xGravityM || yGravityS!=yGravityM || zGravityS!=zGravityM) {
+               LOG("Multi and single sampling mismatch");
+               mismatchCount++;
+            }
+            LOG("Gravity sample %d/%d: %lf %lf %lf",samples,nsamples,xGravityM,yGravityM,zGravityM);
          break;
       }
       samples++;
    }
-   LOG("mismatches %d/%d",mismatchCount,nsamples);
+   LOG("Mismatches between single and multi-byte reads %d/%d (mismatches are to be expected)",mismatchCount,nsamples);
    return true;
 }
 
@@ -366,30 +383,24 @@
           }
        }
     }
-    return true;
-    
-    // set bit depth to 8 and read some values
-    LOG("Sampling at BIT_DEPTH_8");
+
+    LOG("Samping gravities for interactive examination");
     if(acc.setBitDepth(MMA8452::BIT_DEPTH_8)) {
        LOG("Error setting bit depth. Fail.");
        return false;
     }
-    sampleMMA8452Raw(&acc,10);
-    
-    // set bit depth to 12 and read some values
-    LOG("Sampling at BIT_DEPTH_12");
-    acc.setDataRate(MMA8452::RATE_100);
-    acc.setBitDepth(MMA8452::BIT_DEPTH_12);
-    sampleMMA8452Raw(&acc,10);
-   
-    LOG("Sampling counts");
-    acc.setDynamicRange(MMA8452::DYNAMIC_RANGE_2G);
-    sampleMMA8452Counts(&acc,100);
-
-    LOG("Samping gravities");
-    acc.setBitDepth(MMA8452::BIT_DEPTH_8);
-    acc.setDynamicRange(MMA8452::DYNAMIC_RANGE_4G);
-    sampleMMA8452Gravities(&acc,2000);
+    if(acc.setDynamicRange(MMA8452::DYNAMIC_RANGE_4G)) {
+       LOG("Error setting dynamic range. Fail.");
+       return false;
+    }
+    if(acc.setDataRate(MMA8452::RATE_100)) {
+       LOG("Error setting data rate. Fail");
+       return false;
+    }
+    if(sampleMMA8452Gravities(&acc,1000)!=true) {
+       LOG("Sampling gravities failed");
+       return false;
+    }
 
     return true;
 }
@@ -506,19 +517,19 @@
 int main() {
     pc.baud(115200);
     LOG("Begin");
+    LOG("Executing twos complement test");
     if(!twosCompTest()) {
-       LOG("twos comp test failed");
+       LOG("Twos complement test failed");
        loop();
     }
-    LOG("twos comp test passed");
-    //loop();
-    for(int i=0; i<20; i++) {
-       LOG(" ");
-    }
+    LOG("Twos complement test passed");
+
+    LOG("Executing MMA8452 tests");
     if(!test()) {
-       LOG("FAIL.");
+       LOG("MMA8452 test failed.");
        loop();
     }
-    LOG("Tests passed");
+    LOG("MMA8452 test passed");
+    LOG("All tests passed");
     loop();
 }