Test program for the MMA8451Q library
Dependencies: FRDM_MMA8451Q mbed
Fork of FRDM_MMA8451Q by
main.cpp
00001 #include "mbed.h" 00002 #include "MMA8451Q.h" 00003 00004 #define MMA8451_I2C_ADDRESS (0x1d<<1) 00005 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); 00006 00007 PwmOut rled(LED_RED); 00008 PwmOut gled(LED_GREEN); 00009 PwmOut bled(LED_BLUE); 00010 Serial pc(USBTX, USBRX); 00011 00012 void motion( void); // callback function for motion detection mode 00013 void freefall( void); // callback function for freefall detection mode 00014 void orientation( void); // callback function for orientation detection mode 00015 void dataready( void); 00016 00017 unsigned int ff, md, od; 00018 float sensor_data[3]; 00019 int16_t raw_data[3]; 00020 00021 int main(void) { 00022 00023 rled = 0.0; 00024 gled = 0.0; 00025 bled = 0.0; 00026 00027 ff = md = 0; 00028 00029 pc.baud( 230400); 00030 pc.printf("MMA8451 Accelerometer. [%X]\r\n", acc.getWhoAmI()); 00031 00032 // 00033 pc.printf("FreeFall Detection\r\n"); 00034 // Configure the accelerometer for the freefall detection and 00035 // set the callback function: 00036 acc.FreeFallDetection( &freefall); 00037 00038 while( 1) { 00039 // please type in a key: 00040 if(pc.readable()) { 00041 switch (pc.getc()) { 00042 case 'f': 00043 // Configure the accelerometer for the freefall detection and 00044 // set the callback function: 00045 pc.printf("FreeFall Detection\r\n"); 00046 acc.FreeFallDetection( &freefall); 00047 break; 00048 case 'm': 00049 // Configure the accelerometer for the motion detection and 00050 // set the callback function: 00051 pc.printf("Motion Detection\r\n"); 00052 acc.MotionDetection( &motion); 00053 break; 00054 case 'o': 00055 // Configure the accelerometer for the orientation detection and 00056 // set the callback function: 00057 pc.printf("Orientation Detection\r\n"); 00058 acc.OrientationDetect( &orientation); 00059 break; 00060 case 'r': 00061 // Configure the accelerometer for the data ready and 00062 // set the callback function: 00063 pc.printf("Data Ready\r\n"); 00064 acc.DataReady( &dataready, cODR_1_56HZ); 00065 break; 00066 case 'w': 00067 // Read the accelorometer value in raw mode. 00068 pc.printf("Polling method and data raw\r\n"); 00069 // 00070 while( 1) { 00071 if ( pc.readable()) { 00072 break; 00073 } 00074 // Check data availability 00075 if ( acc.getAccRawAllAxis( &raw_data[0])) { 00076 pc.printf("X: %X, Y: %X, Z: %X\r\n", raw_data[0], raw_data[1], raw_data[2]); 00077 wait( 1.0); 00078 } 00079 } 00080 break; 00081 00082 } 00083 } 00084 } 00085 } 00086 00087 // callback function for orientation detection mode 00088 void orientation( void) 00089 { 00090 unsigned char o; 00091 00092 o = acc.GetOrientationState(); 00093 00094 bled = 1.0; 00095 00096 od++; 00097 00098 // 00099 if ( o & 0x01) 00100 pc.printf("Front "); 00101 else 00102 pc.printf("Back "); 00103 pc.printf("facing\r\n"); 00104 00105 // 00106 o = (o>>1) & 0x03; 00107 switch( o) { 00108 case 0: 00109 pc.printf("Portrait Up "); 00110 break; 00111 case 1: 00112 pc.printf("Portrait Down "); 00113 break; 00114 case 2: 00115 pc.printf("Landscape Right "); 00116 break; 00117 case 3: 00118 pc.printf("Landscape Left "); 00119 break; 00120 } 00121 00122 pc.printf( "\r\nod %d\t ornt: %X\r\n", od, o); 00123 } 00124 00125 // callback function for motion detection mode 00126 void motion( void) 00127 { 00128 rled = 1.0; 00129 00130 md++; 00131 pc.printf( "md %d\r\n", md); 00132 } 00133 00134 // callback function for freefall detection mode 00135 void freefall( void) 00136 { 00137 gled = 1.0; 00138 00139 ff++; 00140 pc.printf( "ff %d\r\n", ff); 00141 } 00142 00143 // callback function for data reading 00144 void dataready( void) 00145 { 00146 acc.getAccAllAxis( &sensor_data[0]); 00147 pc.printf("X: %f, Y: %f, Z: %f\r\n", sensor_data[0], sensor_data[1], sensor_data[2]); 00148 }
Generated on Fri Jul 15 2022 14:33:51 by 1.7.2