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