Nicholas Herriot
/
MMA8452_test
A test utility for mma8452
main.cpp@7:2fa57cf76768, 2013-10-17 (annotated)
- Committer:
- nherriot
- Date:
- Thu Oct 17 09:40:52 2013 +0000
- Revision:
- 7:2fa57cf76768
- Parent:
- 6:94d7ca596f52
commit bug fix for multiple start bit on IIC bus.
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 | |
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 | 6:94d7ca596f52 | 57 | |
nherriot | 6:94d7ca596f52 | 58 | |
nherriot | 6:94d7ca596f52 | 59 | int ControlRegister(0); |
nherriot | 6:94d7ca596f52 | 60 | if (accelerometer.get_CTRL_Reg1(ControlRegister)==0) |
nherriot | 6:94d7ca596f52 | 61 | { |
nherriot | 6:94d7ca596f52 | 62 | pc.printf("\nGot contents of Control Register1 which is: 0x%x \n", ControlRegister); |
nherriot | 6:94d7ca596f52 | 63 | |
nherriot | 6:94d7ca596f52 | 64 | |
nherriot | 6:94d7ca596f52 | 65 | } |
nherriot | 6:94d7ca596f52 | 66 | else |
nherriot | 6:94d7ca596f52 | 67 | { |
nherriot | 6:94d7ca596f52 | 68 | pc.printf("\nFailed to get the device status. The id is set as: 0x%x ", deviceStatus); |
nherriot | 6:94d7ca596f52 | 69 | } |
nherriot | 6:94d7ca596f52 | 70 | |
nherriot | 6:94d7ca596f52 | 71 | |
nherriot | 6:94d7ca596f52 | 72 | wait(5); |
nherriot | 5:34fa5db89a3a | 73 | |
nherriot | 4:489573e65d47 | 74 | for (int i=0;i<=50;i++) |
nherriot | 4:489573e65d47 | 75 | { |
nherriot | 5:34fa5db89a3a | 76 | |
nherriot | 3:6d888ac31d2c | 77 | if (accelerometer.activate()==0) |
nherriot | 3:6d888ac31d2c | 78 | { |
nherriot | 3:6d888ac31d2c | 79 | led1 = 1; |
nherriot | 3:6d888ac31d2c | 80 | led2 = 1; |
nherriot | 3:6d888ac31d2c | 81 | led3 = 1; |
nherriot | 3:6d888ac31d2c | 82 | pc.printf("\nActivated chip\n"); |
nherriot | 3:6d888ac31d2c | 83 | wait(0.5); |
nherriot | 4:489573e65d47 | 84 | |
nherriot | 5:34fa5db89a3a | 85 | |
nherriot | 5:34fa5db89a3a | 86 | for(int i=0; i<10;i++) |
nherriot | 3:6d888ac31d2c | 87 | { |
nherriot | 4:489573e65d47 | 88 | |
nherriot | 5:34fa5db89a3a | 89 | /* |
nherriot | 4:489573e65d47 | 90 | if (accelerometer.read_x_raw(x_buffer)==0) |
nherriot | 4:489573e65d47 | 91 | { |
nherriot | 4:489573e65d47 | 92 | //pc.printf("\nThe x-axis LSB is: 0x%x", x); |
nherriot | 4:489573e65d47 | 93 | 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 | 94 | 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 | 95 | print2bytebinchar(x_buffer); |
nherriot | 4:489573e65d47 | 96 | |
nherriot | 4:489573e65d47 | 97 | } |
nherriot | 4:489573e65d47 | 98 | else |
nherriot | 4:489573e65d47 | 99 | { |
nherriot | 4:489573e65d47 | 100 | pc.printf("\nError - could not get xaxis LSB! ....\n"); |
nherriot | 4:489573e65d47 | 101 | } |
nherriot | 4:489573e65d47 | 102 | |
nherriot | 5:34fa5db89a3a | 103 | */ |
nherriot | 4:489573e65d47 | 104 | |
nherriot | 3:6d888ac31d2c | 105 | |
nherriot | 5:34fa5db89a3a | 106 | /* |
nherriot | 4:489573e65d47 | 107 | if (accelerometer.read_y_raw(y_buffer)==0) |
nherriot | 4:489573e65d47 | 108 | { |
nherriot | 4:489573e65d47 | 109 | //pc.printf("\nThe x-axis LSB is: 0x%x", x); |
nherriot | 4:489573e65d47 | 110 | 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 | 111 | 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 | 112 | print2bytebinchar(y_buffer); |
nherriot | 4:489573e65d47 | 113 | |
nherriot | 4:489573e65d47 | 114 | } |
nherriot | 4:489573e65d47 | 115 | else |
nherriot | 4:489573e65d47 | 116 | { |
nherriot | 4:489573e65d47 | 117 | pc.printf("\nError - could not get yaxis LSB! ....\n"); |
nherriot | 4:489573e65d47 | 118 | } |
nherriot | 4:489573e65d47 | 119 | |
nherriot | 5:34fa5db89a3a | 120 | */ |
nherriot | 5:34fa5db89a3a | 121 | |
nherriot | 4:489573e65d47 | 122 | |
nherriot | 4:489573e65d47 | 123 | |
nherriot | 4:489573e65d47 | 124 | if (accelerometer.read_z_raw(z_buffer)==0) |
nherriot | 4:489573e65d47 | 125 | { |
nherriot | 4:489573e65d47 | 126 | //pc.printf("\nThe x-axis LSB is: 0x%x", x); |
nherriot | 4:489573e65d47 | 127 | 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 | 128 | 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 | 129 | print2bytebinchar(z_buffer); |
nherriot | 4:489573e65d47 | 130 | |
nherriot | 4:489573e65d47 | 131 | } |
nherriot | 4:489573e65d47 | 132 | else |
nherriot | 4:489573e65d47 | 133 | { |
nherriot | 4:489573e65d47 | 134 | pc.printf("\nError - could not get zaxis LSB! ....\n"); |
nherriot | 4:489573e65d47 | 135 | } |
nherriot | 4:489573e65d47 | 136 | |
nherriot | 4:489573e65d47 | 137 | |
nherriot | 4:489573e65d47 | 138 | |
nherriot | 5:34fa5db89a3a | 139 | wait(0.5); |
nherriot | 4:489573e65d47 | 140 | } |
nherriot | 3:6d888ac31d2c | 141 | } |
nherriot | 3:6d888ac31d2c | 142 | else |
nherriot | 3:6d888ac31d2c | 143 | { |
nherriot | 3:6d888ac31d2c | 144 | pc.printf("Could not activate the chip\n"); |
nherriot | 3:6d888ac31d2c | 145 | } |
nherriot | 4:489573e65d47 | 146 | |
nherriot | 4:489573e65d47 | 147 | |
nherriot | 4:489573e65d47 | 148 | int deviceID= 0; |
nherriot | 4:489573e65d47 | 149 | if (accelerometer.get_DeviceID(deviceID)==0) |
nherriot | 4:489573e65d47 | 150 | { |
nherriot | 4:489573e65d47 | 151 | pc.printf("\nGot the who am I - device id which is: 0x%x \n", deviceID); |
nherriot | 4:489573e65d47 | 152 | } |
nherriot | 4:489573e65d47 | 153 | else |
nherriot | 4:489573e65d47 | 154 | { |
nherriot | 4:489573e65d47 | 155 | pc.printf("\nFailed to get the device id. The id is set as: 0x%x ", deviceID); |
nherriot | 4:489573e65d47 | 156 | |
nherriot | 4:489573e65d47 | 157 | } |
nherriot | 4:489573e65d47 | 158 | |
nherriot | 4:489573e65d47 | 159 | if (accelerometer.get_Status(deviceStatus)==0) |
nherriot | 4:489573e65d47 | 160 | { |
nherriot | 4:489573e65d47 | 161 | pc.printf("\nGot the device Status - device system mode which is: 0x%x \n", deviceSystemMode); |
nherriot | 4:489573e65d47 | 162 | } |
nherriot | 4:489573e65d47 | 163 | else |
nherriot | 4:489573e65d47 | 164 | { |
nherriot | 4:489573e65d47 | 165 | pc.printf("\nFailed to get the device system mode. The id is set as: 0x%x ", deviceSystemMode); |
nherriot | 4:489573e65d47 | 166 | |
nherriot | 4:489573e65d47 | 167 | } |
nherriot | 4:489573e65d47 | 168 | |
nherriot | 5:34fa5db89a3a | 169 | wait(2); |
nherriot | 4:489573e65d47 | 170 | |
nherriot | 4:489573e65d47 | 171 | pc.printf("In loop %d, of repeated initialisation.\n",i); |
nherriot | 4:489573e65d47 | 172 | i++; |
nherriot | 3:6d888ac31d2c | 173 | } |
matsu | 0:904e837fccc5 | 174 | |
matsu | 0:904e837fccc5 | 175 | //get analog data |
nherriot | 4:489573e65d47 | 176 | pc.printf("Set system into standby mode........."); |
nherriot | 4:489573e65d47 | 177 | |
nherriot | 4:489573e65d47 | 178 | if(accelerometer.standby()==0) |
nherriot | 4:489573e65d47 | 179 | { |
nherriot | 4:489573e65d47 | 180 | pc.printf("Device set into standby...\n"); |
nherriot | 4:489573e65d47 | 181 | } |
nherriot | 4:489573e65d47 | 182 | else |
nherriot | 4:489573e65d47 | 183 | { |
nherriot | 4:489573e65d47 | 184 | pc.printf("Device failed to set into standby...\n"); |
nherriot | 4:489573e65d47 | 185 | } |
nherriot | 5:34fa5db89a3a | 186 | |
nherriot | 4:489573e65d47 | 187 | pc.printf("\nGet system mode, it should now be standby! ...\n"); |
nherriot | 4:489573e65d47 | 188 | if (accelerometer.get_SystemMode(deviceSystemMode)==0) |
nherriot | 4:489573e65d47 | 189 | { |
nherriot | 4:489573e65d47 | 190 | pc.printf("\nGot the device System Mode - device system mode which is: 0x%x \n", deviceSystemMode); |
nherriot | 4:489573e65d47 | 191 | } |
nherriot | 4:489573e65d47 | 192 | else |
nherriot | 4:489573e65d47 | 193 | { |
nherriot | 4:489573e65d47 | 194 | pc.printf("\nFailed to get the device system mode. The id is set as: 0x%x ", deviceSystemMode); |
nherriot | 4:489573e65d47 | 195 | |
nherriot | 4:489573e65d47 | 196 | } |
nherriot | 4:489573e65d47 | 197 | |
nherriot | 5:34fa5db89a3a | 198 | } |