David Summers / Mbed 2 deprecated MS5611

Dependencies:   mbed

Revision:
3:a8c38c3fe967
Parent:
2:b6a0d4ba24a1
Child:
4:e0dbf2bdb967
--- a/main.cpp	Thu Mar 15 17:07:42 2018 +0000
+++ b/main.cpp	Fri Mar 16 18:35:06 2018 +0000
@@ -20,6 +20,8 @@
     for (int i=0; i<8; i++) {
         pc.printf("Prom(%i): %i\r\n",i,PROM[i]);
     }
+    
+    while (1) {
     // the third paramater gives over samping
     // 0 - 256
     // 1 - 512
@@ -34,11 +36,13 @@
     pc.printf("Temperature = %f deg C\r\n",T/100.0);
     pc.printf("Pressure = %f mbar\r\n",P/100.0);
 
+    wait(10.0);
+    }
 }
 
 void MS5611_init(int *PROM)
 {
-    long crc=0;
+    uint32_t crc=0;
     spi.format(8,0); // 8 bit mode 0 - this is default anyway but good to code.
     spi.frequency(1000000); // 1MHz is this the minimum?
     cs.write(0); // Enable the MS5611 intercae
@@ -57,11 +61,11 @@
         register uint8_t proml=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];
+        crc|=(i==7)?(PROM[i]&0xff00):PROM[i]; // Note AN520 gives this bit mask
         for (int j=0; j<16; j++) {
             crc=crc<<1;
             if (crc&0x1000000) { // implimt the CRC4 algorithm 0x13 -
-                crc^=0x1300000; // to my mind this is implimented wrong - but its how the MS5611 does it
+                crc^=0x1300000; // This won't do the last 4 bits of PROM7
             }
         }
         wait_us(10); // Pause probably not stricly needed, but after switching CSB high give a bit of time.
@@ -89,6 +93,9 @@
     cs.write(1); // Disable the MS5611 intercae
     wait_us(600*(1<<os)); // pause for read, longer when oversampling
     cs.write(0); // Enable the MS5611 intercae
+    if(spi.write(0x00)!=0xfe) { // can I have the result?;
+        pc.printf("Error reading D1\r\n");
+    }
     register uint8_t d1h=spi.write(0x00);
     register uint8_t d1m=spi.write(0x00);
     register uint8_t d1l=spi.write(0x00);
@@ -101,6 +108,9 @@
     cs.write(1); // Disable the MS5611 intercae
     wait_us(600*(1<<os)); // pause for read, longer when oversampling
     cs.write(0); // Enable the MS5611 intercae
+    if(spi.write(0x00)!=0xfe) { // can I have the result?;
+        pc.printf("Error reading D2\r\n");
+    }
     register uint8_t d2h=spi.write(0x00);
     register uint8_t d2m=spi.write(0x00);
     register uint8_t d2l=spi.write(0x00);
@@ -111,9 +121,9 @@
 
 void MS5611_phys(uint32_t D1,uint32_t D2, int *PROM, double *T, double *P)
 {
-    int64_t dt=D2-(PROM[5]<<8);
+    int64_t dt=((int64_t) D2)-(((int64_t) PROM[5])<<8);
     *T=2000.0+((double) dt*PROM[6])/8388608.0;
-    int64_t off128=(PROM[2]<<23)+PROM[4]*dt;
-    int64_t sens256=(PROM[1]<<23)+PROM[3]*dt;
-    *P=(((double) D1*sens256)/536870912.0-((double) off128)/128.0)/ 4194304.0;
+    int64_t off=(((int64_t) PROM[2])<<16)+((((int64_t) PROM[4])*dt)>>7);
+    int64_t sens=(((int64_t) PROM[1])<<15)+((((int64_t) PROM[3])*dt)>>8);
+    *P=(((double) D1)*((double) sens)/2097152.0-((double) off))/32768.0;
 }
\ No newline at end of file