Llibrary for the WiGo MPL3115A2, I2C Precision Altimeter sensor.

Dependents:   XtrinsicSensorEVK

Fork of MPL3115A2 by clemente di caprio

Files at this revision

API Documentation at this revision

Comitter:
clemente
Date:
Sun Sep 22 11:17:28 2013 +0000
Parent:
8:89ed6aeb5dbb
Child:
10:82ac06669316
Commit message:
Added function to read the maximum and minimum value captured. Some modification to the internal function.

Changed in this revision

MPL3115A2.cpp Show annotated file Show diff for this revision Revisions of this file
MPL3115A2.h Show annotated file Show diff for this revision Revisions of this file
--- a/MPL3115A2.cpp	Sun Sep 22 07:04:14 2013 +0000
+++ b/MPL3115A2.cpp	Sun Sep 22 11:17:28 2013 +0000
@@ -12,6 +12,15 @@
 #define REG_PT_DATA_CFG     0x13
 #define REG_P_TGT_MSB       0x16
 #define REG_P_WND_MSB       0x19
+#define REG_OFF_P           0x2b
+#define REG_OFF_T           0x2c
+#define REG_OFF_H           0x2d
+#define REG_PRES_MIN_MSB    0x1c
+#define REG_ALTI_MIN_MSB    0x1c
+#define REG_TEMP_MIN_MSB    0x1f
+#define REG_PRES_MAX_MSB    0x21
+#define REG_ALTI_MAX_MSB    0x21
+#define REG_TEMP_MAX_MSB    0x24
 
 #define UINT14_MAX        16383
 
@@ -40,6 +49,8 @@
 InterruptIn MPL3115A2_Int2( PTA12);      // INT2
 
 MPL3115A2::MPL3115A2(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) {
+    unsigned char data[6];
+    
     MPL3115A2_mode = BAROMETRIC_MODE;
     MPL3115A2_oversampling = OVERSAMPLE_RATIO_1;
     //
@@ -49,6 +60,10 @@
     MPL3115A2_Int2.fall( NULL);
     
     Reset();
+    
+    data[0]=REG_PRES_MIN_MSB;
+    data[1]=0;data[2]=0;data[3]=0;data[4]=0;data[5]=0;
+    writeRegs( &data[0], 6);
 }
 
 void MPL3115A2::Reset( void)
@@ -298,20 +313,50 @@
 {
     if ( isDataAvailable() & PTDR_STATUS) {
         if ( MPL3115A2_mode == ALTIMETER_MODE) {
-            f[0] = getAltimeter();
+            f[0] = getAltimeter( REG_ALTIMETER_MSB);
         } else {
-            f[0] = getPressure();
+            f[0] = getPressure( REG_PRESSURE_MSB);
         }
         
-        f[1] = getTemperature();
+        f[1] = getTemperature( REG_TEMP_MSB);
         //
         return 1;
     } else
         return 0;
 }
 
+void MPL3115A2::getAllMaximumData( float *f)
+{
+    if ( MPL3115A2_mode == ALTIMETER_MODE) {
+        f[0] = getAltimeter( REG_ALTI_MAX_MSB);
+    } else {
+        f[0] = getPressure( REG_PRES_MAX_MSB);
+    }
+    
+    f[1] = getTemperature( REG_TEMP_MAX_MSB);
+}
+
+void MPL3115A2::getAllMinimumData( float *f)
+{
+    if ( MPL3115A2_mode == ALTIMETER_MODE) {
+        f[0] = getAltimeter( REG_ALTI_MIN_MSB);
+    } else {
+        f[0] = getPressure( REG_PRES_MIN_MSB);
+    }
+    
+    f[1] = getTemperature( REG_TEMP_MIN_MSB);
+}
+
 float MPL3115A2::getAltimeter( void)
 {
+    float a;
+    
+    a = getAltimeter( REG_ALTIMETER_MSB);
+    return a;
+}
+
+float MPL3115A2::getAltimeter( unsigned char reg)
+{
     unsigned char dt[3];
     unsigned short altm;
     float faltm;
@@ -321,7 +366,7 @@
     * 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)
     */
-    readRegs( REG_ALTIMETER_MSB, &dt[0], 3);
+    readRegs( reg, &dt[0], 3);
     altm = (dt[0]<<8) | dt[1];
     //
     if ( dt[0] > 0x7F) {
@@ -337,6 +382,14 @@
 
 float MPL3115A2::getPressure( void)
 {
+    float a;
+    
+    a = getPressure( REG_PRESSURE_MSB);
+    return a;
+}
+
+float MPL3115A2::getPressure( unsigned char reg)
+{
     unsigned char dt[3];
     unsigned int prs;
     float fprs;
@@ -346,7 +399,7 @@
     * 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)
     */
-    readRegs( REG_PRESSURE_MSB, &dt[0], 3);
+    readRegs( reg, &dt[0], 3);
     prs = (dt[0]<<10) | (dt[1]<<2) | (dt[2]>>6);
     //
     fprs = (float)prs * 1.0f;
@@ -359,8 +412,15 @@
     return fprs;
 }
 
+float MPL3115A2::getTemperature( void)
+{
+    float a;
+    
+    a = getTemperature( REG_TEMP_MSB);
+    return a;
+}
 
-float MPL3115A2::getTemperature( void)
+float MPL3115A2::getTemperature( unsigned char reg)
 {
     unsigned char dt[2];
     unsigned short temp;
@@ -370,7 +430,7 @@
     * 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)
     */
-    readRegs( REG_TEMP_MSB, &dt[0], 2);
+    readRegs( reg, &dt[0], 2);
     temp = dt[0];
     //
     if ( dt[0] > 0x7F) {
@@ -385,6 +445,7 @@
 
 }
 
+
 unsigned int MPL3115A2::getAllDataRaw( unsigned char *dt)
 {
     // Check for Press/Alti and Temp value ready
@@ -456,7 +517,7 @@
 
 void MPL3115A2::SetPressureOffset( char offset)
 {
-    unsigned char data [2] = {0x2b, offset};
+    unsigned char data [2] = {REG_OFF_P, offset};
 
     Standby();
     writeRegs(data,2);
@@ -466,7 +527,7 @@
 
 void MPL3115A2::SetTemperatureOffset( char offset)
 {
-    unsigned char data [2] = {0x2c, offset};
+    unsigned char data [2] = {REG_OFF_T, offset};
 
     Standby();
     writeRegs(data,2);
@@ -476,7 +537,7 @@
 
 void MPL3115A2::SetAltitudeOffset( char offset)
 {
-    unsigned char data [2] = {0x2d, offset};
+    unsigned char data [2] = {REG_OFF_H, offset};
 
     Standby();
     writeRegs(data,2);
--- a/MPL3115A2.h	Sun Sep 22 07:04:14 2013 +0000
+++ b/MPL3115A2.h	Sun Sep 22 11:17:28 2013 +0000
@@ -146,6 +146,22 @@
     unsigned int  getAllData( float *f);
 
     /**
+    * Get the altimeter or pressure and temperature captured maximum value
+    *
+    * @param array of float f[2]
+    * @returns 0 no data available, 1 for data available
+    */
+    void getAllMaximumData( float *f);
+
+    /**
+    * Get the altimeter or pressure and temperature captured minimum value
+    *
+    * @param array of float f[2]
+    * @returns 0 no data available, 1 for data available
+    */
+    void getAllMinimumData( float *f);
+
+    /**
     * Get the altimeter or pressure, and temperature values in raw mode
     *
     * @param array of unsigned char[5]
@@ -245,6 +261,33 @@
     */
     void Standby( void);
     
+    /** Get the altimiter value from the sensor.
+    *
+    * @param    reg the register from which read the data. 
+    *           Can be: REG_ALTIMETER_MSB for altimeter value
+    *                   REG_ALTI_MIN_MSB for the minimum value captured
+    *                   REG_ALTI_MAX_MSB for the maximum value captured
+    */
+    float getAltimeter( unsigned char reg);    
+
+    /** Get the pressure value from the sensor.
+    *
+    * @param    reg the register from which read the data. 
+    *           Can be: REG_PRESSURE_MSB for altimeter value
+    *                   REG_PRES_MIN_MSB for the minimum value captured
+    *                   REG_PRES_MAX_MSB for the maximum value captured
+    */    
+    float getPressure( unsigned char reg);
+
+    /** Get the altimiter value from the sensor.
+    *
+    * @param    reg the register from which read the data. 
+    *           Can be: REG_TEMP_MSB for altimeter value
+    *                   REG_TEMP_MIN_MSB for the minimum value captured
+    *                   REG_TEMP_MAX_MSB for the maximum value captured
+    */
+    float getTemperature( unsigned char reg);
+    
     void readRegs(int addr, uint8_t * data, int len);
     void writeRegs(uint8_t * data, int len);