High resolution barometer and altimeter using i2c mode

Dependents:   upverter_fitbit_clone ReadingMag_HMC5883L_work

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ms5611.h Source File

ms5611.h

00001 #ifndef MS5611_H
00002 #define MS5611_H
00003 
00004 #include "mbed.h"
00005 
00006 //#define MS5611i2cLOWLEVEL   1               //if the use of low-level I2C routines is needed
00007 //#warning "MS5611 using low level I2C routines"
00008 
00009 #define SEA_PRESS   1013.25                 //default sea level pressure level in mb
00010 #define KNOWNALT    327.0                   //default known altitude, 5200 Franklin Dr., 94588
00011 #define INHG        0.02952998751           //convert mb to in/Hg constant
00012 #define MB          33.8638815              //convert in/Hg to mb constant
00013 #define FTMETERS    0.3048                  //convert feet to meters
00014 
00015 
00016 /** Software routines to access the Measurement Specialties' MS5611-01BA03 
00017  *  Variometer Module using the I2C bus option.  The MS5611 is a 24 bit 
00018  *  temperature and pressure transducer for high accuracy Barometer and 
00019  *  Altimeter applications.  It also includes compensation coefficients
00020  *  stored within the device. 
00021  * 
00022  *  Code adapted from Measurement Specialties:
00023  *  "AN520 C-code example for MS56xx, MS57xx (except analog sensor), and 
00024  *  MS58xx series pressure sensors"
00025  *
00026  *  Note: AN520 has not been updated for use with the MS5611.  Changes
00027  *  were necessary to "calcPT()" in order to correct scaling of 
00028  *  pressure readings.
00029  * 
00030  *  Features:
00031  *          Altitude resolution to 10cm
00032  *          Fast conversion down to 1 ms
00033  *          Low power, 1 μA (standby < 0.15 μA)
00034  *          QFN package 5.0 x 3.0 x 1.0 mm^3
00035  *          Supply voltage 1.8 to 3.6 V
00036  *          Integrated digital pressure sensor (24 bit DeltaSigma ADC)
00037  *          Operating range: 10 to 1200 mbar, -40 to +85 °C
00038  *          I2C and SPI interface up to 20 MHz
00039  *          No external components (Internal oscillator)
00040  *          Excellent long term stability
00041  *
00042  * @code
00043  * #include "mbed.h"
00044  * #include "ms5611.h" 
00045  *
00046  * //ms5611 ms(p9, p10);                        // i2c pins used
00047  * ms5611 ms(p9, p10, ms5611::CSBpin_0);      // NEW!! with rev 7. User can set polarity of CSB pin
00048  * //ms5611 ms(p9, p10, ms5611::CSBpin_1);
00049  *
00050  * Serial pc(USBTX, USBRX);                   // local terminal interface
00051  *
00052  *
00053  * int main (void) {
00054  *     pc.baud(921600);                        // set up USB serial speed
00055  *
00056  *     // set up the ms5611
00057  *     pc.printf("\n\nInitializing the MS5611..\n");
00058  *     ms.cmd_reset();
00059  *     pc.printf("Ready\n");
00060  *
00061  *     while(1) {
00062  *         double Temp = ms.calcTemp();                         //calculate press and temp, then returns current temperature in degC
00063  *         double Press = ms.calcPressure();                    //calculate press and temp, then returns current pressure in mb
00064  *         double GetPress = ms.getPressure();                  //returns current pressure in mb. Does no calculations.  Ususally done after calcTemp()
00065  *         double Altitude = ms.getAltitudeFT(1013.25);         //enter pressure at sea level in mb, returns altitude in feet
00066  *         double PressSeaLvlFT = ms.getSeaLevelBaroFT(327.2);  //enter known altitude in feet, returns sea level pressure in mb
00067  *         double PressSeaLvlM = ms.getAltitudeFT(99.73);       //enter known altitude in meters, returns seal level pressure in mb
00068  *
00069  *         pc.printf("Temp: %.2f degC\n", Temp);    
00070  *         pc.printf("Barometer: %.1f mB  %.3f in/Hg\n", Press, Press * 0.0295301);
00071  *         pc.printf("Alt: %.1f ft\n", Altitude);
00072  *         pc.printf("Sea_Lvl: %.1f ft   %.2f m\n", PressSeaLvlFT, PressSeaLvlM);
00073  *         wait(2.0);
00074  *     }
00075  * }
00076  *
00077  * @endcode
00078  */
00079  
00080 //_____ M A C R O S
00081 
00082 #define MS5611_ADDR_W 0xEE // Module address write mode (CSBpin = 0);
00083 #define MS5611_ADDR_R 0xEF // Module address read mode
00084 #define MS5611_CMD_RESET 0x1E // ADC reset command
00085 #define MS5611_CMD_ADC_READ 0x00 // ADC read command
00086 #define MS5611_CMD_ADC_CONV 0x40 // ADC conversion command
00087 #define MS5611_CMD_ADC_D1 0x00 // ADC D1 conversion
00088 #define MS5611_CMD_ADC_D2 0x10 // ADC D2 conversion
00089 #define MS5611_CMD_ADC_256 0x00 // ADC OSR=256
00090 #define MS5611_CMD_ADC_512 0x02 // ADC OSR=512
00091 #define MS5611_CMD_ADC_1024 0x04 // ADC OSR=1024
00092 #define MS5611_CMD_ADC_2048 0x06 // ADC OSR=2048
00093 #define MS5611_CMD_ADC_4096 0x08 // ADC OSR=4096
00094 #define MS5611_CMD_PROM_RD 0xA0 // Prom read command
00095 
00096     /** Create ms5611 controller class
00097      *
00098      * @param ms5611 class
00099      *
00100      */
00101 class ms5611 {
00102 
00103 public:
00104     enum CSBpolarity {
00105         CSBpin_0,  //CSB pin is grounded, I2C address is 0xEE and 0xEF
00106         CSBpin_1,  //CSB pin is tied to Vdd, I2C address is 0xEC and 0xED
00107     };
00108     uint32_t C[8];
00109     /** Create a MS5611 object using the specified I2C object
00110      *   - User fixed I2C address 0xEE, CSB pin = 0
00111      *   - This is the default legacy constructor
00112      * @param sda - mbed I2C interface pin
00113      * @param scl - mbed I2C interface pin
00114      */
00115     ms5611(PinName sda, PinName scl);
00116     /** Create a MS5611 object using the specified I2C object
00117      *   - User defined use of the CSB pin
00118      *   - CSB pin = 0, user set I2C address to 0xEE
00119      *   - CSB pin = 1, user set I2C address to 0xEC 
00120      * @param sda - mbed I2C interface pin
00121      * @param scl - mbed I2C interface pin
00122      * @param ms5611::CSBpin_0 - CSB pin tied to ground
00123      * @param ms5611::CSBpin_1 - CSB pin tied to VDD
00124      */
00125     ms5611(PinName sda, PinName scl, CSBpolarity CSBpin);
00126     /** Initialize the MS5611 and set up the coefficients
00127      *    First - reset the MS5611
00128      *    Second - load coefficient values from the MS5611 PROM
00129      *    Third  - calculate coefficient checksum
00130      *  This routine only needs to be run once at boot up
00131      *
00132      * @param NONE
00133      */
00134     void cmd_reset();
00135     /** Calculate and return compensated temperature
00136      *    Returns double temperature in degC
00137      *
00138      * @param NONE
00139      */
00140     double calcTemp();
00141     /** Calculate and return compensated barometric pressure
00142      *    Returns double pressure in millibars
00143      *
00144      * @param NONE
00145      */
00146     double calcPressure();
00147     /** Return compensated barometric pressure
00148      *    Returns double pressure in millibars
00149      *    DOES NOT RE-CALCULATE FIRST!!!
00150      *    Saves time if you calcTemp(); first
00151      *
00152      * @param NONE
00153      */
00154     double getPressure();
00155     /** Calculate and returns altitude in feet
00156      *    Returns float altitude in feet
00157      *
00158      * @param float known pressure (mB) at sea level
00159      */
00160     float getAltitudeFT(float sea_pressure);
00161     /** Calculate and returns sea level baro
00162      *    Returns float seal level barometer in feet
00163      *
00164      * @param float known altitude in feet
00165      */
00166     float getSeaLevelBaroFT(float known_alt);
00167     /** Calculate and returns sea level baro
00168      *    Returns float seal level barometer in meters
00169      *
00170      * @param float known altitude in meters
00171      */
00172     float getSeaLevelBaroM(float known_alt);
00173     
00174 private:
00175 #if not defined  MS5611i2cLOWLEVEL
00176     char cobuf[3];
00177 #endif
00178     uint8_t _i2cWAddr;
00179     uint8_t _i2cRAddr;
00180     int m_i2c_start(bool readMode);
00181     void m_i2c_stop();
00182     uint8_t m_i2c_write(uint8_t data);
00183     uint8_t m_i2c_readAck();
00184     uint8_t m_i2c_readNak();
00185     void m_i2c_send(uint8_t cmd);
00186     void loadCoefs();
00187     uint64_t cmd_adc(uint8_t cmd);
00188     uint32_t cmd_prom(uint8_t coef_num);
00189     uint8_t crc4(uint32_t  n_prom[]);
00190     void calcPT();
00191     uint32_t PTbuffer[8];       // calibration coefficients
00192   
00193 protected:
00194     I2C     _i2c;
00195     
00196 
00197 }; 
00198 #endif