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