High resolution barometer and altimeter using i2c mode

Dependents:   Q2_Stabi

Fork of ms5611 by Kevin Braun

Committer:
loopsva
Date:
Mon May 13 20:12:15 2013 +0000
Revision:
3:c2d1b0d432ad
Parent:
2:05804ed70748
Child:
4:f7b0ad1b5751
Fixed pressure error <20C. Added altitude calculation.

Who changed what in which revision?

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