Fixed Incorrect pressure readings for the barometer

Fork of MS5637 by chris stevens

Files at this revision

API Documentation at this revision

Comitter:
cam_eadie
Date:
Wed Jun 15 14:45:25 2016 +0000
Parent:
8:3a9d37268ccd
Commit message:
Fixed incorrect pressure reading using some float convesions, cleaning up and simplifying code in calcPT()

Changed in this revision

ms5637.cpp Show annotated file Show diff for this revision Revisions of this file
ms5637.h Show annotated file Show diff for this revision Revisions of this file
diff -r 3a9d37268ccd -r 83b2554646f5 ms5637.cpp
--- a/ms5637.cpp	Tue May 20 16:18:00 2014 +0000
+++ b/ms5637.cpp	Wed Jun 15 14:45:25 2016 +0000
@@ -15,6 +15,7 @@
 
 #include "mbed.h"
 #include "ms5637.h"
+//Serial pc2(USBTX, USBRX);
 
 double P;                       // compensated pressure value (mB)
 double T;                       // compensated temperature value (degC)
@@ -256,30 +257,32 @@
 void ms5637::calcPT() {
     int32_t D2 = cmd_adc(MS5637_CMD_ADC_D2 + MS5637_CMD_ADC_4096); // read D2
     int32_t D1 = cmd_adc(MS5637_CMD_ADC_D1 + MS5637_CMD_ADC_4096); // read D1
-    int64_t dT = D2 - ((uint64_t)C[5] << 8);
-    int64_t OFF  = ((uint32_t)C[2] << 17) + ((dT * (C[4]) >> 6));     //was  OFF  = (C[2] << 17) + dT * C[4] / (1 << 6);
-    int64_t SENS = ((uint32_t)C[1] << 16) + ((dT * (C[3]) >> 7));     //was  SENS = (C[1] << 16) + dT * C[3] / (1 << 7);
-    //T = (2000 + (((uint64_t)dT * C[6]) / (float)(1 << 23))) / 100;
-    T=(2000+(dT*C[6])/8388608)/100;
-    //int32_t TEMP = 2000 + (int64_t)dT * (int64_t)C[6] / (int64_t)(1 << 23);
-    int32_t TEMP = 2000 + (int64_t)dT * (int64_t)(C[6] >> 23);
-    if(TEMP < 2000) { // if temperature lower than 20 Celsius
-        float T1 = (TEMP - 2000) * (TEMP - 2000);
-        int64_t OFF1  = (61 * T1) / 16;
-        int64_t SENS1 = (29 * T1) / 16;
 
+    float dT = (float)D2 - ((float)C[5] * 256.0);
+    float TEMP = 2000.0 + (dT*(float)C[6])/8388608.0;
+    
+    float OFF  = ((float)C[2] * 131072.0 + (dT * (float)C[4])/64.0);
+    float SENS = ((float)C[1] * 65536.0)+ ((dT * ((float)C[3]) /128.0));
+    
+    // Commented out serial pc2 at start of code so you can check raw values
+    //pc2.printf("D1: %d\r\nD2: %d\r\nC1: %d\r\nC2: %d\r\nC3: %d\r\nC4: %d\r\nC5: %d\r\nC6: %d\r\n", D1, D2, C[1], C[2], C[3], C[4], C[5], C[6]);
+    //pc2.printf("OFF: %f\r\nSENS: %f\r\n",OFF, SENS);
+
+if(TEMP < 2000) { // if temperature lower than 20 Celsius
+        float T1 = (TEMP - 2000.0) * (TEMP - 2000.0);
+        float OFF1  = (61.0 * T1) / 16.0;
+        float SENS1 = (29.0 * T1) / 16.0;
+ 
         if(TEMP < -1500) { // if temperature lower than -15 Celsius
-            T1 = (TEMP + 1500) * (TEMP + 1500);
-            OFF1  += 17 * T1;
-            SENS1 += 9 * T1 ;
+            T1 = (TEMP + 1500.0) * (TEMP + 1500.0);
+            OFF1  += 17.0 * T1;
+            SENS1 += 9.0 * T1 ;
         } 
         OFF -= OFF1;
         SENS -= SENS1;
-        T = (float)TEMP / 100; 
     }
-//    int64_t P1 = ((((int64_t)D1 * SENS) >> 21) - OFF) >> 15;   
-    //P = ((((int64_t)D1 * SENS ) >> 21) - OFF) / (double) (1 << 15) / 100.0;
-    P=(D1*SENS/2097152-OFF)/3276800;
+        T = TEMP / 100.0;
+        P = ((D1*(SENS / 2097152.0)-OFF)/32768.0)/100.0;
 }
 
 //********************************************************
diff -r 3a9d37268ccd -r 83b2554646f5 ms5637.h
--- a/ms5637.h	Tue May 20 16:18:00 2014 +0000
+++ b/ms5637.h	Wed Jun 15 14:45:25 2016 +0000
@@ -86,6 +86,7 @@
 #define MS5637_CMD_ADC_1024 0x04 // ADC OSR=1024
 #define MS5637_CMD_ADC_2048 0x06 // ADC OSR=2048
 #define MS5637_CMD_ADC_4096 0x08 // ADC OSR=4096
+#define MS5637_CMD_ADC_8192 0x0A // ADC OSR=8192
 #define MS5637_CMD_PROM_RD 0xA0 // Prom read command
 
     /** Create ms5637 controller class