High resolution barometer and altimeter using i2c mode
Fork of ms5611 by
Diff: ms5611.h
- Revision:
- 9:6104e8cdb3ec
- Parent:
- 8:461f68bc94f2
--- a/ms5611.h Tue Apr 28 23:36:22 2015 +0000 +++ b/ms5611.h Tue Jul 14 16:19:59 2015 +0000 @@ -3,18 +3,20 @@ #include "mbed.h" -#if(defined(TARGET_KL25Z) )//|| defined(TARGET_K64F)) +//IMPORTANT NOTE: First attempt at also using the MS5805. This code is untested!! + +// #define MS5611i2cLOWLEVEL 1 //if the use of low-level I2C routines is needed +// #warning "MS5611 using low level I2C routines" +// IMPORTANT NOTE: On the K64F, "ack/nack", returns from single byte i2c.write(int data) are opposite +// of i2c.write(int address, const char *data, int length, bool repeated=false) - #define MS5611i2cLOWLEVEL 1 //if the use of low-level I2C routines is needed - #warning "MS5611 using low level I2C routines" - -#endif - -#define SEA_PRESS 1013.25 //default sea level pressure level in mb -#define KNOWNALT 327.0 //default known altitude, 5200 Franklin Dr., 94588 -#define INHG 0.02952998751 //convert mb to in/Hg constant -#define MB 33.8638815 //convert in/Hg to mb constant -#define FTMETERS 0.3048 //convert feet to meters +#define SEA_PRESS 1013.25 //default sea level pressure level in mb +#define KNOWNALT 327.0 //default known altitude, 5200 Franklin Dr., 94588 +#define DEGC_DEGF_FLOAT 9.0 / 5.0 + 32.0//convert degrees C to degrees F +#define METERS_FEET 3.2808399 //convert meters to feet +#define FEET_METERS 0.3048006 //convert feet to meters +#define MB_INHG_DOUBLE 0.02952998751 //convert millibars to inches_of_mercury +#define INHG_MB_DOUBLE 33.8638815767 //convert inches_of_mercury to millibars /** Software routines to access the Measurement Specialties' MS5611-01BA03 @@ -46,34 +48,40 @@ * @code * #include "mbed.h" * #include "ms5611.h" - * - * //ms5611 ms(p9, p10); // i2c pins used - * ms5611 ms(p9, p10, ms5611::CSBpin_0); // NEW!! with rev 7. User can set polarity of CSB pin - * //ms5611 ms(p9, p10, ms5611::CSBpin_1); - * - * Serial pc(USBTX, USBRX); // local terminal interface - * - * + * + * #define SDA PTE25 + * #define SCL PTE24 + * + * //ms5611 ms(SDA, SCL); // i2c pins used, legacy default constructor + * ms5611 ms(SDA, SCL, ms5611::CSBpin_0); // NEW!! with rev 7. User can set polarity of CSB pin + * //ms5611 ms(SDA, SCL, ms5611::CSBpin_1); + * //ms5611 ms(SDA, SCL, ms5611::MS5805); // NEW!! with rev 8. Allow use of MS5805 as well + * + * Serial pc(USBTX, USBRX); // local terminal interface + * * int main (void) { - * pc.baud(921600); // set up USB serial speed - * + * pc.baud(230400); // set up USB serial speed + * * // set up the ms5611 - * pc.printf("\n\nInitializing the MS5611..\n"); - * ms.cmd_reset(); + * pc.printf("\n\nInitializing the MS5611..\r\n"); + * int msFlag = ms.cmd_reset(); + * if(!(msFlag)) { + * pc.printf("MS5611 I2C error!!\r\n"); + * return(-1); + * } * pc.printf("Ready\n"); - * + * * while(1) { * double Temp = ms.calcTemp(); //calculate press and temp, then returns current temperature in degC * double Press = ms.calcPressure(); //calculate press and temp, then returns current pressure in mb * double GetPress = ms.getPressure(); //returns current pressure in mb. Does no calculations. Ususally done after calcTemp() * double Altitude = ms.getAltitudeFT(1013.25); //enter pressure at sea level in mb, returns altitude in feet * double PressSeaLvlFT = ms.getSeaLevelBaroFT(327.2); //enter known altitude in feet, returns sea level pressure in mb - * double PressSeaLvlM = ms.getAltitudeFT(99.73); //enter known altitude in meters, returns seal level pressure in mb - * - * pc.printf("Temp: %.2f degC\n", Temp); - * pc.printf("Barometer: %.1f mB %.3f in/Hg\n", Press, Press * 0.0295301); - * pc.printf("Alt: %.1f ft\n", Altitude); - * pc.printf("Sea_Lvl: %.1f ft %.2f m\n", PressSeaLvlFT, PressSeaLvlM); + * + * pc.printf("Temp: %6.2f degC %6.2f degF\r\n", Temp, Temp * 9.0 / 5.0 + 32.0); + * pc.printf("Barometer: %7.2f mB %8.3f in/Hg\r\n", Press, Press * MB_INHG_DOUBLE); + * pc.printf("Sea_Lvl: %7.2f mB %8.3f in/Hg\r\n", PressSeaLvlFT, PressSeaLvlFT * MB_INHG_DOUBLE); + * pc.printf("Alt: %7.2f m %7.2f ft\r\n\r\n", Altitude, Altitude * FEET_METERS); * wait(2.0); * } * } @@ -83,19 +91,20 @@ //_____ M A C R O S -#define MS5611_ADDR_W 0xEE // Module address write mode (CSBpin = 0); -#define MS5611_ADDR_R 0xEF // Module address read mode -#define MS5611_CMD_RESET 0x1E // ADC reset command -#define MS5611_CMD_ADC_READ 0x00 // ADC read command -#define MS5611_CMD_ADC_CONV 0x40 // ADC conversion command -#define MS5611_CMD_ADC_D1 0x00 // ADC D1 conversion -#define MS5611_CMD_ADC_D2 0x10 // ADC D2 conversion -#define MS5611_CMD_ADC_256 0x00 // ADC OSR=256 -#define MS5611_CMD_ADC_512 0x02 // ADC OSR=512 -#define MS5611_CMD_ADC_1024 0x04 // ADC OSR=1024 -#define MS5611_CMD_ADC_2048 0x06 // ADC OSR=2048 -#define MS5611_CMD_ADC_4096 0x08 // ADC OSR=4096 -#define MS5611_CMD_PROM_RD 0xA0 // Prom read command +#define MS5611_ADDR_W 0xEE // Module address write mode (CSBpin = 0); +#define MS5611_ADDR_R 0xEF // Module address read mode +#define MS5611_CMD_RESET 0x1E // ADC reset command +#define MS5611_CMD_ADC_READ 0x00 // ADC read command +#define MS5611_CMD_ADC_CONV 0x40 // ADC conversion command +#define MS5611_CMD_ADC_D1 0x00 // ADC D1 conversion +#define MS5611_CMD_ADC_D2 0x10 // ADC D2 conversion +#define MS5611_CMD_ADC_256 0x00 // ADC OSR=256 +#define MS5611_CMD_ADC_512 0x02 // ADC OSR=512 +#define MS5611_CMD_ADC_1024 0x04 // ADC OSR=1024 +#define MS5611_CMD_ADC_2048 0x06 // ADC OSR=2048 +#define MS5611_CMD_ADC_4096 0x08 // ADC OSR=4096 +#define MS5805_CMD_ADC_8192 0x0A // ADC OSR=8192 +#define MS5611_CMD_PROM_RD 0xA0 // Prom read command /** Create ms5611 controller class * @@ -106,16 +115,20 @@ public: enum CSBpolarity { - CSBpin_0, //CSB pin is grounded, I2C address is 0xEE and 0xEF - CSBpin_1, //CSB pin is tied to Vdd, I2C address is 0xEC and 0xED + CSBpin_0, //CSB pin is grounded, I2C address is 0xEE and 0xEF + CSBpin_1, //CSB pin is tied to Vdd, I2C address is 0xEC and 0xED + MS5805, //MS5805 fixed at 0EC and 0xED }; + /** Create a MS5611 object using the specified I2C object * - User fixed I2C address 0xEE, CSB pin = 0 * - This is the default legacy constructor * @param sda - mbed I2C interface pin * @param scl - mbed I2C interface pin + * @return NONE */ ms5611(PinName sda, PinName scl); + /** Create a MS5611 object using the specified I2C object * - User defined use of the CSB pin * - CSB pin = 0, user set I2C address to 0xEE @@ -124,53 +137,56 @@ * @param scl - mbed I2C interface pin * @param ms5611::CSBpin_0 - CSB pin tied to ground * @param ms5611::CSBpin_1 - CSB pin tied to VDD + * @param ms5611::MS5805 - MS5805 has only one i2c address + * @return NONE */ ms5611(PinName sda, PinName scl, CSBpolarity CSBpin); + /** Initialize the MS5611 and set up the coefficients - * First - reset the MS5611 - * Second - load coefficient values from the MS5611 PROM - * Third - calculate coefficient checksum - * This routine only needs to be run once at boot up - * + * - First - reset the MS5611 + * - Second - load coefficient values from the MS5611 PROM + * - Third - calculate coefficient checksum + * - This routine only needs to be run once at boot up * @param NONE + * @return int I2C ack/nack status */ - void cmd_reset(); - /** Calculate and return compensated temperature - * Returns double temperature in degC - * + int cmd_reset(); + + /** Calculates compensated temperature * @param NONE + * @return double temperature in degC */ double calcTemp(); - /** Calculate and return compensated barometric pressure - * Returns double pressure in millibars - * + + /** Calculates compensated barometric pressure * @param NONE + * @return double pressure in millibars */ double calcPressure(); - /** Return compensated barometric pressure - * Returns double pressure in millibars - * DOES NOT RE-CALCULATE FIRST!!! - * Saves time if you calcTemp(); first - * + + /** Get compensated barometric pressure + * - DOES NOT RE-CALCULATE FIRST!!! + * - Result only valid if you calcTemp(); first * @param NONE + * @eturn double pressure in millibars */ double getPressure(); - /** Calculate and returns altitude in feet - * Returns float altitude in feet - * + + /** Calculates altitude in feet * @param float known pressure (mB) at sea level + * @return float altitude in feet */ float getAltitudeFT(float sea_pressure); - /** Calculate and returns sea level baro - * Returns float seal level barometer in feet - * + + /** Calculates sea level baro * @param float known altitude in feet + * @return float sea level barometer in feet */ float getSeaLevelBaroFT(float known_alt); - /** Calculate and returns sea level baro - * Returns float seal level barometer in meters - * + + /** Calculates sea level baro * @param float known altitude in meters + * @return float sea level barometer in meters */ float getSeaLevelBaroM(float known_alt); @@ -178,11 +194,12 @@ char _i2cWAddr; char _i2cRAddr; int m_i2c_start(bool readMode); + int MStype; void m_i2c_stop(void); unsigned char m_i2c_write(unsigned char data); unsigned char m_i2c_readAck(void); unsigned char m_i2c_readNak(void); - void m_i2c_send(char cmd); + int m_i2c_send(char cmd); void loadCoefs(); unsigned long cmd_adc(char cmd); unsigned int cmd_prom(char coef_num);