David Summers / Mbed 2 deprecated MS5611

Dependencies:   mbed

Revision:
2:b6a0d4ba24a1
Parent:
1:3295382ddc81
Child:
3:a8c38c3fe967
--- a/main.cpp	Wed Mar 14 21:04:25 2018 +0000
+++ b/main.cpp	Thu Mar 15 17:07:42 2018 +0000
@@ -12,37 +12,108 @@
 int main()
 {
     int PROM[8];
+    uint32_t D1,D2;
+    double T,P;
     cs.write(1); // disable all SPI
-    pc.printf("Hello World! 0\r\n");
+    pc.printf("Hello World!\r\n");
     MS5611_init(PROM);
     for (int i=0; i<8; i++) {
-        pc.printf("Prom(%i): %x\r\n",i,PROM[i]);
+        pc.printf("Prom(%i): %i\r\n",i,PROM[i]);
     }
+    // the third paramater gives over samping
+    // 0 - 256
+    // 1 - 512
+    // 2 - 1024
+    // 3 - 2048
+    // 4 - 4096
+    MS5611(&D1,&D2,4);
+    pc.printf("D1 = %i\r\nD2 = %i\r\n",D1,D2);
+
+    MS5611_phys(D1,D2,PROM,&T,&P);
+
+    pc.printf("Temperature = %f deg C\r\n",T/100.0);
+    pc.printf("Pressure = %f mbar\r\n",P/100.0);
+
 }
 
 void MS5611_init(int *PROM)
 {
+    long 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
-    pc.printf("Reset result - expect fe : %x\r\n",spi.write(0x1E)); // reset the device
+    if (spi.write(0x1E)!=0xfe) {
+        pc.printf("Error reseting the device\r\n");
+    }
     wait_ms(3); // give time for ROM to reload 2.8ms by the spec
+    cs.write(1); // close the connection - does it finish the command?
+    wait_us(10); // Pause after putting CSB high
     for (int i=0; i<8; i++) {
-        pc.printf("s: %x r: %x ",0xA0|(i<<1),spi.write(0xA0|(i<<1)));
+        cs.write(0); // enable the SC to start a command.
+        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);
+        cs.write(1); // disable CS to finish the command
         PROM[i]=((promh<<8)|(proml));
-        pc.printf("%i: Code=%x : Reply=%x\r\n",i,0xA0|(i<<1),PROM[i]);
-        wait(1);
+        crc|=(i==7)?(PROM[i]&0xff00):PROM[i];
+        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
+            }
+        }
+        wait_us(10); // Pause probably not stricly needed, but after switching CSB high give a bit of time.
     }
-    cs.write(1); // and disable the interace
+    if ((crc>>20)!=(PROM[7]&0xf))
+        pc.printf("CRC check sum: %x vs recorded %x\r\n",crc>>20,PROM[7]&0xf);
 }
 
-
-
-void MS5611(long *D1, long *D2)
+void MS5611(uint32_t *D1, uint32_t *D2, int os)
 {
     //  cs.write(0);
     spi.format(8,0);
     spi.frequency(1000000);
+    cs.write(0); // Enable the MS5611 intercae
+    if (spi.write(0x1E)!=0xfe) { // and reset the device
+        pc.printf("Error reseting the device\r\n");
+    }
+    wait_ms(3); // give time for ROM to reload 2.8ms by the spec
+    cs.write(1); // close the connection - does it finish the command?
+    wait_us(10); // Pause after putting CSB high
+    cs.write(0); // Enable the MS5611 intercae
+    if (spi.write(0x40|(os<<1))!=0xfe) { // D1 please
+        pc.printf("Error asking for D1\r\n");
+    }
+    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
+    register uint8_t d1h=spi.write(0x00);
+    register uint8_t d1m=spi.write(0x00);
+    register uint8_t d1l=spi.write(0x00);
+    cs.write(1); // and terminate the command
+    wait_us(10); // Pause after putting CSB high
+    cs.write(0); // Enable the MS5611 intercae
+    if (spi.write(0x50|(os<<1))!=0xfe) { // D2 please
+        pc.printf("Error asking for D2\r\n");
+    }
+    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
+    register uint8_t d2h=spi.write(0x00);
+    register uint8_t d2m=spi.write(0x00);
+    register uint8_t d2l=spi.write(0x00);
+    cs.write(1); // and terminate the command
+    *D1=((uint32_t) d1h<<16)|((uint32_t) d1m<<8)|((uint32_t) d1l);
+    *D2=((uint32_t) d2h<<16)|((uint32_t) d2m<<8)|((uint32_t) d2l);
+}
+
+void MS5611_phys(uint32_t D1,uint32_t D2, int *PROM, double *T, double *P)
+{
+    int64_t dt=D2-(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;
 }
\ No newline at end of file