SPI Accelerometer Hello World program

Dependencies:   FXLS8471 mbed

Committer:
AswinSivakumar
Date:
Sat Jan 23 00:05:52 2016 +0000
Revision:
1:d8ca07afb3ec
Parent:
0:6b858a805a38
Child:
3:9eae869bdf8e
Added additional sensor libraries;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AswinSivakumar 0:6b858a805a38 1 /* Copyright (c) 2015 NXP Semiconductors. MIT License
AswinSivakumar 0:6b858a805a38 2 *
AswinSivakumar 0:6b858a805a38 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
AswinSivakumar 0:6b858a805a38 4 * and associated documentation files (the "Software"), to deal in the Software without
AswinSivakumar 0:6b858a805a38 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
AswinSivakumar 0:6b858a805a38 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
AswinSivakumar 0:6b858a805a38 7 * Software is furnished to do so, subject to the following conditions:
AswinSivakumar 0:6b858a805a38 8 *
AswinSivakumar 0:6b858a805a38 9 * The above copyright notice and this permission notice shall be included in all copies or
AswinSivakumar 0:6b858a805a38 10 * substantial portions of the Software.
AswinSivakumar 0:6b858a805a38 11 *
AswinSivakumar 0:6b858a805a38 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
AswinSivakumar 0:6b858a805a38 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
AswinSivakumar 0:6b858a805a38 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
AswinSivakumar 0:6b858a805a38 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
AswinSivakumar 0:6b858a805a38 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
AswinSivakumar 0:6b858a805a38 17 */
AswinSivakumar 0:6b858a805a38 18 #include "FXAS21002.h"
AswinSivakumar 0:6b858a805a38 19 #include "FXOS8700.h"
AswinSivakumar 1:d8ca07afb3ec 20 #include "MAG3110.h"
AswinSivakumar 1:d8ca07afb3ec 21 #include "MMA8652.h"
AswinSivakumar 1:d8ca07afb3ec 22 #include "MPL3115.h"
AswinSivakumar 0:6b858a805a38 23 #include "mbed.h"
AswinSivakumar 0:6b858a805a38 24
AswinSivakumar 0:6b858a805a38 25 // Initialize Serial port
AswinSivakumar 0:6b858a805a38 26 Serial pc(USBTX, USBRX);
AswinSivakumar 0:6b858a805a38 27
AswinSivakumar 0:6b858a805a38 28 // Initialize pins for I2C communication for sensors. Set jumpers J6,J7 in FRDM-STBC-AGM01 board accordingly.
AswinSivakumar 0:6b858a805a38 29 FXOS8700 accel(D14,D15);
AswinSivakumar 0:6b858a805a38 30 FXOS8700 mag(D14,D15);
AswinSivakumar 0:6b858a805a38 31 FXAS21002 gyro(D14,D15);
AswinSivakumar 1:d8ca07afb3ec 32 MAG3110 mag3110(D14,D15);
AswinSivakumar 1:d8ca07afb3ec 33 MMA8652 mma8652(D14,D15);
AswinSivakumar 1:d8ca07afb3ec 34 MPL3115 mpl3115(D14,D15);
AswinSivakumar 0:6b858a805a38 35
AswinSivakumar 1:d8ca07afb3ec 36
AswinSivakumar 1:d8ca07afb3ec 37
AswinSivakumar 0:6b858a805a38 38 int main()
AswinSivakumar 0:6b858a805a38 39 {
AswinSivakumar 1:d8ca07afb3ec 40
AswinSivakumar 1:d8ca07afb3ec 41 // Configure Accelerometer FXOS8700, Magnetometer FXOS8700 & Gyroscope FXAS21002
AswinSivakumar 0:6b858a805a38 42 accel.accel_config();
AswinSivakumar 0:6b858a805a38 43 mag.mag_config();
AswinSivakumar 0:6b858a805a38 44 gyro.gyro_config();
AswinSivakumar 1:d8ca07afb3ec 45 mag3110.MAG3110_config();
AswinSivakumar 1:d8ca07afb3ec 46 mma8652.MMA8652_config();
AswinSivakumar 1:d8ca07afb3ec 47 mpl3115.MPL3115_config();
AswinSivakumar 1:d8ca07afb3ec 48
AswinSivakumar 1:d8ca07afb3ec 49
AswinSivakumar 0:6b858a805a38 50 float accel_data[3]; float accel_rms=0.0;
AswinSivakumar 0:6b858a805a38 51 float mag_data[3]; float mag_rms=0.0;
AswinSivakumar 0:6b858a805a38 52 float gyro_data[3]; float gyro_rms=0.0;
AswinSivakumar 1:d8ca07afb3ec 53 float alt_data[3]; float alt_rms=0.0;
AswinSivakumar 0:6b858a805a38 54
AswinSivakumar 1:d8ca07afb3ec 55 printf("Begin Data Acquisition....\r\n\r\n");
AswinSivakumar 0:6b858a805a38 56 wait(0.5);
AswinSivakumar 0:6b858a805a38 57
AswinSivakumar 0:6b858a805a38 58 while(1)
AswinSivakumar 0:6b858a805a38 59 {
AswinSivakumar 0:6b858a805a38 60 accel.acquire_accel_data_g(accel_data);
AswinSivakumar 0:6b858a805a38 61 accel_rms = sqrt(((accel_data[0]*accel_data[0])+(accel_data[1]*accel_data[1])+(accel_data[2]*accel_data[2]))/3);
AswinSivakumar 1:d8ca07afb3ec 62 printf("%4.2f,%4.2f,%4.2f,\t",accel_data[0],accel_data[1],accel_data[2]);
AswinSivakumar 0:6b858a805a38 63 wait(0.005);
AswinSivakumar 0:6b858a805a38 64
AswinSivakumar 1:d8ca07afb3ec 65 // mma8652.acquire_MMA8652_data_g(accel_data);
AswinSivakumar 1:d8ca07afb3ec 66 // accel_rms = sqrt(((accel_data[0]*accel_data[0])+(accel_data[1]*accel_data[1])+(accel_data[2]*accel_data[2]))/3);
AswinSivakumar 1:d8ca07afb3ec 67 // printf("%4.2f,%4.2f,%4.2f,\t",accel_data[0],accel_data[1],accel_data[2]);
AswinSivakumar 1:d8ca07afb3ec 68 // wait(0.005);
AswinSivakumar 1:d8ca07afb3ec 69
AswinSivakumar 1:d8ca07afb3ec 70 // mag.acquire_mag_data_uT(mag_data);
AswinSivakumar 1:d8ca07afb3ec 71 // printf("%4.2f,\t%4.2f,\t%4.2f,\t",mag_data[0],mag_data[1],mag_data[2]);
AswinSivakumar 1:d8ca07afb3ec 72 // mag_rms = sqrt(((mag_data[0]*mag_data[0])+(mag_data[1]*mag_data[1])+(mag_data[2]*mag_data[2]))/3);
AswinSivakumar 1:d8ca07afb3ec 73 // wait(0.005);
AswinSivakumar 1:d8ca07afb3ec 74
AswinSivakumar 1:d8ca07afb3ec 75 mag3110.acquire_MAG3110_data_uT(mag_data);
AswinSivakumar 1:d8ca07afb3ec 76 printf("%f,%f,%f,\t",mag_data[0],mag_data[1],mag_data[2]);
AswinSivakumar 0:6b858a805a38 77 mag_rms = sqrt(((mag_data[0]*mag_data[0])+(mag_data[1]*mag_data[1])+(mag_data[2]*mag_data[2]))/3);
AswinSivakumar 0:6b858a805a38 78 wait(0.005);
AswinSivakumar 0:6b858a805a38 79
AswinSivakumar 0:6b858a805a38 80 gyro.acquire_gyro_data_dps(gyro_data);
AswinSivakumar 1:d8ca07afb3ec 81 printf("%4.2f,%4.2f,%4.2f,",gyro_data[0],gyro_data[1],gyro_data[2]);
AswinSivakumar 0:6b858a805a38 82 gyro_rms = sqrt(((gyro_data[0]*gyro_data[0])+(gyro_data[1]*gyro_data[1])+(gyro_data[2]*gyro_data[2]))/3);
AswinSivakumar 0:6b858a805a38 83 wait(0.005);
AswinSivakumar 0:6b858a805a38 84
AswinSivakumar 1:d8ca07afb3ec 85 mpl3115.acquire_MPL3115_data_Altitude_in_m(alt_data);
AswinSivakumar 1:d8ca07afb3ec 86 printf("\t%f",alt_data[0]);
AswinSivakumar 1:d8ca07afb3ec 87 alt_rms = sqrt(((alt_data[0]*alt_data[0])+(alt_data[1]*alt_data[1])+(alt_data[2]*alt_data[2]))/3);
AswinSivakumar 1:d8ca07afb3ec 88 wait(0.005);
AswinSivakumar 1:d8ca07afb3ec 89
AswinSivakumar 1:d8ca07afb3ec 90 printf("\n\r");
AswinSivakumar 1:d8ca07afb3ec 91
AswinSivakumar 1:d8ca07afb3ec 92
AswinSivakumar 0:6b858a805a38 93 }
AswinSivakumar 0:6b858a805a38 94
AswinSivakumar 0:6b858a805a38 95 }