Demo program for the MPL3115A2 library.

Dependencies:   MPL3115A2 mbed

Example program for MPL3115 sensor. Added code for data acquisition using Interrupt.

Files at this revision

API Documentation at this revision

Comitter:
clemente
Date:
Thu Aug 22 12:03:47 2013 +0000
Parent:
2:0b726b8c8ab9
Child:
4:bcf56c80dd2b
Commit message:
Added example about how to use raw mode function.

Changed in this revision

MPL3115A2.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/MPL3115A2.lib	Thu May 30 07:29:41 2013 +0000
+++ b/MPL3115A2.lib	Thu Aug 22 12:03:47 2013 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/clemente/code/MPL3115A2/#fdf14a259af8
+http://mbed.org/users/clemente/code/MPL3115A2/#9edec5ee8bf4
--- a/main.cpp	Thu May 30 07:29:41 2013 +0000
+++ b/main.cpp	Thu Aug 22 12:03:47 2013 +0000
@@ -14,12 +14,17 @@
 void dataready( void);      // callback function for data streaming using Interrupt
 void alttrigger( void);
 
+/* Helper functions to print out as float the raw data */
+float print_PressureValue( unsigned char *dt);
+float print_AltimiterValue( unsigned char *dt);
+float print_TemperatureValue( unsigned char *dt);
+
 int main() {
     
     pc.baud( 230400);
     pc.printf("MPL3115A2 Sensor. [%X]\r\n", wigo_sensor1.getDeviceID());
         
-#if 1
+#if 0
     // ***** Data acquisition using Interrupt
     
     // Configure the sensor as Barometer.
@@ -33,6 +38,7 @@
         pc.printf(".");
     }
 #else
+#if 0
     // ***** Data acquisition using polling method
     
     // Configure the sensor as Barometer.
@@ -57,8 +63,39 @@
             mode++;
         }
         //
-        wait( 0.001);
+        wait( 1.0);
     }
+#else
+    // ***** Data acquisition in RAW mode using polling method
+
+    // Create a buffer for raw data
+    unsigned char raw_data[5];
+    // Configure the sensor as Barometer.
+    unsigned int mode=1;
+    
+    // Set over sampling value (see MPL3115A2.h for details)
+    wigo_sensor1.Oversample_Ratio( OVERSAMPLE_RATIO_64);
+    // Configure the sensor as Barometer.
+    wigo_sensor1.Barometric_Mode();
+
+    while(1) {
+        //
+        if ( wigo_sensor1.isDataAvailable()) {
+            wigo_sensor1.getAllDataRaw( &raw_data[0]);
+            if ( mode & 0x0001) {
+                pc.printf("\tPressure: %f\tTemperature: %f\r\n", print_PressureValue( &raw_data[0]), print_TemperatureValue( &raw_data[3]));
+                wigo_sensor1.Altimeter_Mode();
+            } else {
+                pc.printf("\tAltitude: %f\tTemperature: %f\r\n", print_AltimiterValue( &raw_data[0]), print_TemperatureValue( &raw_data[3]));
+                wigo_sensor1.Barometric_Mode();            
+            }
+            mode++;
+        }
+        //
+        wait( 1.0);
+    }
+    
+#endif    
 #endif    
 }
 
@@ -67,3 +104,72 @@
     wigo_sensor1.getAllData( &sensor_data[0]);
     pc.printf("\tPressure: %f\tTemperature: %f\r\n", sensor_data[0], sensor_data[1]);
 }
+
+float print_PressureValue( unsigned char *dt)
+{
+    unsigned int prs;
+    float fprs;
+
+    /*
+    * dt[0] = Bits 12-19 of 20-bit real-time Pressure sample. (b7-b0)
+    * dt[1] = Bits 4-11 of 20-bit real-time Pressure sample. (b7-b0)
+    * dt[2] = Bits 0-3 of 20-bit real-time Pressure sample (b7-b4)
+    */
+    prs = (dt[0]<<10) | (dt[1]<<2) | (dt[2]>>6);
+    //
+    fprs = (float)prs * 1.0f;
+    
+    if ( dt[2] & 0x20)
+        fprs += 0.25f;
+    if ( dt[2] & 0x10)
+        fprs += 0.5f;
+    
+    return fprs;
+}
+
+float print_AltimiterValue( unsigned char *dt)
+{
+    unsigned short altm;
+    float faltm;
+
+    /*
+    * dt[0] = Bits 12-19 of 20-bit real-time Altitude sample. (b7-b0)
+    * dt[1] = Bits 4-11 of 20-bit real-time Altitude sample. (b7-b0)
+    * dt[2] = Bits 0-3 of 20-bit real-time Altitude sample (b7-b4)
+    */    
+    altm = (dt[0]<<8) | dt[1];
+    //
+    if ( dt[0] > 0x7F) {
+        altm = ~altm + 1;
+        faltm = (float)altm * -1.0f;
+    } else {
+        faltm = (float)altm * 1.0f;
+    }
+    //
+    faltm = faltm+((float)(dt[2]>>4) * 0.0625f);
+    return faltm;
+}
+
+float print_TemperatureValue( unsigned char *dt)
+{
+    unsigned short temp;
+    float ftemp;
+    
+    /*
+    * dt[0] = Bits 4-11 of 16-bit real-time temperature sample. (b7-b0)
+    * dt[1] = Bits 0-3 of 16-bit real-time temperature sample. (b7-b4)
+    */
+    temp = dt[0];
+    //
+    if ( dt[0] > 0x7F) {
+        temp = ~temp + 1;
+        ftemp = (float)temp * -1.0f;
+    } else {
+        ftemp = (float)temp * 1.0f;
+    }
+    //
+    ftemp = ftemp+((float)(dt[1]>>4) * 0.0625f);
+    return ftemp;
+
+}
+