
A test utility for mma8452
Revision 4:489573e65d47, committed 2013-10-16
- Comitter:
- nherriot
- Date:
- Wed Oct 16 14:12:28 2013 +0000
- Parent:
- 3:6d888ac31d2c
- Child:
- 5:34fa5db89a3a
- Commit message:
- A test utility for the mma8452 accelerometer.
Changed in this revision
--- a/MMA8452.lib Tue Oct 08 15:25:46 2013 +0000 +++ b/MMA8452.lib Wed Oct 16 14:12:28 2013 +0000 @@ -1,1 +1,1 @@ -MMA8452#ef026bf28798 +http://mbed.org/users/nherriot/code/MMA8452/#ffb0b1650ca2
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/helperFunctions.cpp Wed Oct 16 14:12:28 2013 +0000 @@ -0,0 +1,24 @@ + +#include "helperFunctions.h" + +//Serial pc(USBTX, USBRX); + +// This function will print a 7 bit char as a binary number on the screen +void printbincharpad(char c) +{ + for (int i = 7; i >= 0; --i) + { + putchar( (c & (1 << i)) ? '1' : '0' ); + if (i==4){putchar(':');} + } + +} + +// This function will take two characters and print formatted as a binary number on the screen +void print2bytebinchar(char *buffer) +{ + printbincharpad(buffer[0]); + printf(" -- "); + printbincharpad(buffer[1]); + printf("\n"); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/helperFunctions.h Wed Oct 16 14:12:28 2013 +0000 @@ -0,0 +1,9 @@ +#include <stdio.h> +#include "mbed.h" + +// This function will print a 7 bit char as a binary number on the screen +void printbincharpad(char c); + +// This function will take two characters and print formatted as a binary number on the screen +void print2bytebinchar(char *buffer); +
--- a/main.cpp Tue Oct 08 15:25:46 2013 +0000 +++ b/main.cpp Wed Oct 16 14:12:28 2013 +0000 @@ -1,7 +1,7 @@ #include "mbed.h" //#include <bitset> #include "MMA8452.h" - +#include "helperFunctions.h" // I2C i2c(p28,p27); Accelerometer_MMA8452 accelerometer(p28, p27, 40000); @@ -12,6 +12,14 @@ DigitalOut led2(LED2); DigitalOut led3(LED3); + + + + + + + + int main() { led1 = 0; led2 = 0; @@ -19,11 +27,13 @@ char cmd[6]; char add[1]; char init[2]; - add[0] = OUT_X_MSB; // x-axis register - init[0] = CTRL_REG_1; // control register 1 - init[1] = 0x01; // set to active + add[0] = OUT_X_MSB; // x-axis register + init[0] = CTRL_REG_1; // control register 1 + init[1] = 0x01; // sets the accelerometer to active int number=0; - //i2c.frequency(40000); + char x_buffer[2]; // used to store a bit array from the x axis acclerometer + char y_buffer[2]; // this represents the raw data value from the acclerometer + char z_buffer[2]; // which is converted from 12bit to 16bit value 2s compliment. // just playing with address values char mcu_address = 0x00; @@ -37,9 +47,32 @@ //init //set active mode pc.printf("\nWriting to master register\n"); - // while(i2c.write(mcu_address,init,2)); - for (int i=0;i<=400;i++) + for (int i=0;i<=50;i++) + { + int deviceSystemMode= 0; + if (accelerometer.get_SystemMode(deviceSystemMode)==0) + { + pc.printf("\nGot the device System Mode - device system mode which is: 0x%x \n", deviceSystemMode); + } + else { + pc.printf("\nFailed to get the device system mode. The id is set as: 0x%x ", deviceSystemMode); + + } + + int deviceStatus = 0; + if (accelerometer.get_Status(deviceStatus)==0) + { + pc.printf("\nGot the device Status - device status which is: 0x%x \n", deviceStatus); + } + else + { + pc.printf("\nFailed to get the device status. The id is set as: 0x%x ", deviceStatus); + + } + + + if (accelerometer.activate()==0) { led1 = 1; @@ -47,27 +80,132 @@ 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++) + + //int x=0;//int y=0;int z=0; + for(int i=0; i<5;i++) { - z = accelerometer.read_z(); - x = accelerometer.read_x(); - y = accelerometer.read_y(); + + + if (accelerometer.read_x_raw(x_buffer)==0) + { + //pc.printf("\nThe x-axis LSB is: 0x%x", x); + 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]); + 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]); + print2bytebinchar(x_buffer); + + } + else + { + pc.printf("\nError - could not get xaxis LSB! ....\n"); + } + + - pc.printf("\nAxis acceleration in x is: %i , y is: %i, z is %z \n", x,y,z); - wait(3); - } - */ + if (accelerometer.read_y_raw(y_buffer)==0) + { + //pc.printf("\nThe x-axis LSB is: 0x%x", x); + 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]); + 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]); + print2bytebinchar(y_buffer); + + } + else + { + pc.printf("\nError - could not get yaxis LSB! ....\n"); + } + + + + if (accelerometer.read_z_raw(z_buffer)==0) + { + //pc.printf("\nThe x-axis LSB is: 0x%x", x); + 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]); + 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]); + print2bytebinchar(y_buffer); + + } + else + { + pc.printf("\nError - could not get zaxis LSB! ....\n"); + } + + + + //y = accelerometer.read_y(); + + //pc.printf("\nAxis acceleration in x is: %i , y is: %i, z is %z \n", x,y,z); + wait(1); + } } else { pc.printf("Could not activate the chip\n"); } - pc.printf("In loop %d, of repeated initialisation.\n",i); i++; + + + int deviceID= 0; + if (accelerometer.get_DeviceID(deviceID)==0) + { + pc.printf("\nGot the who am I - device id which is: 0x%x \n", deviceID); + } + else + { + pc.printf("\nFailed to get the device id. The id is set as: 0x%x ", deviceID); + + } + + if (accelerometer.get_Status(deviceStatus)==0) + { + pc.printf("\nGot the device Status - device system mode which is: 0x%x \n", deviceSystemMode); + } + else + { + pc.printf("\nFailed to get the device system mode. The id is set as: 0x%x ", deviceSystemMode); + + } + + wait(3); + + pc.printf("In loop %d, of repeated initialisation.\n",i); + i++; } //get analog data - pc.printf("Getting analog data"); + pc.printf("Set system into standby mode........."); + + if(accelerometer.standby()==0) + { + pc.printf("Device set into standby...\n"); + } + else + { + pc.printf("Device failed to set into standby...\n"); + } + int deviceSystemMode = 0; + pc.printf("\nGet system mode, it should now be standby! ...\n"); + if (accelerometer.get_SystemMode(deviceSystemMode)==0) + { + pc.printf("\nGot the device System Mode - device system mode which is: 0x%x \n", deviceSystemMode); + } + else + { + pc.printf("\nFailed to get the device system mode. The id is set as: 0x%x ", deviceSystemMode); + + } + + + + + + + + + + + + + + /* while(1) {