Oxford CWM Team / MS5637

Fork of MS5637 by chris stevens

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ms5637.h Source File

ms5637.h

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