David Summers / Mbed 2 deprecated MS5611

Dependencies:   mbed

Revision:
5:630aefee388b
Parent:
4:e0dbf2bdb967
Child:
6:be9a6932c869
--- a/main.cpp	Sat Mar 17 04:43:32 2018 +0000
+++ b/main.cpp	Fri Mar 30 11:14:51 2018 +0000
@@ -1,5 +1,35 @@
 #include "mbed.h"
 #include "main.h"
+/* This code is to drive the Measurement Specialties MS5611-01BA03
+ * pressure sensor. The sensor measures pressure via piezo electric
+ * sensor, that is measured by a 24bit delta-sigma ADC. Temperure is
+ * also measured via the same ADC.
+ * 
+ * Pressure is then calculated via a first order equation in this code
+ * where both the pressure offset and linear term are also both linear
+ * dependent on temperature.
+ * 
+ * Higher order terms are avaiable, but are not included in this code
+ * at present.
+ *
+ * The sensor can be accessed by either SPI or I2C, this code uses SPI.
+ *
+ * The algorithms are based on the MS5611-01BA03 data sheet, and also
+ * AN520
+ *
+ * Some details are not clear in the documents, these are:
+ * . The commands are 7bit, the 8th bit is a stop bit and must be zero
+ * . The sensor will reply to the command with 0xfe, e.g. has it own stop bit
+ * . If 0xfe is not receieved error condition has happened
+ * . This starts the command, but does not complete it
+ * . Chip Select (CS) must be pulled high (inactive) after every command
+ * . The linear cooeficients are help in the PROM
+ * . This has a CRC4 check sum
+ * . The CRC4 check sum is based on the polymonial 0x13=x^4+x+1
+ * . The CRC4 acts on the first 7 PROM values each 16bit,
+ * . The 8th PROM value, the CRC acts on the 12 MSB of the register
+ * . In addition in requires bits 9-12 to be zeroed in the check sum  
+ */
 
 Serial pc(SERIAL_TX, SERIAL_RX, 115200); // tx, rx
 //DigitalOut led(LED_RED);
@@ -11,7 +41,7 @@
 
 int main()
 {
-    int PROM[8];
+    uint16_t PROM[8];
     uint32_t D1,D2;
     double T,P;
     cs.write(1); // disable all SPI
@@ -29,18 +59,18 @@
     // 3 - 2048
     // 4 - 4096
     MS5611(&D1,&D2,4);
-    pc.printf("%i\t%i\t",D1,D2);
+//    pc.printf("%i\t%i\t",D1,D2);
 
     MS5611_phys(D1,D2,PROM,&T,&P);
 
-    pc.printf("%f\t",T/100.0);
-    pc.printf("%f\r\n",P/100.0);
-
+//    pc.printf("%f\t",T/100.0);
+//    pc.printf("%f\r\n",P/100.0);
+    pc.printf("%i\t%i\t%f\t%f\r\n",D1,D2,T/100,P/100.0);
     wait(1.0);
     }
 }
 
-void MS5611_init(int *PROM)
+void MS5611_init(uint16_t *PROM)
 {
     uint32_t crc=0;
     spi.format(8,0); // 8 bit mode 0 - this is default anyway but good to code.
@@ -57,10 +87,9 @@
         if (spi.write(0xA0|(i<<1))!=0xfe) {
             pc.printf("Error reading prom(%i)\r\n",i);
         }
-        register uint8_t promh=spi.write(0x00);
-        register uint8_t proml=spi.write(0x00);
+        PROM[i]=((uint16_t) spi.write(0x00))<<8;
+        PROM[i]|=((uint16_t) spi.write(0x00));
         cs.write(1); // disable CS to finish the command
-        PROM[i]=((promh<<8)|(proml));
         crc|=(i==7)?(PROM[i]&0xff00):PROM[i]; // Note AN520 gives this bit mask
         for (int j=0; j<16; j++) {
             crc=crc<<1;
@@ -117,11 +146,27 @@
     cs.write(1); // and terminate the command
 }
 
-void MS5611_phys(uint32_t D1,uint32_t D2, int *PROM, double *T, double *P)
+void MS5611_phys(uint32_t D1,uint32_t D2,uint16_t *PROM, double *T, double *P)
 {
     int64_t dt=((int64_t) D2)-(((int64_t) PROM[5])<<8);
     *T=2000.0+((double) dt*PROM[6])/8388608.0;
     int64_t off128=(((int64_t) PROM[2])<<23)+(((int64_t) PROM[4])*dt);
     int64_t sens256=(((int64_t) PROM[1])<<23)+(((int64_t) PROM[3])*dt);
     *P=(((double) D1)*((double) sens256)/4194304.0-((double) off128))/4194304.0;
+/* this keeps the power expansion - result not diffferent from above
+  Hence the above doesn't have rouding problems.
+    Pp[0]=
+    -((double) PROM[2])*2.0+((double) PROM[4])*((double) PROM[5])/16384.0
+    +((double) D1)*( ((double) PROM[1])*32768.0
+        -((double) PROM[3])*((double) PROM[5]))/68719476736.0
+    -((double) PROM[4])*((double) D2)/4194304.0
+    +((double) D1)*((double) PROM[3])*((double) D2)/17592186044416.0;
+ * this breaks it down by terms
+    Pp[0]=-((double) PROM[2])*2.0+((double) PROM[4])*((double) PROM[5])/16384.0;
+    Pp[1]=+((double) D1)*( ((double) PROM[1])*32768.0
+        -((double) PROM[3])*((double) PROM[5]))/68719476736.0;
+    Pp[2]=-((double) PROM[4])*((double) D2)/4194304.0;
+    Pp[3]=+((double) D1)*((double) PROM[3])*((double) D2)/17592186044416.0;
+*/
+
 }
\ No newline at end of file