High resolution barometer and altimeter using i2c mode

Dependents:   Q2_Stabi

Fork of ms5611 by Kevin Braun

Revision:
0:f97f410d4a21
Child:
1:94a46b4fed9d
diff -r 000000000000 -r f97f410d4a21 ms5611.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ms5611.h	Tue May 07 18:44:06 2013 +0000
@@ -0,0 +1,127 @@
+#ifndef MS5611_H
+#define MS5611_H
+
+#include "mbed.h"
+
+/** Software routines to access the Measurement Specialties' MS5611-01BA03 
+ *  Variometer Module using the I2C bus option.  The MS5611 is a 24 bit 
+ *  temperature and pressure transducer for high accuracy Barometer and 
+ *  Altimeter applications.  It also includes compensation coefficients
+ *  stored within the device. 
+ * 
+ *  Code adapted from Measurement Specialties:
+ *  "AN520 C-code example for MS56xx, MS57xx (except analog sensor), and 
+ *  MS58xx series pressure sensors"
+ *
+ *  Note: AN520 has not been updated for use with the MS5611.  Changes
+ *  were necessary to "calcPT()" in order to correct scaling of 
+ *  pressure readings.
+ * 
+ *  Features:
+ *          Altitude resolution to 10cm
+ *          Fast conversion down to 1 ms
+ *          Low power, 1 μA (standby < 0.15 μA)
+ *          QFN package 5.0 x 3.0 x 1.0 mm^3
+ *          Supply voltage 1.8 to 3.6 V
+ *          Integrated digital pressure sensor (24 bit DeltaSigma ADC)
+ *          Operating range: 10 to 1200 mbar, -40 to +85 °C
+ *          I2C and SPI interface up to 20 MHz
+ *          No external components (Internal oscillator)
+ *          Excellent long term stability
+ *
+ * @code
+ * #include "mbed.h"
+ * #include "ms5611.h" 
+ *
+ * ms5611 ms(p9, p10);                        // i2c pins used
+ * Serial pc(USBTX, USBRX);                   // local terminal interface
+ *
+ *
+ * int main (void) {
+ *     pc.baud(921600);                        // set up USB serial speed
+ *     i2c.frequency(400000);                  // set up i2c speed
+ *
+ *     // set up the ms5611
+ *     pc.printf("\n\nInitializing the MS5611..\n");
+ *     ms.cmd_reset();
+ *     pc.printf("Ready\n");
+ *
+ *     while(1) {
+ *         double Temp = ms.calcTemp();
+ *         double Press = ms.calcPressure();
+ *         pc.printf("Temp: %.2f degC    Barometer: %.1f mB  %.3f in/Hg\n", Temp, Press, Press * 0.0295301);
+ *         wait(2.0);
+ *     }
+ * }
+ *
+ * @endcode
+ */
+ 
+//_____ M A C R O S
+
+#define MS5611_ADDR_W 0xEE // Module address write mode
+#define MS5611_ADDR_R 0xEF // Module address read mode
+#define MS5611_CMD_RESET 0x1E // ADC reset command
+#define MS5611_CMD_ADC_READ 0x00 // ADC read command
+#define MS5611_CMD_ADC_CONV 0x40 // ADC conversion command
+#define MS5611_CMD_ADC_D1 0x00 // ADC D1 conversion
+#define MS5611_CMD_ADC_D2 0x10 // ADC D2 conversion
+#define MS5611_CMD_ADC_256 0x00 // ADC OSR=256
+#define MS5611_CMD_ADC_512 0x02 // ADC OSR=512
+#define MS5611_CMD_ADC_1024 0x04 // ADC OSR=1024
+#define MS5611_CMD_ADC_2048 0x06 // ADC OSR=2048
+#define MS5611_CMD_ADC_4096 0x08 // ADC OSR=4096
+#define MS5611_CMD_PROM_RD 0xA0 // Prom read command
+
+/* MS5611 controller class
+ */
+class ms5611{
+
+public:
+    /** Create a MS5611 object using the specified I2C object
+     *
+     * @param constructor, - the I2C object to communicate with
+     */
+    ms5611(PinName sda, PinName scl);
+    /** Initialize the MS5611 and set up the coefficients
+     *    First - reset the MS5611
+     *    Second - load coefficient values from the MS5611 PROM
+     *    Third  - calculate coefficient checksum
+     *  This routine only needs to be run once at boot up
+     *
+     * @param NONE
+     */
+    void cmd_reset();
+    /** Calculate and return compensated temperature
+     *    Returns double temperature in degC
+     *
+     * @param NONE
+     */
+    double calcTemp();
+    /** Calculate and return compensated barometric pressure
+     *    Returns double pressure in millibars
+     *
+     * @param NONE
+     */
+    double calcPressure();
+    
+private:
+    int m_i2c_start(bool readMode);
+    void m_i2c_stop(void);
+    unsigned char m_i2c_write(unsigned char data);
+    unsigned char m_i2c_readAck(void);
+    unsigned char m_i2c_readNak(void);
+    void m_i2c_send(char cmd);
+    void loadCoefs();
+    unsigned long cmd_adc(char cmd);
+    unsigned int cmd_prom(char coef_num);
+    unsigned char crc4(unsigned int n_prom[]);
+    void calcPT();
+    unsigned int PTbuffer[8];       // calibration coefficients
+  
+protected:
+    I2C     _i2c;
+    
+
+}; 
+#endif