High resolution barometer and altimeter using i2c mode
Fork of ms5611 by
ms5611.cpp@7:90ee2168baa7, 2014-12-22 (annotated)
- Committer:
- loopsva
- Date:
- Mon Dec 22 23:38:34 2014 +0000
- Revision:
- 7:90ee2168baa7
- Parent:
- 6:41c370fa1f7b
- Child:
- 8:461f68bc94f2
Added second constructor so user can choose the polarity of the ms5611's CSB pin.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
loopsva | 0:f97f410d4a21 | 1 | //! |
loopsva | 0:f97f410d4a21 | 2 | //! @file an520_I2C.c,v |
loopsva | 0:f97f410d4a21 | 3 | //! |
loopsva | 0:f97f410d4a21 | 4 | //! Copyright (c) 2009 MEAS Switzerland |
loopsva | 0:f97f410d4a21 | 5 | //! |
loopsva | 0:f97f410d4a21 | 6 | //! |
loopsva | 0:f97f410d4a21 | 7 | //! |
loopsva | 0:f97f410d4a21 | 8 | //! @brief This C code is for starter reference only. It is written for the |
loopsva | 0:f97f410d4a21 | 9 | //! MEAS Switzerland MS56xx pressure sensor modules and Atmel Atmega644p |
loopsva | 0:f97f410d4a21 | 10 | //! microcontroller. |
loopsva | 0:f97f410d4a21 | 11 | //! |
loopsva | 0:f97f410d4a21 | 12 | //! @version 1.0 $Id: an520_I2C.c,v 1.0 |
loopsva | 0:f97f410d4a21 | 13 | //! |
loopsva | 0:f97f410d4a21 | 14 | //! @todo |
loopsva | 0:f97f410d4a21 | 15 | |
loopsva | 0:f97f410d4a21 | 16 | #include "mbed.h" |
loopsva | 0:f97f410d4a21 | 17 | #include "ms5611.h" |
loopsva | 0:f97f410d4a21 | 18 | |
loopsva | 5:0d7b229474c6 | 19 | double P; // compensated pressure value (mB) |
loopsva | 5:0d7b229474c6 | 20 | double T; // compensated temperature value (degC) |
loopsva | 5:0d7b229474c6 | 21 | double A; // altitude (ft) |
loopsva | 5:0d7b229474c6 | 22 | double S; // sea level barometer (mB) |
loopsva | 2:05804ed70748 | 23 | |
loopsva | 2:05804ed70748 | 24 | uint32_t C[8]; //coefficient storage |
loopsva | 2:05804ed70748 | 25 | |
loopsva | 0:f97f410d4a21 | 26 | //--------------------------------------------------------------------------------------------------------------------------------------// |
loopsva | 7:90ee2168baa7 | 27 | // Constructor and destructor - default to be compatible with legacy m5611 driver |
loopsva | 0:f97f410d4a21 | 28 | |
loopsva | 0:f97f410d4a21 | 29 | ms5611::ms5611(PinName sda, PinName scl) : _i2c(sda, scl) { |
loopsva | 6:41c370fa1f7b | 30 | _i2c.frequency(400000); |
loopsva | 7:90ee2168baa7 | 31 | _i2cWAddr = MS5611_ADDR_W; |
loopsva | 7:90ee2168baa7 | 32 | _i2cRAddr = MS5611_ADDR_R; |
loopsva | 7:90ee2168baa7 | 33 | } |
loopsva | 7:90ee2168baa7 | 34 | |
loopsva | 7:90ee2168baa7 | 35 | //--------------------------------------------------------------------------------------------------------------------------------------// |
loopsva | 7:90ee2168baa7 | 36 | // Constructor and destructor - new, to allow for user to select i2c address based on CSB pin |
loopsva | 7:90ee2168baa7 | 37 | |
loopsva | 7:90ee2168baa7 | 38 | ms5611::ms5611(PinName sda, PinName scl, CSBpolarity CSBpin) : _i2c(sda, scl) { |
loopsva | 7:90ee2168baa7 | 39 | _i2c.frequency(400000); |
loopsva | 7:90ee2168baa7 | 40 | _i2cWAddr = MS5611_ADDR_W; |
loopsva | 7:90ee2168baa7 | 41 | _i2cRAddr = MS5611_ADDR_R; |
loopsva | 7:90ee2168baa7 | 42 | if(CSBpin == CSBpin_1) { |
loopsva | 7:90ee2168baa7 | 43 | _i2cWAddr -= 2; |
loopsva | 7:90ee2168baa7 | 44 | _i2cRAddr -= 2; |
loopsva | 7:90ee2168baa7 | 45 | |
loopsva | 7:90ee2168baa7 | 46 | } |
loopsva | 0:f97f410d4a21 | 47 | } |
loopsva | 0:f97f410d4a21 | 48 | |
loopsva | 0:f97f410d4a21 | 49 | //******************************************************** |
loopsva | 0:f97f410d4a21 | 50 | //! @brief send I2C start condition and the address byte |
loopsva | 0:f97f410d4a21 | 51 | //! |
loopsva | 0:f97f410d4a21 | 52 | //! @return 0 |
loopsva | 0:f97f410d4a21 | 53 | //******************************************************** |
loopsva | 0:f97f410d4a21 | 54 | |
loopsva | 0:f97f410d4a21 | 55 | int ms5611::m_i2c_start(bool readMode) { |
loopsva | 0:f97f410d4a21 | 56 | int twst; |
loopsva | 0:f97f410d4a21 | 57 | _i2c.start(); |
loopsva | 0:f97f410d4a21 | 58 | if(readMode == true) { |
loopsva | 7:90ee2168baa7 | 59 | twst = m_i2c_write(_i2cRAddr); |
loopsva | 0:f97f410d4a21 | 60 | } else { |
loopsva | 7:90ee2168baa7 | 61 | twst = m_i2c_write(_i2cWAddr); |
loopsva | 0:f97f410d4a21 | 62 | } |
loopsva | 0:f97f410d4a21 | 63 | return(twst); |
loopsva | 0:f97f410d4a21 | 64 | } |
loopsva | 0:f97f410d4a21 | 65 | |
loopsva | 0:f97f410d4a21 | 66 | //******************************************************** |
loopsva | 0:f97f410d4a21 | 67 | //! @brief send I2C stop condition |
loopsva | 0:f97f410d4a21 | 68 | //! |
loopsva | 0:f97f410d4a21 | 69 | //! @return none |
loopsva | 0:f97f410d4a21 | 70 | //******************************************************** |
loopsva | 0:f97f410d4a21 | 71 | |
loopsva | 0:f97f410d4a21 | 72 | void ms5611::m_i2c_stop(void) { |
loopsva | 0:f97f410d4a21 | 73 | _i2c.stop(); |
loopsva | 0:f97f410d4a21 | 74 | } |
loopsva | 0:f97f410d4a21 | 75 | |
loopsva | 0:f97f410d4a21 | 76 | //******************************************************** |
loopsva | 0:f97f410d4a21 | 77 | //! @brief send I2C stop condition |
loopsva | 0:f97f410d4a21 | 78 | //! |
loopsva | 0:f97f410d4a21 | 79 | //! @return remote ack status |
loopsva | 0:f97f410d4a21 | 80 | //******************************************************** |
loopsva | 0:f97f410d4a21 | 81 | |
loopsva | 0:f97f410d4a21 | 82 | unsigned char ms5611::m_i2c_write(unsigned char data) { |
loopsva | 0:f97f410d4a21 | 83 | int twst = _i2c.write(data); |
loopsva | 0:f97f410d4a21 | 84 | return(twst); |
loopsva | 0:f97f410d4a21 | 85 | } |
loopsva | 0:f97f410d4a21 | 86 | |
loopsva | 0:f97f410d4a21 | 87 | //******************************************************** |
loopsva | 0:f97f410d4a21 | 88 | //! @brief read I2C byte with acknowledgment |
loopsva | 0:f97f410d4a21 | 89 | //! |
loopsva | 0:f97f410d4a21 | 90 | //! @return read byte |
loopsva | 0:f97f410d4a21 | 91 | //******************************************************** |
loopsva | 0:f97f410d4a21 | 92 | |
loopsva | 0:f97f410d4a21 | 93 | unsigned char ms5611::m_i2c_readAck(void) { |
loopsva | 0:f97f410d4a21 | 94 | int twst = _i2c.read(1); |
loopsva | 0:f97f410d4a21 | 95 | return(twst); |
loopsva | 0:f97f410d4a21 | 96 | } |
loopsva | 0:f97f410d4a21 | 97 | |
loopsva | 0:f97f410d4a21 | 98 | //******************************************************** |
loopsva | 0:f97f410d4a21 | 99 | //! @brief read I2C byte without acknowledgment |
loopsva | 0:f97f410d4a21 | 100 | //! |
loopsva | 0:f97f410d4a21 | 101 | //! @return read byte |
loopsva | 0:f97f410d4a21 | 102 | //******************************************************** |
loopsva | 0:f97f410d4a21 | 103 | |
loopsva | 0:f97f410d4a21 | 104 | unsigned char ms5611::m_i2c_readNak(void) { |
loopsva | 0:f97f410d4a21 | 105 | int twst = _i2c.read(0); |
loopsva | 0:f97f410d4a21 | 106 | return(twst); |
loopsva | 0:f97f410d4a21 | 107 | } |
loopsva | 0:f97f410d4a21 | 108 | |
loopsva | 0:f97f410d4a21 | 109 | //******************************************************** |
loopsva | 0:f97f410d4a21 | 110 | //! @brief send command using I2C hardware interface |
loopsva | 0:f97f410d4a21 | 111 | //! |
loopsva | 0:f97f410d4a21 | 112 | //! @return none |
loopsva | 0:f97f410d4a21 | 113 | //******************************************************** |
loopsva | 0:f97f410d4a21 | 114 | |
loopsva | 0:f97f410d4a21 | 115 | void ms5611::m_i2c_send(char cmd) { |
loopsva | 0:f97f410d4a21 | 116 | unsigned char ret; |
loopsva | 2:05804ed70748 | 117 | ret = m_i2c_start(false); |
loopsva | 2:05804ed70748 | 118 | if(!(ret)) { |
loopsva | 0:f97f410d4a21 | 119 | m_i2c_stop(); |
loopsva | 2:05804ed70748 | 120 | } else { |
loopsva | 0:f97f410d4a21 | 121 | ret = m_i2c_write(cmd); |
loopsva | 0:f97f410d4a21 | 122 | m_i2c_stop(); |
loopsva | 0:f97f410d4a21 | 123 | } |
loopsva | 0:f97f410d4a21 | 124 | } |
loopsva | 0:f97f410d4a21 | 125 | |
loopsva | 0:f97f410d4a21 | 126 | //******************************************************** |
loopsva | 0:f97f410d4a21 | 127 | //! @brief send reset sequence |
loopsva | 0:f97f410d4a21 | 128 | //! |
loopsva | 0:f97f410d4a21 | 129 | //! @return none |
loopsva | 0:f97f410d4a21 | 130 | //******************************************************** |
loopsva | 0:f97f410d4a21 | 131 | |
loopsva | 0:f97f410d4a21 | 132 | void ms5611::cmd_reset() { |
loopsva | 2:05804ed70748 | 133 | m_i2c_send(MS5611_CMD_RESET); |
loopsva | 2:05804ed70748 | 134 | wait_ms(4); |
loopsva | 0:f97f410d4a21 | 135 | loadCoefs(); |
loopsva | 0:f97f410d4a21 | 136 | } |
loopsva | 0:f97f410d4a21 | 137 | |
loopsva | 0:f97f410d4a21 | 138 | //******************************************************** |
loopsva | 0:f97f410d4a21 | 139 | //! @brief preform adc conversion |
loopsva | 0:f97f410d4a21 | 140 | //! |
loopsva | 0:f97f410d4a21 | 141 | //! @return 24bit result |
loopsva | 0:f97f410d4a21 | 142 | //******************************************************** |
loopsva | 0:f97f410d4a21 | 143 | |
loopsva | 0:f97f410d4a21 | 144 | unsigned long ms5611::cmd_adc(char cmd) { |
loopsva | 0:f97f410d4a21 | 145 | char cobuf[3]; |
loopsva | 0:f97f410d4a21 | 146 | cobuf[0] = 0; |
loopsva | 0:f97f410d4a21 | 147 | cobuf[1] = 0; |
loopsva | 0:f97f410d4a21 | 148 | cobuf[2] = 0; |
loopsva | 0:f97f410d4a21 | 149 | unsigned int ret; |
loopsva | 0:f97f410d4a21 | 150 | unsigned long temp = 0; |
loopsva | 2:05804ed70748 | 151 | m_i2c_send(MS5611_CMD_ADC_CONV + cmd); |
loopsva | 2:05804ed70748 | 152 | switch (cmd & 0x0f) { |
loopsva | 0:f97f410d4a21 | 153 | case MS5611_CMD_ADC_256 : wait_us(900); break; |
loopsva | 0:f97f410d4a21 | 154 | case MS5611_CMD_ADC_512 : wait_ms(3); break; |
loopsva | 0:f97f410d4a21 | 155 | case MS5611_CMD_ADC_1024: wait_ms(4); break; |
loopsva | 0:f97f410d4a21 | 156 | case MS5611_CMD_ADC_2048: wait_ms(6); break; |
loopsva | 0:f97f410d4a21 | 157 | case MS5611_CMD_ADC_4096: wait_ms(10); break; |
loopsva | 0:f97f410d4a21 | 158 | } |
loopsva | 0:f97f410d4a21 | 159 | m_i2c_send(MS5611_CMD_ADC_READ); |
loopsva | 0:f97f410d4a21 | 160 | |
loopsva | 7:90ee2168baa7 | 161 | ret = _i2c.read(_i2cRAddr, cobuf, 3, false); |
loopsva | 0:f97f410d4a21 | 162 | if(ret) printf("\n*** ms5611 ADC Read Error "); |
loopsva | 0:f97f410d4a21 | 163 | temp = (cobuf[0] << 16) + (cobuf[1] << 8) + cobuf[2]; |
loopsva | 0:f97f410d4a21 | 164 | return temp; |
loopsva | 0:f97f410d4a21 | 165 | } |
loopsva | 0:f97f410d4a21 | 166 | |
loopsva | 0:f97f410d4a21 | 167 | //******************************************************** |
loopsva | 0:f97f410d4a21 | 168 | //! @brief Read calibration coefficients |
loopsva | 0:f97f410d4a21 | 169 | //! |
loopsva | 0:f97f410d4a21 | 170 | //! @return coefficient |
loopsva | 0:f97f410d4a21 | 171 | //******************************************************** |
loopsva | 0:f97f410d4a21 | 172 | |
loopsva | 0:f97f410d4a21 | 173 | unsigned int ms5611::cmd_prom(char coef_num) { |
loopsva | 0:f97f410d4a21 | 174 | char cobuf[2]; |
loopsva | 0:f97f410d4a21 | 175 | unsigned int ret; |
loopsva | 0:f97f410d4a21 | 176 | unsigned int rC = 0; |
loopsva | 0:f97f410d4a21 | 177 | cobuf[0] = 0; |
loopsva | 0:f97f410d4a21 | 178 | cobuf[1] = 0; |
loopsva | 0:f97f410d4a21 | 179 | m_i2c_send(MS5611_CMD_PROM_RD + coef_num * 2); // send PROM READ command |
loopsva | 7:90ee2168baa7 | 180 | ret = _i2c.read(_i2cRAddr, cobuf, 2, false); |
loopsva | 0:f97f410d4a21 | 181 | if(ret) printf("\n*** ms5611 PROM Read Error "); |
loopsva | 0:f97f410d4a21 | 182 | rC = cobuf[0] * 256 + cobuf[1]; |
loopsva | 0:f97f410d4a21 | 183 | return rC; |
loopsva | 0:f97f410d4a21 | 184 | } |
loopsva | 0:f97f410d4a21 | 185 | |
loopsva | 0:f97f410d4a21 | 186 | //******************************************************** |
loopsva | 0:f97f410d4a21 | 187 | //! @brief calculate the CRC code |
loopsva | 0:f97f410d4a21 | 188 | //! |
loopsva | 0:f97f410d4a21 | 189 | //! @return crc code |
loopsva | 0:f97f410d4a21 | 190 | //******************************************************** |
loopsva | 0:f97f410d4a21 | 191 | |
loopsva | 0:f97f410d4a21 | 192 | unsigned char ms5611::crc4(unsigned int n_prom[]) { |
loopsva | 2:05804ed70748 | 193 | unsigned int n_rem; |
loopsva | 2:05804ed70748 | 194 | unsigned int crc_read; |
loopsva | 0:f97f410d4a21 | 195 | unsigned char n_bit; |
loopsva | 0:f97f410d4a21 | 196 | n_rem = 0x00; |
loopsva | 2:05804ed70748 | 197 | crc_read = n_prom[7]; |
loopsva | 2:05804ed70748 | 198 | n_prom[7]=(0xFF00 & (n_prom[7])); |
loopsva | 2:05804ed70748 | 199 | for (int cnt = 0; cnt < 16; cnt++) { |
loopsva | 0:f97f410d4a21 | 200 | if (cnt%2 == 1) { |
loopsva | 0:f97f410d4a21 | 201 | n_rem ^= (unsigned short) ((n_prom[cnt>>1]) & 0x00FF); |
loopsva | 0:f97f410d4a21 | 202 | } else { |
loopsva | 0:f97f410d4a21 | 203 | n_rem ^= (unsigned short) (n_prom[cnt>>1]>>8); |
loopsva | 0:f97f410d4a21 | 204 | } |
loopsva | 0:f97f410d4a21 | 205 | for (n_bit = 8; n_bit > 0; n_bit--) { |
loopsva | 0:f97f410d4a21 | 206 | if (n_rem & (0x8000)) { |
loopsva | 0:f97f410d4a21 | 207 | n_rem = (n_rem << 1) ^ 0x3000; |
loopsva | 0:f97f410d4a21 | 208 | } else { |
loopsva | 0:f97f410d4a21 | 209 | n_rem = (n_rem << 1); |
loopsva | 0:f97f410d4a21 | 210 | } |
loopsva | 0:f97f410d4a21 | 211 | } |
loopsva | 0:f97f410d4a21 | 212 | } |
loopsva | 2:05804ed70748 | 213 | n_rem= (0x000F & (n_rem >> 12)); |
loopsva | 2:05804ed70748 | 214 | n_prom[7]=crc_read; |
loopsva | 0:f97f410d4a21 | 215 | return (n_rem ^ 0x0); |
loopsva | 2:05804ed70748 | 216 | } |
loopsva | 2:05804ed70748 | 217 | |
loopsva | 0:f97f410d4a21 | 218 | /* |
loopsva | 0:f97f410d4a21 | 219 | The CRC code is calculated and written in factory with the LSB byte in the prom n_prom[7] set to 0x00 (see |
loopsva | 0:f97f410d4a21 | 220 | Coefficient table below). It is thus important to clear those bytes from the calculation buffer before proceeding |
loopsva | 0:f97f410d4a21 | 221 | with the CRC calculation itself: |
loopsva | 0:f97f410d4a21 | 222 | n_prom[7]=(0xFF00 & (n_prom[7])); //CRC byte is replaced by 0 |
loopsva | 0:f97f410d4a21 | 223 | As a simple test of the CRC code, the following coefficient table could be used: |
loopsva | 0:f97f410d4a21 | 224 | unsigned int nprom[] = {0x3132,0x3334,0x3536,0x3738,0x3940,0x4142,0x4344,0x4500}; |
loopsva | 0:f97f410d4a21 | 225 | the resulting calculated CRC should be 0xB. |
loopsva | 0:f97f410d4a21 | 226 | |
loopsva | 0:f97f410d4a21 | 227 | DB 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 |
loopsva | 0:f97f410d4a21 | 228 | Addr |
loopsva | 0:f97f410d4a21 | 229 | 0 16 bit reserved for manufacturer |
loopsva | 0:f97f410d4a21 | 230 | 1 Coefficient 1 (16 bit unsigned) |
loopsva | 0:f97f410d4a21 | 231 | 2 Coefficient 2 (16 bit unsigned) |
loopsva | 0:f97f410d4a21 | 232 | 3 Coefficient 3 (16 bit unsigned) |
loopsva | 0:f97f410d4a21 | 233 | 4 Coefficient 4 (16 bit unsigned) |
loopsva | 0:f97f410d4a21 | 234 | 5 Coefficient 5 (16 bit unsigned) |
loopsva | 0:f97f410d4a21 | 235 | 6 Coefficient 6 (16 bit unsigned) |
loopsva | 0:f97f410d4a21 | 236 | 7 0 0 0 0 CRC(0x0) |
loopsva | 0:f97f410d4a21 | 237 | */ |
loopsva | 0:f97f410d4a21 | 238 | /* |
loopsva | 0:f97f410d4a21 | 239 | //Returns 0x0b as per AP520_004 |
loopsva | 2:05804ed70748 | 240 | C[0] = 0x3132; |
loopsva | 2:05804ed70748 | 241 | C[1] = 0x3334; |
loopsva | 2:05804ed70748 | 242 | C[2] = 0x3536; |
loopsva | 2:05804ed70748 | 243 | C[3] = 0x3738; |
loopsva | 2:05804ed70748 | 244 | C[4] = 0x3940; |
loopsva | 2:05804ed70748 | 245 | C[5] = 0x4142; |
loopsva | 2:05804ed70748 | 246 | C[6] = 0x4344; |
loopsva | 2:05804ed70748 | 247 | C[7] = 0x4546; |
loopsva | 0:f97f410d4a21 | 248 | n_crc = ms.crc4(C); // calculate the CRC |
loopsva | 0:f97f410d4a21 | 249 | pc.printf("testing CRC: 0x%x\n", n_crc); |
loopsva | 0:f97f410d4a21 | 250 | */ |
loopsva | 2:05804ed70748 | 251 | |
loopsva | 0:f97f410d4a21 | 252 | //******************************************************** |
loopsva | 0:f97f410d4a21 | 253 | //! @brief load all calibration coefficients |
loopsva | 0:f97f410d4a21 | 254 | //! |
loopsva | 0:f97f410d4a21 | 255 | //! @return none |
loopsva | 0:f97f410d4a21 | 256 | //******************************************************** |
loopsva | 0:f97f410d4a21 | 257 | |
loopsva | 0:f97f410d4a21 | 258 | void ms5611::loadCoefs() { |
loopsva | 0:f97f410d4a21 | 259 | for (int i = 0; i < 8; i++){ |
loopsva | 0:f97f410d4a21 | 260 | wait_ms(50); |
loopsva | 2:05804ed70748 | 261 | C[i] = cmd_prom(i); |
loopsva | 0:f97f410d4a21 | 262 | } |
loopsva | 2:05804ed70748 | 263 | unsigned char n_crc = crc4(C); |
loopsva | 0:f97f410d4a21 | 264 | } |
loopsva | 0:f97f410d4a21 | 265 | |
loopsva | 0:f97f410d4a21 | 266 | //******************************************************** |
loopsva | 0:f97f410d4a21 | 267 | //! @brief calculate temperature and pressure |
loopsva | 0:f97f410d4a21 | 268 | //! |
loopsva | 0:f97f410d4a21 | 269 | //! @return none |
loopsva | 0:f97f410d4a21 | 270 | //******************************************************** |
loopsva | 0:f97f410d4a21 | 271 | |
loopsva | 0:f97f410d4a21 | 272 | void ms5611::calcPT() { |
loopsva | 2:05804ed70748 | 273 | int32_t D2 = cmd_adc(MS5611_CMD_ADC_D2 + MS5611_CMD_ADC_4096); // read D2 |
loopsva | 2:05804ed70748 | 274 | int32_t D1 = cmd_adc(MS5611_CMD_ADC_D1 + MS5611_CMD_ADC_4096); // read D1 |
loopsva | 3:c2d1b0d432ad | 275 | int64_t dT = D2 - ((uint64_t)C[5] << 8); |
loopsva | 2:05804ed70748 | 276 | int64_t OFF = ((uint32_t)C[2] << 16) + ((dT * (C[4]) >> 7)); //was OFF = (C[2] << 17) + dT * C[4] / (1 << 6); |
loopsva | 2:05804ed70748 | 277 | int64_t SENS = ((uint32_t)C[1] << 15) + ((dT * (C[3]) >> 8)); //was SENS = (C[1] << 16) + dT * C[3] / (1 << 7); |
loopsva | 2:05804ed70748 | 278 | T = (2000 + (((uint64_t)dT * C[6]) / (float)(1 << 23))) / 100; |
loopsva | 2:05804ed70748 | 279 | int32_t TEMP = 2000 + (int64_t)dT * (int64_t)C[6] / (int64_t)(1 << 23); |
loopsva | 0:f97f410d4a21 | 280 | |
loopsva | 2:05804ed70748 | 281 | if(TEMP < 2000) { // if temperature lower than 20 Celsius |
loopsva | 2:05804ed70748 | 282 | float T1 = (TEMP - 2000) * (TEMP - 2000); |
loopsva | 2:05804ed70748 | 283 | int64_t OFF1 = (5 * T1) / 2; |
loopsva | 2:05804ed70748 | 284 | int64_t SENS1 = (5 * T1) / 4; |
loopsva | 2:05804ed70748 | 285 | |
loopsva | 2:05804ed70748 | 286 | if(TEMP < -1500) { // if temperature lower than -15 Celsius |
loopsva | 2:05804ed70748 | 287 | T1 = (TEMP + 1500) * (TEMP + 1500); |
loopsva | 2:05804ed70748 | 288 | OFF1 += 7 * T1; |
loopsva | 2:05804ed70748 | 289 | SENS1 += 11 * T1 / 2; |
loopsva | 2:05804ed70748 | 290 | } |
loopsva | 2:05804ed70748 | 291 | OFF -= OFF1; |
loopsva | 2:05804ed70748 | 292 | SENS -= SENS1; |
loopsva | 2:05804ed70748 | 293 | T = (float)TEMP / 100; |
loopsva | 2:05804ed70748 | 294 | } |
loopsva | 2:05804ed70748 | 295 | // int64_t P1 = ((((int64_t)D1 * SENS) >> 21) - OFF) >> 15; |
loopsva | 2:05804ed70748 | 296 | P = ((((int64_t)D1 * SENS ) >> 21) - OFF) / (double) (1 << 15) / 100.0; |
loopsva | 0:f97f410d4a21 | 297 | } |
loopsva | 0:f97f410d4a21 | 298 | |
loopsva | 0:f97f410d4a21 | 299 | //******************************************************** |
loopsva | 0:f97f410d4a21 | 300 | //! @brief calculate temperature |
loopsva | 0:f97f410d4a21 | 301 | //! |
loopsva | 0:f97f410d4a21 | 302 | //! @return double temperature degC |
loopsva | 0:f97f410d4a21 | 303 | //******************************************************** |
loopsva | 0:f97f410d4a21 | 304 | |
loopsva | 0:f97f410d4a21 | 305 | double ms5611::calcTemp() { |
loopsva | 0:f97f410d4a21 | 306 | calcPT(); |
loopsva | 0:f97f410d4a21 | 307 | return(T); |
loopsva | 0:f97f410d4a21 | 308 | } |
loopsva | 0:f97f410d4a21 | 309 | |
loopsva | 0:f97f410d4a21 | 310 | //******************************************************** |
loopsva | 3:c2d1b0d432ad | 311 | //! @brief calculate pressure |
loopsva | 0:f97f410d4a21 | 312 | //! |
loopsva | 0:f97f410d4a21 | 313 | //! @return double barometric pressure millibar |
loopsva | 0:f97f410d4a21 | 314 | //******************************************************** |
loopsva | 0:f97f410d4a21 | 315 | |
loopsva | 0:f97f410d4a21 | 316 | double ms5611::calcPressure() { |
loopsva | 0:f97f410d4a21 | 317 | calcPT(); |
loopsva | 0:f97f410d4a21 | 318 | return(P); |
loopsva | 2:05804ed70748 | 319 | } |
loopsva | 3:c2d1b0d432ad | 320 | |
loopsva | 3:c2d1b0d432ad | 321 | //******************************************************** |
loopsva | 3:c2d1b0d432ad | 322 | //! @brief get pressure, no calculation |
loopsva | 3:c2d1b0d432ad | 323 | //! |
loopsva | 3:c2d1b0d432ad | 324 | //! @return double barometric pressure millibar |
loopsva | 3:c2d1b0d432ad | 325 | //******************************************************** |
loopsva | 3:c2d1b0d432ad | 326 | |
loopsva | 3:c2d1b0d432ad | 327 | double ms5611::getPressure() { |
loopsva | 3:c2d1b0d432ad | 328 | calcPT(); |
loopsva | 3:c2d1b0d432ad | 329 | return(P); |
loopsva | 3:c2d1b0d432ad | 330 | } |
loopsva | 3:c2d1b0d432ad | 331 | |
loopsva | 3:c2d1b0d432ad | 332 | //******************************************************** |
loopsva | 5:0d7b229474c6 | 333 | //! @brief get altitude from known sea level barometer, |
loopsva | 5:0d7b229474c6 | 334 | //! @ no pre-pressure calculation |
loopsva | 3:c2d1b0d432ad | 335 | //! |
loopsva | 3:c2d1b0d432ad | 336 | //! @enter float sea level barometer |
loopsva | 3:c2d1b0d432ad | 337 | //! @return float altitude in feet |
loopsva | 3:c2d1b0d432ad | 338 | //******************************************************** |
loopsva | 3:c2d1b0d432ad | 339 | |
loopsva | 3:c2d1b0d432ad | 340 | float ms5611::getAltitudeFT(float sea_pressure) { |
loopsva | 3:c2d1b0d432ad | 341 | A = (1 - (pow((P / (double)sea_pressure), 0.190284))) * 145366.45; |
loopsva | 3:c2d1b0d432ad | 342 | return((float)A); |
loopsva | 3:c2d1b0d432ad | 343 | } |
loopsva | 5:0d7b229474c6 | 344 | |
loopsva | 5:0d7b229474c6 | 345 | //******************************************************** |
loopsva | 5:0d7b229474c6 | 346 | //! @brief get sea level pressure from known altitude(ft), |
loopsva | 5:0d7b229474c6 | 347 | //! @ no pre-pressure calculation |
loopsva | 5:0d7b229474c6 | 348 | //! |
loopsva | 5:0d7b229474c6 | 349 | //! @enter float known altitude in feet |
loopsva | 5:0d7b229474c6 | 350 | //! @return float seal level barometer in mb |
loopsva | 5:0d7b229474c6 | 351 | //******************************************************** |
loopsva | 5:0d7b229474c6 | 352 | |
loopsva | 5:0d7b229474c6 | 353 | float ms5611::getSeaLevelBaroFT(float known_alt) { |
loopsva | 5:0d7b229474c6 | 354 | S = pow(pow((P * INHG), 0.190284) + 0.00001313 * known_alt , 5.2553026) * MB; |
loopsva | 5:0d7b229474c6 | 355 | return((float)S); |
loopsva | 5:0d7b229474c6 | 356 | } |
loopsva | 5:0d7b229474c6 | 357 | |
loopsva | 5:0d7b229474c6 | 358 | //******************************************************** |
loopsva | 5:0d7b229474c6 | 359 | //! @brief get sea level pressure from known altitude(m), |
loopsva | 5:0d7b229474c6 | 360 | //! @ no pre-pressure calculation |
loopsva | 5:0d7b229474c6 | 361 | //! |
loopsva | 5:0d7b229474c6 | 362 | //! @enter float known altitude in meters |
loopsva | 5:0d7b229474c6 | 363 | //! @return float seal level barometer in mb |
loopsva | 5:0d7b229474c6 | 364 | //******************************************************** |
loopsva | 5:0d7b229474c6 | 365 | |
loopsva | 5:0d7b229474c6 | 366 | float ms5611::getSeaLevelBaroM(float known_alt) { |
loopsva | 5:0d7b229474c6 | 367 | S = pow(pow((P * INHG), 0.190284) + 0.00001313 * known_alt * FTMETERS , 5.2553026) * MB; |
loopsva | 5:0d7b229474c6 | 368 | return((float)S); |
loopsva | 5:0d7b229474c6 | 369 | } |