Nicholas Herriot
/
MMA8452_test
A test utility for mma8452
main.cpp@4:489573e65d47, 2013-10-16 (annotated)
- Committer:
- nherriot
- Date:
- Wed Oct 16 14:12:28 2013 +0000
- Revision:
- 4:489573e65d47
- Parent:
- 3:6d888ac31d2c
- Child:
- 5:34fa5db89a3a
A test utility for the mma8452 accelerometer.
Who changed what in which revision?
User | Revision | Line number | New 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 | |
nherriot | 4:489573e65d47 | 17 | |
nherriot | 4:489573e65d47 | 18 | |
nherriot | 4:489573e65d47 | 19 | |
nherriot | 4:489573e65d47 | 20 | |
nherriot | 4:489573e65d47 | 21 | |
nherriot | 4:489573e65d47 | 22 | |
matsu | 0:904e837fccc5 | 23 | int main() { |
matsu | 0:904e837fccc5 | 24 | led1 = 0; |
matsu | 0:904e837fccc5 | 25 | led2 = 0; |
matsu | 0:904e837fccc5 | 26 | led3 = 0; |
matsu | 0:904e837fccc5 | 27 | char cmd[6]; |
matsu | 0:904e837fccc5 | 28 | char add[1]; |
matsu | 0:904e837fccc5 | 29 | char init[2]; |
nherriot | 4:489573e65d47 | 30 | add[0] = OUT_X_MSB; // x-axis register |
nherriot | 4:489573e65d47 | 31 | init[0] = CTRL_REG_1; // control register 1 |
nherriot | 4:489573e65d47 | 32 | init[1] = 0x01; // sets the accelerometer to active |
matsu | 0:904e837fccc5 | 33 | int number=0; |
nherriot | 4:489573e65d47 | 34 | char x_buffer[2]; // used to store a bit array from the x axis acclerometer |
nherriot | 4:489573e65d47 | 35 | char y_buffer[2]; // this represents the raw data value from the acclerometer |
nherriot | 4:489573e65d47 | 36 | char z_buffer[2]; // which is converted from 12bit to 16bit value 2s compliment. |
matsu | 0:904e837fccc5 | 37 | |
nherriot | 2:0587772b03b0 | 38 | // just playing with address values |
nherriot | 2:0587772b03b0 | 39 | char mcu_address = 0x00; |
nherriot | 2:0587772b03b0 | 40 | pc.printf("\nmcu_address is: 0x%x ", mcu_address); |
nherriot | 2:0587772b03b0 | 41 | mcu_address = MMA8452_ADDRESS; |
nherriot | 2:0587772b03b0 | 42 | pc.printf("\nmcu_address is: 0x%x ", mcu_address); |
nherriot | 3:6d888ac31d2c | 43 | 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 | 44 | pc.printf("\nmcu_address is now : 0x%x ", mcu_address); |
nherriot | 2:0587772b03b0 | 45 | |
matsu | 0:904e837fccc5 | 46 | wait(0.5); |
matsu | 0:904e837fccc5 | 47 | //init |
nherriot | 2:0587772b03b0 | 48 | //set active mode |
nherriot | 3:6d888ac31d2c | 49 | pc.printf("\nWriting to master register\n"); |
nherriot | 4:489573e65d47 | 50 | for (int i=0;i<=50;i++) |
nherriot | 4:489573e65d47 | 51 | { |
nherriot | 4:489573e65d47 | 52 | int deviceSystemMode= 0; |
nherriot | 4:489573e65d47 | 53 | if (accelerometer.get_SystemMode(deviceSystemMode)==0) |
nherriot | 4:489573e65d47 | 54 | { |
nherriot | 4:489573e65d47 | 55 | pc.printf("\nGot the device System Mode - device system mode which is: 0x%x \n", deviceSystemMode); |
nherriot | 4:489573e65d47 | 56 | } |
nherriot | 4:489573e65d47 | 57 | else |
nherriot | 3:6d888ac31d2c | 58 | { |
nherriot | 4:489573e65d47 | 59 | pc.printf("\nFailed to get the device system mode. The id is set as: 0x%x ", deviceSystemMode); |
nherriot | 4:489573e65d47 | 60 | |
nherriot | 4:489573e65d47 | 61 | } |
nherriot | 4:489573e65d47 | 62 | |
nherriot | 4:489573e65d47 | 63 | int deviceStatus = 0; |
nherriot | 4:489573e65d47 | 64 | if (accelerometer.get_Status(deviceStatus)==0) |
nherriot | 4:489573e65d47 | 65 | { |
nherriot | 4:489573e65d47 | 66 | pc.printf("\nGot the device Status - device status which is: 0x%x \n", deviceStatus); |
nherriot | 4:489573e65d47 | 67 | } |
nherriot | 4:489573e65d47 | 68 | else |
nherriot | 4:489573e65d47 | 69 | { |
nherriot | 4:489573e65d47 | 70 | pc.printf("\nFailed to get the device status. The id is set as: 0x%x ", deviceStatus); |
nherriot | 4:489573e65d47 | 71 | |
nherriot | 4:489573e65d47 | 72 | } |
nherriot | 4:489573e65d47 | 73 | |
nherriot | 4:489573e65d47 | 74 | |
nherriot | 4:489573e65d47 | 75 | |
nherriot | 3:6d888ac31d2c | 76 | if (accelerometer.activate()==0) |
nherriot | 3:6d888ac31d2c | 77 | { |
nherriot | 3:6d888ac31d2c | 78 | led1 = 1; |
nherriot | 3:6d888ac31d2c | 79 | led2 = 1; |
nherriot | 3:6d888ac31d2c | 80 | led3 = 1; |
nherriot | 3:6d888ac31d2c | 81 | pc.printf("\nActivated chip\n"); |
nherriot | 3:6d888ac31d2c | 82 | wait(0.5); |
nherriot | 4:489573e65d47 | 83 | |
nherriot | 4:489573e65d47 | 84 | //int x=0;//int y=0;int z=0; |
nherriot | 4:489573e65d47 | 85 | for(int i=0; i<5;i++) |
nherriot | 3:6d888ac31d2c | 86 | { |
nherriot | 4:489573e65d47 | 87 | |
nherriot | 4:489573e65d47 | 88 | |
nherriot | 4:489573e65d47 | 89 | if (accelerometer.read_x_raw(x_buffer)==0) |
nherriot | 4:489573e65d47 | 90 | { |
nherriot | 4:489573e65d47 | 91 | //pc.printf("\nThe x-axis LSB is: 0x%x", x); |
nherriot | 4:489573e65d47 | 92 | 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 | 93 | 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 | 94 | print2bytebinchar(x_buffer); |
nherriot | 4:489573e65d47 | 95 | |
nherriot | 4:489573e65d47 | 96 | } |
nherriot | 4:489573e65d47 | 97 | else |
nherriot | 4:489573e65d47 | 98 | { |
nherriot | 4:489573e65d47 | 99 | pc.printf("\nError - could not get xaxis LSB! ....\n"); |
nherriot | 4:489573e65d47 | 100 | } |
nherriot | 4:489573e65d47 | 101 | |
nherriot | 4:489573e65d47 | 102 | |
nherriot | 3:6d888ac31d2c | 103 | |
nherriot | 4:489573e65d47 | 104 | if (accelerometer.read_y_raw(y_buffer)==0) |
nherriot | 4:489573e65d47 | 105 | { |
nherriot | 4:489573e65d47 | 106 | //pc.printf("\nThe x-axis LSB is: 0x%x", x); |
nherriot | 4:489573e65d47 | 107 | 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 | 108 | 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 | 109 | print2bytebinchar(y_buffer); |
nherriot | 4:489573e65d47 | 110 | |
nherriot | 4:489573e65d47 | 111 | } |
nherriot | 4:489573e65d47 | 112 | else |
nherriot | 4:489573e65d47 | 113 | { |
nherriot | 4:489573e65d47 | 114 | pc.printf("\nError - could not get yaxis LSB! ....\n"); |
nherriot | 4:489573e65d47 | 115 | } |
nherriot | 4:489573e65d47 | 116 | |
nherriot | 4:489573e65d47 | 117 | |
nherriot | 4:489573e65d47 | 118 | |
nherriot | 4:489573e65d47 | 119 | if (accelerometer.read_z_raw(z_buffer)==0) |
nherriot | 4:489573e65d47 | 120 | { |
nherriot | 4:489573e65d47 | 121 | //pc.printf("\nThe x-axis LSB is: 0x%x", x); |
nherriot | 4:489573e65d47 | 122 | 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 | 123 | 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 | 4:489573e65d47 | 124 | print2bytebinchar(y_buffer); |
nherriot | 4:489573e65d47 | 125 | |
nherriot | 4:489573e65d47 | 126 | } |
nherriot | 4:489573e65d47 | 127 | else |
nherriot | 4:489573e65d47 | 128 | { |
nherriot | 4:489573e65d47 | 129 | pc.printf("\nError - could not get zaxis LSB! ....\n"); |
nherriot | 4:489573e65d47 | 130 | } |
nherriot | 4:489573e65d47 | 131 | |
nherriot | 4:489573e65d47 | 132 | |
nherriot | 4:489573e65d47 | 133 | |
nherriot | 4:489573e65d47 | 134 | //y = accelerometer.read_y(); |
nherriot | 4:489573e65d47 | 135 | |
nherriot | 4:489573e65d47 | 136 | //pc.printf("\nAxis acceleration in x is: %i , y is: %i, z is %z \n", x,y,z); |
nherriot | 4:489573e65d47 | 137 | wait(1); |
nherriot | 4:489573e65d47 | 138 | } |
nherriot | 3:6d888ac31d2c | 139 | } |
nherriot | 3:6d888ac31d2c | 140 | else |
nherriot | 3:6d888ac31d2c | 141 | { |
nherriot | 3:6d888ac31d2c | 142 | pc.printf("Could not activate the chip\n"); |
nherriot | 3:6d888ac31d2c | 143 | } |
nherriot | 4:489573e65d47 | 144 | |
nherriot | 4:489573e65d47 | 145 | |
nherriot | 4:489573e65d47 | 146 | int deviceID= 0; |
nherriot | 4:489573e65d47 | 147 | if (accelerometer.get_DeviceID(deviceID)==0) |
nherriot | 4:489573e65d47 | 148 | { |
nherriot | 4:489573e65d47 | 149 | pc.printf("\nGot the who am I - device id which is: 0x%x \n", deviceID); |
nherriot | 4:489573e65d47 | 150 | } |
nherriot | 4:489573e65d47 | 151 | else |
nherriot | 4:489573e65d47 | 152 | { |
nherriot | 4:489573e65d47 | 153 | pc.printf("\nFailed to get the device id. The id is set as: 0x%x ", deviceID); |
nherriot | 4:489573e65d47 | 154 | |
nherriot | 4:489573e65d47 | 155 | } |
nherriot | 4:489573e65d47 | 156 | |
nherriot | 4:489573e65d47 | 157 | if (accelerometer.get_Status(deviceStatus)==0) |
nherriot | 4:489573e65d47 | 158 | { |
nherriot | 4:489573e65d47 | 159 | pc.printf("\nGot the device Status - device system mode which is: 0x%x \n", deviceSystemMode); |
nherriot | 4:489573e65d47 | 160 | } |
nherriot | 4:489573e65d47 | 161 | else |
nherriot | 4:489573e65d47 | 162 | { |
nherriot | 4:489573e65d47 | 163 | pc.printf("\nFailed to get the device system mode. The id is set as: 0x%x ", deviceSystemMode); |
nherriot | 4:489573e65d47 | 164 | |
nherriot | 4:489573e65d47 | 165 | } |
nherriot | 4:489573e65d47 | 166 | |
nherriot | 4:489573e65d47 | 167 | wait(3); |
nherriot | 4:489573e65d47 | 168 | |
nherriot | 4:489573e65d47 | 169 | pc.printf("In loop %d, of repeated initialisation.\n",i); |
nherriot | 4:489573e65d47 | 170 | i++; |
nherriot | 3:6d888ac31d2c | 171 | } |
matsu | 0:904e837fccc5 | 172 | |
matsu | 0:904e837fccc5 | 173 | //get analog data |
nherriot | 4:489573e65d47 | 174 | pc.printf("Set system into standby mode........."); |
nherriot | 4:489573e65d47 | 175 | |
nherriot | 4:489573e65d47 | 176 | if(accelerometer.standby()==0) |
nherriot | 4:489573e65d47 | 177 | { |
nherriot | 4:489573e65d47 | 178 | pc.printf("Device set into standby...\n"); |
nherriot | 4:489573e65d47 | 179 | } |
nherriot | 4:489573e65d47 | 180 | else |
nherriot | 4:489573e65d47 | 181 | { |
nherriot | 4:489573e65d47 | 182 | pc.printf("Device failed to set into standby...\n"); |
nherriot | 4:489573e65d47 | 183 | } |
nherriot | 4:489573e65d47 | 184 | int deviceSystemMode = 0; |
nherriot | 4:489573e65d47 | 185 | pc.printf("\nGet system mode, it should now be standby! ...\n"); |
nherriot | 4:489573e65d47 | 186 | if (accelerometer.get_SystemMode(deviceSystemMode)==0) |
nherriot | 4:489573e65d47 | 187 | { |
nherriot | 4:489573e65d47 | 188 | pc.printf("\nGot the device System Mode - device system mode which is: 0x%x \n", deviceSystemMode); |
nherriot | 4:489573e65d47 | 189 | } |
nherriot | 4:489573e65d47 | 190 | else |
nherriot | 4:489573e65d47 | 191 | { |
nherriot | 4:489573e65d47 | 192 | pc.printf("\nFailed to get the device system mode. The id is set as: 0x%x ", deviceSystemMode); |
nherriot | 4:489573e65d47 | 193 | |
nherriot | 4:489573e65d47 | 194 | } |
nherriot | 4:489573e65d47 | 195 | |
nherriot | 4:489573e65d47 | 196 | |
nherriot | 4:489573e65d47 | 197 | |
nherriot | 4:489573e65d47 | 198 | |
nherriot | 4:489573e65d47 | 199 | |
nherriot | 4:489573e65d47 | 200 | |
nherriot | 4:489573e65d47 | 201 | |
nherriot | 4:489573e65d47 | 202 | |
nherriot | 4:489573e65d47 | 203 | |
nherriot | 4:489573e65d47 | 204 | |
nherriot | 4:489573e65d47 | 205 | |
nherriot | 4:489573e65d47 | 206 | |
nherriot | 4:489573e65d47 | 207 | |
nherriot | 4:489573e65d47 | 208 | |
nherriot | 2:0587772b03b0 | 209 | /* |
nherriot | 2:0587772b03b0 | 210 | while(1) |
nherriot | 2:0587772b03b0 | 211 | { |
matsu | 0:904e837fccc5 | 212 | //send procedure |
matsu | 0:904e837fccc5 | 213 | wait(0.07); |
matsu | 0:904e837fccc5 | 214 | switch(number){ |
matsu | 0:904e837fccc5 | 215 | case 0: |
nherriot | 2:0587772b03b0 | 216 | if(i2c.write(mcu_address,add,1) == 0){ |
matsu | 0:904e837fccc5 | 217 | number++; |
nherriot | 2:0587772b03b0 | 218 | pc.printf("In case 0 loop. The number is: %d \n", number); |
matsu | 0:904e837fccc5 | 219 | led1 = 1; |
matsu | 0:904e837fccc5 | 220 | }else{ |
matsu | 0:904e837fccc5 | 221 | number=0; |
nherriot | 2:0587772b03b0 | 222 | pc.printf("In case 0 loop. The number is reset to: %d \n", number); |
matsu | 0:904e837fccc5 | 223 | led1 = 0; |
matsu | 0:904e837fccc5 | 224 | led2 = 0; |
matsu | 0:904e837fccc5 | 225 | led3 = 1; |
matsu | 0:904e837fccc5 | 226 | } |
matsu | 0:904e837fccc5 | 227 | break; |
matsu | 0:904e837fccc5 | 228 | case 1: |
nherriot | 2:0587772b03b0 | 229 | if(i2c.read(mcu_address,cmd,6) == 0){ |
matsu | 0:904e837fccc5 | 230 | number++; |
nherriot | 2:0587772b03b0 | 231 | pc.printf("In case 1 loop. The number is: %d \n", number); |
matsu | 0:904e837fccc5 | 232 | led2 = 1; |
matsu | 0:904e837fccc5 | 233 | }else{ |
matsu | 0:904e837fccc5 | 234 | number=0; |
nherriot | 2:0587772b03b0 | 235 | pc.printf("In case 1 loop. The number is reset to: %d \n", number); |
matsu | 0:904e837fccc5 | 236 | led1 = 0; |
matsu | 0:904e837fccc5 | 237 | led2 = 0; |
matsu | 0:904e837fccc5 | 238 | led3 = 0; |
matsu | 0:904e837fccc5 | 239 | } |
matsu | 0:904e837fccc5 | 240 | break; |
matsu | 0:904e837fccc5 | 241 | case 2: |
matsu | 1:0c1dd02be97a | 242 | //get analog data |
nherriot | 2:0587772b03b0 | 243 | pc.printf("\r X = %d,Y = %d,Z = %d \n",cmd[1],cmd[3],cmd[5]); |
matsu | 0:904e837fccc5 | 244 | led3 = 1; |
matsu | 0:904e837fccc5 | 245 | wait(0.5); |
matsu | 0:904e837fccc5 | 246 | led1 = 0; |
matsu | 0:904e837fccc5 | 247 | led2 = 0; |
matsu | 0:904e837fccc5 | 248 | led3 = 0; |
matsu | 0:904e837fccc5 | 249 | number=0; |
matsu | 0:904e837fccc5 | 250 | break; |
matsu | 0:904e837fccc5 | 251 | } |
nherriot | 2:0587772b03b0 | 252 | }*/ |
nherriot | 2:0587772b03b0 | 253 | |
matsu | 0:904e837fccc5 | 254 | } |