Llibrary for the WiGo MPL3115A2, I2C Precision Altimeter sensor.

Dependents:   KL25Z_Batt_Test WIGO_MPL3115A2 Multi-Sensor SPACEmk2 ... more

30/05/2013 Added and tested the data acquisition using Interrupt. Added code for Altimeter trigger Interrupt but not yet tested.

Very basic library. Under development. Need to add in order: 1. IRQ configuration. 2. FIFO mode configuration.

Revision:
10:82ac06669316
Parent:
9:75a5960adf5c
Child:
11:a5ce3ee460b6
diff -r 75a5960adf5c -r 82ac06669316 MPL3115A2.cpp
--- a/MPL3115A2.cpp	Sun Sep 22 11:17:28 2013 +0000
+++ b/MPL3115A2.cpp	Tue Sep 24 20:22:25 2013 +0000
@@ -21,6 +21,10 @@
 #define REG_PRES_MAX_MSB    0x21
 #define REG_ALTI_MAX_MSB    0x21
 #define REG_TEMP_MAX_MSB    0x24
+#define REG_PRES_DELTA_MSB  0x07
+#define REG_ALTI_DELTA_MSB  0x07
+#define REG_TEMP_DELTA_MSB  0x0a
+
 
 #define UINT14_MAX        16383
 
@@ -325,6 +329,25 @@
         return 0;
 }
 
+unsigned int MPL3115A2::getAllData( float *f, float *d)
+{
+    if ( isDataAvailable() & PTDR_STATUS) {
+        if ( MPL3115A2_mode == ALTIMETER_MODE) {
+            f[0] = getAltimeter();
+            d[0] = getAltimeter( REG_ALTI_DELTA_MSB);
+        } else {
+            f[0] = getPressure();
+            d[0] = getPressure( REG_PRES_DELTA_MSB);
+        }
+        
+        f[1] = getTemperature();
+        d[1] = getTemperature( REG_TEMP_DELTA_MSB);
+        //
+        return 1;
+    } else
+        return 0;
+}
+
 void MPL3115A2::getAllMaximumData( float *f)
 {
     if ( MPL3115A2_mode == ALTIMETER_MODE) {
@@ -359,6 +382,7 @@
 {
     unsigned char dt[3];
     unsigned short altm;
+    short tmp;
     float faltm;
     
     /*
@@ -370,8 +394,9 @@
     altm = (dt[0]<<8) | dt[1];
     //
     if ( dt[0] > 0x7F) {
-        altm = ~altm + 1;
-        faltm = (float)altm * -1.0f;
+        // negative number
+        tmp = ~altm + 1;
+        faltm = (float)tmp * -1.0f;
     } else {
         faltm = (float)altm * 1.0f;
     }
@@ -392,6 +417,7 @@
 {
     unsigned char dt[3];
     unsigned int prs;
+    int tmp;
     float fprs;
     
     /*
@@ -400,15 +426,25 @@
     * dt[2] = Bits 0-3 of 20-bit real-time Pressure sample (b7-b4)
     */
     readRegs( reg, &dt[0], 3);
-    prs = (dt[0]<<10) | (dt[1]<<2) | (dt[2]>>6);
+    prs = ((dt[0]<<10) | (dt[1]<<2) | (dt[2]>>6));
     //
-    fprs = (float)prs * 1.0f;
-    
+    if ( dt[0] > 0x7f) {
+        // negative number
+        if ( dt[0] & 0x80)
+            prs |= 0xFFFC0000;      // set at 1 the bits to complete the word len
+        else
+            prs |= 0xFFFE0000;
+        tmp = ~prs + 1;             // make the complemets. At this point all the bits are inverted.
+        fprs = (float)tmp * -1.0f;  // set the signe..
+    } else {
+        fprs = (float)prs * 1.0f;
+    }
+
+    if ( dt[2] & 0x10)              // I did some experiment to set the fractional parte.
+        fprs += 0.25f;              // ** Warning: the DS is wrong! **
     if ( dt[2] & 0x20)
-        fprs += 0.25f;
-    if ( dt[2] & 0x10)
         fprs += 0.5f;
-    
+        
     return fprs;
 }