High resolution barometer and altimeter using i2c mode

Dependents:   Q2_Stabi

Fork of ms5611 by Kevin Braun

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     /** Create a MS5611 object using the specified I2C object
00109      *   - User fixed I2C address 0xEE, CSB pin = 0
00110      *   - This is the default legacy constructor
00111      * @param sda - mbed I2C interface pin
00112      * @param scl - mbed I2C interface pin
00113      */
00114     ms5611(PinName sda, PinName scl);
00115     /** Create a MS5611 object using the specified I2C object
00116      *   - User defined use of the CSB pin
00117      *   - CSB pin = 0, user set I2C address to 0xEE
00118      *   - CSB pin = 1, user set I2C address to 0xEC 
00119      * @param sda - mbed I2C interface pin
00120      * @param scl - mbed I2C interface pin
00121      * @param ms5611::CSBpin_0 - CSB pin tied to ground
00122      * @param ms5611::CSBpin_1 - CSB pin tied to VDD
00123      */
00124     ms5611(PinName sda, PinName scl, CSBpolarity CSBpin);
00125     /** Initialize the MS5611 and set up the coefficients
00126      *    First - reset the MS5611
00127      *    Second - load coefficient values from the MS5611 PROM
00128      *    Third  - calculate coefficient checksum
00129      *  This routine only needs to be run once at boot up
00130      *
00131      * @param NONE
00132      */
00133     void cmd_reset();
00134     /** Calculate and return compensated temperature
00135      *    Returns double temperature in degC
00136      *
00137      * @param NONE
00138      */
00139     double calcTemp();
00140     /** Calculate and return compensated barometric pressure
00141      *    Returns double pressure in millibars
00142      *
00143      * @param NONE
00144      */
00145     double calcPressure();
00146     /** Return compensated barometric pressure
00147      *    Returns double pressure in millibars
00148      *    DOES NOT RE-CALCULATE FIRST!!!
00149      *    Saves time if you calcTemp(); first
00150      *
00151      * @param NONE
00152      */
00153     double getPressure();
00154     /** Calculate and returns altitude in feet
00155      *    Returns float altitude in feet
00156      *
00157      * @param float known pressure (mB) at sea level
00158      */
00159     float getAltitude();
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     uint8_t calcPT();
00175 
00176     
00177 private:
00178 #if not defined  MS5611i2cLOWLEVEL
00179     char cobuf[3];
00180 #endif
00181     uint8_t _i2cWAddr;
00182     uint8_t _i2cRAddr;
00183     int m_i2c_start(bool readMode);
00184     void m_i2c_stop();
00185     uint8_t m_i2c_write(uint8_t data);
00186     uint8_t m_i2c_readAck();
00187     uint8_t m_i2c_readNak();
00188     void m_i2c_send(uint8_t cmd);
00189     void loadCoefs();
00190     uint64_t cmd_adc(uint8_t cmd);
00191     uint32_t cmd_prom(uint8_t coef_num);
00192     uint8_t crc4(uint32_t  n_prom[]);
00193     uint32_t PTbuffer[8];       // calibration coefficients
00194 
00195     uint8_t step;
00196     int32_t D1, D2;
00197   
00198 protected:
00199     I2C     _i2c;
00200     
00201 
00202 }; 
00203 #endif