This is an example program for the Freescale multi-sensor shield, part number FRDM-FXS-MULTI

Dependencies:   FXAS21000 FXLS8471Q FXOS8700Q MAG3110 MMA8652 MPL3115A2 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* Copyright (c) 2010-2011 mbed.org, MIT License
00002 *
00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004 * and associated documentation files (the "Software"), to deal in the Software without
00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
00007 * Software is furnished to do so, subject to the following conditions:
00008 *
00009 * The above copyright notice and this permission notice shall be included in all copies or
00010 * substantial portions of the Software.
00011 *
00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017 */
00018 
00019 #include "mbed.h"
00020 #include "MAG3110.h"
00021 #include "MPL3115A2.h" // May only be used on a Freescale platform, comment out for others
00022 #include "FXOS8700Q.h"
00023 #include "MMA8652.h"
00024 #include "FXLS8471Q.h"
00025 #include "FXAS21000.h"
00026 
00027 #define MPL3115A2_I2C_ADDRESS (0x60<<1)
00028 
00029 
00030 DigitalOut  red(LED1);
00031 
00032 FXLS8471Q acc1(D11, D12, D13, D10);
00033 MMA8652   acc2(A4, A5);
00034 FXOS8700Q_acc combo_acc(A4, A5, FXOS8700CQ_SLAVE_ADDR0);
00035 FXOS8700Q_mag combo_mag(A4, A5, FXOS8700CQ_SLAVE_ADDR0);
00036 MAG3110   mag2(A4, A5);
00037 FXAS21000 gyro(A4, A5);
00038 MPL3115A2 alt(A4, A5, MPL3115A2_I2C_ADDRESS);  // May only be used on a Freescale platform, comment out for others
00039 
00040 Serial pc(USBTX, USBRX);
00041 
00042 
00043 
00044 float print_AltimiterValue( unsigned char *dt)
00045 {
00046     unsigned short altm;
00047     float faltm;
00048 
00049     /*
00050     * dt[0] = Bits 12-19 of 20-bit real-time Altitude sample. (b7-b0)
00051     * dt[1] = Bits 4-11 of 20-bit real-time Altitude sample. (b7-b0)
00052     * dt[2] = Bits 0-3 of 20-bit real-time Altitude sample (b7-b4)
00053     */
00054     altm = (dt[0]<<8) | dt[1];
00055     //
00056     if ( dt[0] > 0x7F) {
00057         altm = ~altm + 1;
00058         faltm = (float)altm * -1.0f;
00059     } else {
00060         faltm = (float)altm * 1.0f;
00061     }
00062     //
00063     faltm = faltm+((float)(dt[2]>>4) * 0.0625f);
00064     return faltm;
00065 }
00066 
00067 float print_TemperatureValue( unsigned char *dt)
00068 {
00069     unsigned short temp;
00070     float ftemp;
00071 
00072     /*
00073     * dt[0] = Bits 4-11 of 16-bit real-time temperature sample. (b7-b0)
00074     * dt[1] = Bits 0-3 of 16-bit real-time temperature sample. (b7-b4)
00075     */
00076     temp = dt[0];
00077     //
00078     if ( dt[0] > 0x7F) {
00079         temp = ~temp + 1;
00080         ftemp = (float)temp * -1.0f;
00081     } else {
00082         ftemp = (float)temp * 1.0f;
00083     }
00084     //
00085     ftemp = ftemp+((float)(dt[1]>>4) * 0.0625f);
00086     return ftemp;
00087 
00088 }
00089 
00090 
00091 int main()
00092 {
00093     float acc_data[3], mag_data[3], gyro_data[3];
00094     MotionSensorDataUnits adata;
00095     MotionSensorDataUnits mdata;
00096     int16_t acc_raw[3];
00097     unsigned char raw_data[8];
00098 
00099     printf("\r\nStarting\r\n\r\n");
00100 
00101     red = 1;
00102     combo_acc.enable();
00103     combo_mag.enable();
00104     mag2.enable();
00105     alt.Altimeter_Mode();
00106     printf("FXLS8471 Acc   = %X\r\n", acc1.getWhoAmI());
00107     printf("MMA8652 Acc    = %X\r\n", acc2.getWhoAmI());
00108     printf("FXOS8700 Combo = %X\r\n", combo_acc.whoAmI());
00109     printf("MAG3110 Mag    = %X\r\n", mag2.whoAmI());
00110     printf("FXAS21000 Gyro = %X\r\n", gyro.getWhoAmI());
00111     printf("MPL3115A2 Alt  = %X\r\n", alt.getDeviceID());  // May only be used on a Freescale platform, comment out for others
00112     wait(3);
00113     
00114     while(1) {
00115         acc1.ReadXYZ(acc_data);
00116         acc1.ReadXYZraw(acc_raw);
00117         printf("FXLS8471 Acc: X:%1.3f Y:%1.3f Z:%1.3f (Raw X:%3d Y:%3d Z:%3d)\r\n", acc_data[0], acc_data[1], acc_data[2], acc_raw[0], acc_raw[1], acc_raw[2]);
00118 
00119         acc2.ReadXYZ(acc_data);
00120         acc2.ReadXYZraw(acc_raw);
00121         printf("MMA8652 Acc:  X:%1.3f Y:%1.3f Z:%1.3f (Raw X:%3d Y:%3d Z:%3d)\r\n", acc_data[0], acc_data[1], acc_data[2], acc_raw[0], acc_raw[1], acc_raw[2]);
00122 
00123         combo_acc.getAxis(adata);
00124         combo_mag.getAxis(mdata);
00125         printf("FXOS8700 Acc: X:%1.3f Y:%1.3f Z:%1.3f (Mag X:%4.1f Y:%4.1f Z:%4.1f)\r\n", adata.x, adata.y, adata.z, mdata.x, mdata.y, mdata.z);
00126         
00127         gyro.ReadXYZ(gyro_data);
00128         printf("FXAS21000 Gyro: X:%4.2f Y:%4.2f Z:%4.1f\r\n", gyro_data[0], gyro_data[1], gyro_data[2]);
00129         
00130         mag2.getAxis(mdata);
00131         printf("MAG3110 Mag:    X:%4.1f Y:%4.1f Z:%4.1f\r\n", mdata.x, mdata.y, mdata.z);
00132 
00133         alt.getAllDataRaw(&raw_data[0]);  // May only be used on a Freescale platform, comment out for others
00134         printf("MPL3115A2 Alt:  %5.1f\r\n", print_AltimiterValue(&raw_data[0]));  // May only be used on a Freescale platform, comment out for others
00135         printf("MPL3115A2 Temp: %3.1f\r\n", print_TemperatureValue(&raw_data[3]));  // May only be used on a Freescale platform, comment out for others
00136 
00137         printf("\r\n");
00138         
00139         red = 1;
00140         wait(0.5);
00141         red = 0;
00142         wait(0.5);
00143     }
00144 }