Fixed Incorrect pressure readings for the barometer

Fork of MS5637 by chris stevens

Committer:
cstevens
Date:
Tue May 20 16:13:09 2014 +0000
Revision:
7:2e8bc98f3dda
mss5561_needs calibration adjustment

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cstevens 7:2e8bc98f3dda 1 #ifndef MS5561H
cstevens 7:2e8bc98f3dda 2 #define MS5561_H
cstevens 7:2e8bc98f3dda 3
cstevens 7:2e8bc98f3dda 4 #include "mbed.h"
cstevens 7:2e8bc98f3dda 5
cstevens 7:2e8bc98f3dda 6
cstevens 7:2e8bc98f3dda 7 #define SEA_PRESS 1013.25 //default sea level pressure level in mb
cstevens 7:2e8bc98f3dda 8 #define KNOWNALT 327.0 //default known altitude, 5200 Franklin Dr., 94588
cstevens 7:2e8bc98f3dda 9 #define INHG 0.02952998751 //convert mb to in/Hg constant
cstevens 7:2e8bc98f3dda 10 #define MB 33.8638815 //convert in/Hg to mb constant
cstevens 7:2e8bc98f3dda 11 #define FTMETERS 0.3048 //convert feet to meters
cstevens 7:2e8bc98f3dda 12
cstevens 7:2e8bc98f3dda 13
cstevens 7:2e8bc98f3dda 14 /** Software routines to access the Measurement Specialties' MS5611-01BA03
cstevens 7:2e8bc98f3dda 15 * Variometer Module using the I2C bus option. The MS5611 is a 24 bit
cstevens 7:2e8bc98f3dda 16 * temperature and pressure transducer for high accuracy Barometer and
cstevens 7:2e8bc98f3dda 17 * Altimeter applications. It also includes compensation coefficients
cstevens 7:2e8bc98f3dda 18 * stored within the device.
cstevens 7:2e8bc98f3dda 19 *
cstevens 7:2e8bc98f3dda 20 * Code adapted from Measurement Specialties:
cstevens 7:2e8bc98f3dda 21 * "AN520 C-code example for MS56xx, MS57xx (except analog sensor), and
cstevens 7:2e8bc98f3dda 22 * MS58xx series pressure sensors"
cstevens 7:2e8bc98f3dda 23 *
cstevens 7:2e8bc98f3dda 24 * Note: AN520 has not been updated for use with the MS5611. Changes
cstevens 7:2e8bc98f3dda 25 * were necessary to "calcPT()" in order to correct scaling of
cstevens 7:2e8bc98f3dda 26 * pressure readings.
cstevens 7:2e8bc98f3dda 27 *
cstevens 7:2e8bc98f3dda 28 * Features:
cstevens 7:2e8bc98f3dda 29 * Altitude resolution to 10cm
cstevens 7:2e8bc98f3dda 30 * Fast conversion down to 1 ms
cstevens 7:2e8bc98f3dda 31 * Low power, 1 μA (standby < 0.15 μA)
cstevens 7:2e8bc98f3dda 32 * QFN package 5.0 x 3.0 x 1.0 mm^3
cstevens 7:2e8bc98f3dda 33 * Supply voltage 1.8 to 3.6 V
cstevens 7:2e8bc98f3dda 34 * Integrated digital pressure sensor (24 bit DeltaSigma ADC)
cstevens 7:2e8bc98f3dda 35 * Operating range: 10 to 1200 mbar, -40 to +85 °C
cstevens 7:2e8bc98f3dda 36 * I2C and SPI interface up to 20 MHz
cstevens 7:2e8bc98f3dda 37 * No external components (Internal oscillator)
cstevens 7:2e8bc98f3dda 38 * Excellent long term stability
cstevens 7:2e8bc98f3dda 39 *
cstevens 7:2e8bc98f3dda 40 * @code
cstevens 7:2e8bc98f3dda 41 * #include "mbed.h"
cstevens 7:2e8bc98f3dda 42 * #include "ms5611.h"
cstevens 7:2e8bc98f3dda 43 *
cstevens 7:2e8bc98f3dda 44 * ms5611 ms(p9, p10); // i2c pins used
cstevens 7:2e8bc98f3dda 45 * Serial pc(USBTX, USBRX); // local terminal interface
cstevens 7:2e8bc98f3dda 46 *
cstevens 7:2e8bc98f3dda 47 *
cstevens 7:2e8bc98f3dda 48 * int main (void) {
cstevens 7:2e8bc98f3dda 49 * pc.baud(921600); // set up USB serial speed
cstevens 7:2e8bc98f3dda 50 *
cstevens 7:2e8bc98f3dda 51 * // set up the ms5611
cstevens 7:2e8bc98f3dda 52 * pc.printf("\n\nInitializing the MS5611..\n");
cstevens 7:2e8bc98f3dda 53 * ms.cmd_reset();
cstevens 7:2e8bc98f3dda 54 * pc.printf("Ready\n");
cstevens 7:2e8bc98f3dda 55 *
cstevens 7:2e8bc98f3dda 56 * while(1) {
cstevens 7:2e8bc98f3dda 57 * double Temp = ms.calcTemp(); //calculate press and temp, then returns current temperature in degC
cstevens 7:2e8bc98f3dda 58 * double Press = ms.calcPressure(); //calculate press and temp, then returns current pressure in mb
cstevens 7:2e8bc98f3dda 59 * double GetPress = ms.getPressure(); //returns current pressure in mb. Does no calculations. Ususally done after calcTemp()
cstevens 7:2e8bc98f3dda 60 * double Altitude = ms.getAltitudeFT(1013.25); //enter pressure at sea level in mb, returns altitude in feet
cstevens 7:2e8bc98f3dda 61 * double PressSeaLvlFT = ms.getSeaLevelBaroFT(327.2); //enter known altitude in feet, returns sea level pressure in mb
cstevens 7:2e8bc98f3dda 62 * double PressSeaLvlM = ms.getAltitudeFT(99.73); //enter known altitude in meters, returns seal level pressure in mb
cstevens 7:2e8bc98f3dda 63 *
cstevens 7:2e8bc98f3dda 64 * pc.printf("Temp: %.2f degC\n", Temp);
cstevens 7:2e8bc98f3dda 65 * pc.printf("Barometer: %.1f mB %.3f in/Hg\n", Press, Press * 0.0295301);
cstevens 7:2e8bc98f3dda 66 * pc.printf("Alt: %.1f ft\n", Altitude);
cstevens 7:2e8bc98f3dda 67 * pc.printf("Sea_Lvl: %.1f ft %.2f m\n", PressSeaLvlFT, PressSeaLvlM);
cstevens 7:2e8bc98f3dda 68 * wait(2.0);
cstevens 7:2e8bc98f3dda 69 * }
cstevens 7:2e8bc98f3dda 70 * }
cstevens 7:2e8bc98f3dda 71 *
cstevens 7:2e8bc98f3dda 72 * @endcode
cstevens 7:2e8bc98f3dda 73 */
cstevens 7:2e8bc98f3dda 74
cstevens 7:2e8bc98f3dda 75 //_____ M A C R O S
cstevens 7:2e8bc98f3dda 76
cstevens 7:2e8bc98f3dda 77 #define MS5611_ADDR_W 0xEC // Module address write mode CHANGED FOR 5561 ORIGINAL 0Xee
cstevens 7:2e8bc98f3dda 78 #define MS5611_ADDR_R 0xED // Module address read modeCHANGED FOR 5561 ORIGINAL 0XeF
cstevens 7:2e8bc98f3dda 79 #define MS5611_CMD_RESET 0x1E // ADC reset command
cstevens 7:2e8bc98f3dda 80 #define MS5611_CMD_ADC_READ 0x00 // ADC read command
cstevens 7:2e8bc98f3dda 81 #define MS5611_CMD_ADC_CONV 0x40 // ADC conversion command
cstevens 7:2e8bc98f3dda 82 #define MS5611_CMD_ADC_D1 0x00 // ADC D1 conversion
cstevens 7:2e8bc98f3dda 83 #define MS5611_CMD_ADC_D2 0x10 // ADC D2 conversion
cstevens 7:2e8bc98f3dda 84 #define MS5611_CMD_ADC_256 0x00 // ADC OSR=256
cstevens 7:2e8bc98f3dda 85 #define MS5611_CMD_ADC_512 0x02 // ADC OSR=512
cstevens 7:2e8bc98f3dda 86 #define MS5611_CMD_ADC_1024 0x04 // ADC OSR=1024
cstevens 7:2e8bc98f3dda 87 #define MS5611_CMD_ADC_2048 0x06 // ADC OSR=2048
cstevens 7:2e8bc98f3dda 88 #define MS5611_CMD_ADC_4096 0x08 // ADC OSR=4096
cstevens 7:2e8bc98f3dda 89 #define MS5611_CMD_PROM_RD 0xA0 // Prom read command
cstevens 7:2e8bc98f3dda 90
cstevens 7:2e8bc98f3dda 91 /** Create ms5611 controller class
cstevens 7:2e8bc98f3dda 92 *
cstevens 7:2e8bc98f3dda 93 * @param ms5611 class
cstevens 7:2e8bc98f3dda 94 *
cstevens 7:2e8bc98f3dda 95 */
cstevens 7:2e8bc98f3dda 96 class ms5611 {
cstevens 7:2e8bc98f3dda 97
cstevens 7:2e8bc98f3dda 98 public:
cstevens 7:2e8bc98f3dda 99 /** Create a MS5611 object using the specified I2C object
cstevens 7:2e8bc98f3dda 100 *
cstevens 7:2e8bc98f3dda 101 * @param constructor, - the I2C object to communicate with
cstevens 7:2e8bc98f3dda 102 */
cstevens 7:2e8bc98f3dda 103 ms5611(PinName sda, PinName scl);
cstevens 7:2e8bc98f3dda 104 /** Initialize the MS5611 and set up the coefficients
cstevens 7:2e8bc98f3dda 105 * First - reset the MS5611
cstevens 7:2e8bc98f3dda 106 * Second - load coefficient values from the MS5611 PROM
cstevens 7:2e8bc98f3dda 107 * Third - calculate coefficient checksum
cstevens 7:2e8bc98f3dda 108 * This routine only needs to be run once at boot up
cstevens 7:2e8bc98f3dda 109 *
cstevens 7:2e8bc98f3dda 110 * @param NONE
cstevens 7:2e8bc98f3dda 111 */
cstevens 7:2e8bc98f3dda 112 void cmd_reset();
cstevens 7:2e8bc98f3dda 113 /** Calculate and return compensated temperature
cstevens 7:2e8bc98f3dda 114 * Returns double temperature in degC
cstevens 7:2e8bc98f3dda 115 *
cstevens 7:2e8bc98f3dda 116 * @param NONE
cstevens 7:2e8bc98f3dda 117 */
cstevens 7:2e8bc98f3dda 118 double calcTemp();
cstevens 7:2e8bc98f3dda 119 /** Calculate and return compensated barometric pressure
cstevens 7:2e8bc98f3dda 120 * Returns double pressure in millibars
cstevens 7:2e8bc98f3dda 121 *
cstevens 7:2e8bc98f3dda 122 * @param NONE
cstevens 7:2e8bc98f3dda 123 */
cstevens 7:2e8bc98f3dda 124 double calcPressure();
cstevens 7:2e8bc98f3dda 125 /** Return compensated barometric pressure
cstevens 7:2e8bc98f3dda 126 * Returns double pressure in millibars
cstevens 7:2e8bc98f3dda 127 * DOES NOT RE-CALCULATE FIRST!!!
cstevens 7:2e8bc98f3dda 128 * Saves time if you calcTemp(); first
cstevens 7:2e8bc98f3dda 129 *
cstevens 7:2e8bc98f3dda 130 * @param NONE
cstevens 7:2e8bc98f3dda 131 */
cstevens 7:2e8bc98f3dda 132 double getPressure();
cstevens 7:2e8bc98f3dda 133 /** Calculate and returns altitude in feet
cstevens 7:2e8bc98f3dda 134 * Returns float altitude in feet
cstevens 7:2e8bc98f3dda 135 *
cstevens 7:2e8bc98f3dda 136 * @param float known pressure (mB) at sea level
cstevens 7:2e8bc98f3dda 137 */
cstevens 7:2e8bc98f3dda 138 float getAltitudeFT(float sea_pressure);
cstevens 7:2e8bc98f3dda 139 /** Calculate and returns sea level baro
cstevens 7:2e8bc98f3dda 140 * Returns float seal level barometer in feet
cstevens 7:2e8bc98f3dda 141 *
cstevens 7:2e8bc98f3dda 142 * @param float known altitude in feet
cstevens 7:2e8bc98f3dda 143 */
cstevens 7:2e8bc98f3dda 144 float getSeaLevelBaroFT(float known_alt);
cstevens 7:2e8bc98f3dda 145 /** Calculate and returns sea level baro
cstevens 7:2e8bc98f3dda 146 * Returns float seal level barometer in meters
cstevens 7:2e8bc98f3dda 147 *
cstevens 7:2e8bc98f3dda 148 * @param float known altitude in meters
cstevens 7:2e8bc98f3dda 149 */
cstevens 7:2e8bc98f3dda 150 float getSeaLevelBaroM(float known_alt);
cstevens 7:2e8bc98f3dda 151
cstevens 7:2e8bc98f3dda 152 private:
cstevens 7:2e8bc98f3dda 153 int m_i2c_start(bool readMode);
cstevens 7:2e8bc98f3dda 154 void m_i2c_stop(void);
cstevens 7:2e8bc98f3dda 155 unsigned char m_i2c_write(unsigned char data);
cstevens 7:2e8bc98f3dda 156 unsigned char m_i2c_readAck(void);
cstevens 7:2e8bc98f3dda 157 unsigned char m_i2c_readNak(void);
cstevens 7:2e8bc98f3dda 158 void m_i2c_send(char cmd);
cstevens 7:2e8bc98f3dda 159 void loadCoefs();
cstevens 7:2e8bc98f3dda 160 unsigned long cmd_adc(char cmd);
cstevens 7:2e8bc98f3dda 161 unsigned int cmd_prom(char coef_num);
cstevens 7:2e8bc98f3dda 162 unsigned char crc4(unsigned n_prom[]);
cstevens 7:2e8bc98f3dda 163 void calcPT();
cstevens 7:2e8bc98f3dda 164 unsigned int PTbuffer[8]; // calibration coefficients
cstevens 7:2e8bc98f3dda 165
cstevens 7:2e8bc98f3dda 166 protected:
cstevens 7:2e8bc98f3dda 167 I2C _i2c;
cstevens 7:2e8bc98f3dda 168
cstevens 7:2e8bc98f3dda 169
cstevens 7:2e8bc98f3dda 170 };
cstevens 7:2e8bc98f3dda 171 #endif