A test utility for mma8452

Dependencies:   MMA8452 mbed

Committer:
nherriot
Date:
Wed Oct 16 16:26:39 2013 +0000
Revision:
5:34fa5db89a3a
Parent:
4:489573e65d47
Child:
6:94d7ca596f52
simplification on several methods

Who changed what in which revision?

UserRevisionLine numberNew contents of line
matsu 0:904e837fccc5 1 #include "mbed.h"
nherriot 2:0587772b03b0 2 //#include <bitset>
nherriot 2:0587772b03b0 3 #include "MMA8452.h"
nherriot 4:489573e65d47 4 #include "helperFunctions.h"
nherriot 2:0587772b03b0 5
nherriot 2:0587772b03b0 6 // I2C i2c(p28,p27);
nherriot 3:6d888ac31d2c 7 Accelerometer_MMA8452 accelerometer(p28, p27, 40000);
nherriot 3:6d888ac31d2c 8 //Accelerometer_MMA8452 acclerometer(p28, p29);
nherriot 2:0587772b03b0 9
matsu 0:904e837fccc5 10 Serial pc(USBTX, USBRX);
matsu 0:904e837fccc5 11 DigitalOut led1(LED1);
matsu 0:904e837fccc5 12 DigitalOut led2(LED2);
matsu 0:904e837fccc5 13 DigitalOut led3(LED3);
matsu 0:904e837fccc5 14
nherriot 4:489573e65d47 15
nherriot 4:489573e65d47 16
matsu 0:904e837fccc5 17 int main() {
matsu 0:904e837fccc5 18 led1 = 0;
matsu 0:904e837fccc5 19 led2 = 0;
matsu 0:904e837fccc5 20 led3 = 0;
nherriot 5:34fa5db89a3a 21
nherriot 4:489573e65d47 22 char x_buffer[2]; // used to store a bit array from the x axis acclerometer
nherriot 4:489573e65d47 23 char y_buffer[2]; // this represents the raw data value from the acclerometer
nherriot 4:489573e65d47 24 char z_buffer[2]; // which is converted from 12bit to 16bit value 2s compliment.
matsu 0:904e837fccc5 25
nherriot 5:34fa5db89a3a 26 x_buffer[0] = 0x00;
nherriot 5:34fa5db89a3a 27 x_buffer[1] = 0x00;
nherriot 5:34fa5db89a3a 28 y_buffer[0] = 0x00;
nherriot 5:34fa5db89a3a 29 y_buffer[1] = 0x00;
nherriot 5:34fa5db89a3a 30 z_buffer[0] = 0x00;
nherriot 5:34fa5db89a3a 31 z_buffer[0] = 0x00;
nherriot 5:34fa5db89a3a 32
nherriot 5:34fa5db89a3a 33 int deviceSystemMode(0);
nherriot 5:34fa5db89a3a 34
nherriot 2:0587772b03b0 35 // just playing with address values
nherriot 2:0587772b03b0 36 char mcu_address = 0x00;
nherriot 2:0587772b03b0 37 pc.printf("\nmcu_address is: 0x%x ", mcu_address);
nherriot 2:0587772b03b0 38 mcu_address = MMA8452_ADDRESS;
nherriot 2:0587772b03b0 39 pc.printf("\nmcu_address is: 0x%x ", mcu_address);
nherriot 3:6d888ac31d2c 40 mcu_address = (MMA8452_ADDRESS<< 1); // shifting address by 1 bit as i2c is a 7 bit encoding, and this is 8bit encoded
nherriot 2:0587772b03b0 41 pc.printf("\nmcu_address is now : 0x%x ", mcu_address);
nherriot 2:0587772b03b0 42
matsu 0:904e837fccc5 43 wait(0.5);
matsu 0:904e837fccc5 44 //init
nherriot 2:0587772b03b0 45 //set active mode
nherriot 3:6d888ac31d2c 46 pc.printf("\nWriting to master register\n");
nherriot 5:34fa5db89a3a 47
nherriot 5:34fa5db89a3a 48 int deviceStatus = 0;
nherriot 5:34fa5db89a3a 49 if (accelerometer.get_Status(deviceStatus)==0)
nherriot 5:34fa5db89a3a 50 {
nherriot 5:34fa5db89a3a 51 pc.printf("\nGot the device Status - device status which is: 0x%x \n", deviceStatus);
nherriot 5:34fa5db89a3a 52 }
nherriot 5:34fa5db89a3a 53 else
nherriot 5:34fa5db89a3a 54 {
nherriot 5:34fa5db89a3a 55 pc.printf("\nFailed to get the device status. The id is set as: 0x%x ", deviceStatus);
nherriot 5:34fa5db89a3a 56 }
nherriot 5:34fa5db89a3a 57
nherriot 4:489573e65d47 58 for (int i=0;i<=50;i++)
nherriot 4:489573e65d47 59 {
nherriot 5:34fa5db89a3a 60
nherriot 3:6d888ac31d2c 61 if (accelerometer.activate()==0)
nherriot 3:6d888ac31d2c 62 {
nherriot 3:6d888ac31d2c 63 led1 = 1;
nherriot 3:6d888ac31d2c 64 led2 = 1;
nherriot 3:6d888ac31d2c 65 led3 = 1;
nherriot 3:6d888ac31d2c 66 pc.printf("\nActivated chip\n");
nherriot 3:6d888ac31d2c 67 wait(0.5);
nherriot 4:489573e65d47 68
nherriot 5:34fa5db89a3a 69
nherriot 5:34fa5db89a3a 70 for(int i=0; i<10;i++)
nherriot 3:6d888ac31d2c 71 {
nherriot 4:489573e65d47 72
nherriot 5:34fa5db89a3a 73 /*
nherriot 4:489573e65d47 74 if (accelerometer.read_x_raw(x_buffer)==0)
nherriot 4:489573e65d47 75 {
nherriot 4:489573e65d47 76 //pc.printf("\nThe x-axis LSB is: 0x%x", x);
nherriot 4:489573e65d47 77 pc.printf("The value of my x_buffer[0] in digit form is: %d in hex: 0x%x \n",x_buffer[0],x_buffer[0]);
nherriot 4:489573e65d47 78 pc.printf("The value of my x_buffer[1] in digit form is: %d in hex: 0x%x \n",x_buffer[1],x_buffer[1]);
nherriot 4:489573e65d47 79 print2bytebinchar(x_buffer);
nherriot 4:489573e65d47 80
nherriot 4:489573e65d47 81 }
nherriot 4:489573e65d47 82 else
nherriot 4:489573e65d47 83 {
nherriot 4:489573e65d47 84 pc.printf("\nError - could not get xaxis LSB! ....\n");
nherriot 4:489573e65d47 85 }
nherriot 4:489573e65d47 86
nherriot 5:34fa5db89a3a 87 */
nherriot 4:489573e65d47 88
nherriot 3:6d888ac31d2c 89
nherriot 5:34fa5db89a3a 90 /*
nherriot 4:489573e65d47 91 if (accelerometer.read_y_raw(y_buffer)==0)
nherriot 4:489573e65d47 92 {
nherriot 4:489573e65d47 93 //pc.printf("\nThe x-axis LSB is: 0x%x", x);
nherriot 4:489573e65d47 94 pc.printf("The value of my y_buffer[0] in digit form is: %d in hex: 0x%x \n",y_buffer[0],y_buffer[0]);
nherriot 4:489573e65d47 95 pc.printf("The value of my y_buffer[1] in digit form is: %d in hex: 0x%x \n",y_buffer[1],y_buffer[1]);
nherriot 4:489573e65d47 96 print2bytebinchar(y_buffer);
nherriot 4:489573e65d47 97
nherriot 4:489573e65d47 98 }
nherriot 4:489573e65d47 99 else
nherriot 4:489573e65d47 100 {
nherriot 4:489573e65d47 101 pc.printf("\nError - could not get yaxis LSB! ....\n");
nherriot 4:489573e65d47 102 }
nherriot 4:489573e65d47 103
nherriot 5:34fa5db89a3a 104 */
nherriot 5:34fa5db89a3a 105
nherriot 4:489573e65d47 106
nherriot 4:489573e65d47 107
nherriot 4:489573e65d47 108 if (accelerometer.read_z_raw(z_buffer)==0)
nherriot 4:489573e65d47 109 {
nherriot 4:489573e65d47 110 //pc.printf("\nThe x-axis LSB is: 0x%x", x);
nherriot 4:489573e65d47 111 pc.printf("The value of my z_buffer[0] in digit form is: %d in hex: 0x%x \n",z_buffer[0],z_buffer[0]);
nherriot 4:489573e65d47 112 pc.printf("The value of my z_buffer[1] in digit form is: %d in hex: 0x%x \n",z_buffer[1],z_buffer[1]);
nherriot 5:34fa5db89a3a 113 print2bytebinchar(z_buffer);
nherriot 4:489573e65d47 114
nherriot 4:489573e65d47 115 }
nherriot 4:489573e65d47 116 else
nherriot 4:489573e65d47 117 {
nherriot 4:489573e65d47 118 pc.printf("\nError - could not get zaxis LSB! ....\n");
nherriot 4:489573e65d47 119 }
nherriot 4:489573e65d47 120
nherriot 4:489573e65d47 121
nherriot 4:489573e65d47 122
nherriot 5:34fa5db89a3a 123 wait(0.5);
nherriot 4:489573e65d47 124 }
nherriot 3:6d888ac31d2c 125 }
nherriot 3:6d888ac31d2c 126 else
nherriot 3:6d888ac31d2c 127 {
nherriot 3:6d888ac31d2c 128 pc.printf("Could not activate the chip\n");
nherriot 3:6d888ac31d2c 129 }
nherriot 4:489573e65d47 130
nherriot 4:489573e65d47 131
nherriot 4:489573e65d47 132 int deviceID= 0;
nherriot 4:489573e65d47 133 if (accelerometer.get_DeviceID(deviceID)==0)
nherriot 4:489573e65d47 134 {
nherriot 4:489573e65d47 135 pc.printf("\nGot the who am I - device id which is: 0x%x \n", deviceID);
nherriot 4:489573e65d47 136 }
nherriot 4:489573e65d47 137 else
nherriot 4:489573e65d47 138 {
nherriot 4:489573e65d47 139 pc.printf("\nFailed to get the device id. The id is set as: 0x%x ", deviceID);
nherriot 4:489573e65d47 140
nherriot 4:489573e65d47 141 }
nherriot 4:489573e65d47 142
nherriot 4:489573e65d47 143 if (accelerometer.get_Status(deviceStatus)==0)
nherriot 4:489573e65d47 144 {
nherriot 4:489573e65d47 145 pc.printf("\nGot the device Status - device system mode which is: 0x%x \n", deviceSystemMode);
nherriot 4:489573e65d47 146 }
nherriot 4:489573e65d47 147 else
nherriot 4:489573e65d47 148 {
nherriot 4:489573e65d47 149 pc.printf("\nFailed to get the device system mode. The id is set as: 0x%x ", deviceSystemMode);
nherriot 4:489573e65d47 150
nherriot 4:489573e65d47 151 }
nherriot 4:489573e65d47 152
nherriot 5:34fa5db89a3a 153 wait(2);
nherriot 4:489573e65d47 154
nherriot 4:489573e65d47 155 pc.printf("In loop %d, of repeated initialisation.\n",i);
nherriot 4:489573e65d47 156 i++;
nherriot 3:6d888ac31d2c 157 }
matsu 0:904e837fccc5 158
matsu 0:904e837fccc5 159 //get analog data
nherriot 4:489573e65d47 160 pc.printf("Set system into standby mode.........");
nherriot 4:489573e65d47 161
nherriot 4:489573e65d47 162 if(accelerometer.standby()==0)
nherriot 4:489573e65d47 163 {
nherriot 4:489573e65d47 164 pc.printf("Device set into standby...\n");
nherriot 4:489573e65d47 165 }
nherriot 4:489573e65d47 166 else
nherriot 4:489573e65d47 167 {
nherriot 4:489573e65d47 168 pc.printf("Device failed to set into standby...\n");
nherriot 4:489573e65d47 169 }
nherriot 5:34fa5db89a3a 170
nherriot 4:489573e65d47 171 pc.printf("\nGet system mode, it should now be standby! ...\n");
nherriot 4:489573e65d47 172 if (accelerometer.get_SystemMode(deviceSystemMode)==0)
nherriot 4:489573e65d47 173 {
nherriot 4:489573e65d47 174 pc.printf("\nGot the device System Mode - device system mode which is: 0x%x \n", deviceSystemMode);
nherriot 4:489573e65d47 175 }
nherriot 4:489573e65d47 176 else
nherriot 4:489573e65d47 177 {
nherriot 4:489573e65d47 178 pc.printf("\nFailed to get the device system mode. The id is set as: 0x%x ", deviceSystemMode);
nherriot 4:489573e65d47 179
nherriot 4:489573e65d47 180 }
nherriot 4:489573e65d47 181
nherriot 5:34fa5db89a3a 182 }