Demo program for the MPL3115A2 library.

Dependencies:   FOCOMETROPRESION mbed

Fork of WIGO_MPL3115A2 by clemente di caprio

Revision:
3:e2a621ea6976
Parent:
2:0b726b8c8ab9
Child:
4:bcf56c80dd2b
--- 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;
+
+}
+