A test utility for mma8452

Dependencies:   MMA8452 mbed

Revision:
2:0587772b03b0
Parent:
1:0c1dd02be97a
Child:
3:6d888ac31d2c
--- a/main.cpp	Wed Nov 28 17:05:31 2012 +0000
+++ b/main.cpp	Fri Oct 04 14:48:20 2013 +0000
@@ -1,6 +1,11 @@
 #include "mbed.h"
+//#include <bitset>
+#include "MMA8452.h"
 
-I2C i2c(p9,p10);
+
+// I2C i2c(p28,p27);
+Accelerometer_MMA8452 acclerometer(p28, p29, 40000);
+
 Serial pc(USBTX, USBRX);
 DigitalOut led1(LED1);
 DigitalOut led2(LED2);
@@ -13,42 +18,58 @@
     char cmd[6];
     char add[1];
     char init[2];
-    add[0] = 0x01;
-    init[0] = 0x2A;
-    init[1] = 0x01;
+    add[0] = 0x01;                  // x-axis register
+    init[0] = 0x2A;                 // control register 1
+    init[1] = 0x01;                 // set to active
     int number=0;
-    i2c.frequency(40000);
+    //i2c.frequency(40000);
 
+    // just playing with address values
+    char mcu_address = 0x00;
+    pc.printf("\nmcu_address is: 0x%x ", mcu_address);
+    mcu_address = MMA8452_ADDRESS;
+    pc.printf("\nmcu_address is: 0x%x ", mcu_address);
+    mcu_address = (mcu_address << 1);           // shifting address by 1 bit as i2c is a 7 bit encoding, and this is 8bit encoded
+    pc.printf("\nmcu_address is now : 0x%x ", mcu_address);
+    
     wait(0.5);
     //init
-    //set active mode    
-    while(i2c.write(0x3A,init,2));
+    //set active mode
+    pc.printf("\nWriting to master register register\n"); 
+    // while(i2c.write(mcu_address,init,2));
     led1 = 1;
     led2 = 1;
     led3 = 1;
     
     //get analog data
-    while(1) {
+    pc.printf("Getting analog data");
+    /*
+    while(1) 
+    {
         //send procedure
         wait(0.07);
         switch(number){
         case 0:
-            if(i2c.write(0x3A,add,1) == 0){
+            if(i2c.write(mcu_address,add,1) == 0){
                 number++;
+                pc.printf("In case 0 loop. The number is: %d \n", number);
                 led1 = 1;
             }else{
                 number=0;
+                pc.printf("In case 0 loop. The number is reset to: %d \n", number);
                 led1 = 0;
                 led2 = 0;
                 led3 = 1;
             }
             break;
         case 1:
-            if(i2c.read(0x3A,cmd,6) == 0){
+            if(i2c.read(mcu_address,cmd,6) == 0){
                 number++;
+                pc.printf("In case 1 loop. The number is: %d \n", number);
                 led2 = 1;
             }else{
                 number=0;
+                pc.printf("In case 1 loop. The number is reset to: %d \n", number);
                 led1 = 0;
                 led2 = 0;
                 led3 = 0;
@@ -56,7 +77,7 @@
             break;    
         case 2:
             //get analog data
-            pc.printf("\r X = %d,Y = %d,Z = %d                ",cmd[1],cmd[3],cmd[5]);
+            pc.printf("\r X = %d,Y = %d,Z = %d                \n",cmd[1],cmd[3],cmd[5]);
             led3 = 1;
             wait(0.5);
             led1 = 0;
@@ -65,5 +86,6 @@
             number=0;
             break;
         }
-    }
+    }*/
+    
 }