A test utility for mma8452

Dependencies:   MMA8452 mbed

Revision:
3:6d888ac31d2c
Parent:
2:0587772b03b0
Child:
4:489573e65d47
--- a/main.cpp	Fri Oct 04 14:48:20 2013 +0000
+++ b/main.cpp	Tue Oct 08 15:25:46 2013 +0000
@@ -4,7 +4,8 @@
 
 
 // I2C i2c(p28,p27);
-Accelerometer_MMA8452 acclerometer(p28, p29, 40000);
+Accelerometer_MMA8452 accelerometer(p28, p27, 40000);
+//Accelerometer_MMA8452 acclerometer(p28, p29);
 
 Serial pc(USBTX, USBRX);
 DigitalOut led1(LED1);
@@ -18,8 +19,8 @@
     char cmd[6];
     char add[1];
     char init[2];
-    add[0] = 0x01;                  // x-axis register
-    init[0] = 0x2A;                 // control register 1
+    add[0] = OUT_X_MSB;                  // x-axis register
+    init[0] = CTRL_REG_1;                 // control register 1
     init[1] = 0x01;                 // set to active
     int number=0;
     //i2c.frequency(40000);
@@ -29,17 +30,41 @@
     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
+    mcu_address = (MMA8452_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
-    pc.printf("\nWriting to master register register\n"); 
+    pc.printf("\nWriting to master register\n"); 
     // while(i2c.write(mcu_address,init,2));
-    led1 = 1;
-    led2 = 1;
-    led3 = 1;
+    for (int i=0;i<=400;i++)
+        {
+        if (accelerometer.activate()==0)
+        {
+            led1 = 1;
+            led2 = 1;
+            led3 = 1;
+            pc.printf("\nActivated chip\n"); 
+            wait(0.5);
+            /*int x=0;int y=0;int z=0;
+            for(int i=0; i<20;i++)
+            {
+                z = accelerometer.read_z();
+                x = accelerometer.read_x();
+                y = accelerometer.read_y();
+                
+                pc.printf("\nAxis acceleration in x is: %i  , y is: %i, z is %z \n", x,y,z);
+                wait(3);
+            }
+            */       
+        }
+        else
+        {
+            pc.printf("Could not activate the chip\n");
+        }
+        pc.printf("In loop %d, of repeated initialisation.\n",i); i++;
+     }   
     
     //get analog data
     pc.printf("Getting analog data");