2014-10-19 06:54:35 -04:00
|
|
|
//
|
|
|
|
// FILE: MS5611.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2014-10-19 07:01:09 -04:00
|
|
|
// Erni - testing/fixes
|
2023-11-14 10:25:27 -05:00
|
|
|
// VERSION: 0.4.0
|
|
|
|
// PURPOSE: Arduino library for MS5611 temperature and pressure sensor
|
2021-01-29 06:31:58 -05:00
|
|
|
// URL: https://github.com/RobTillaart/MS5611
|
2014-10-19 06:54:35 -04:00
|
|
|
//
|
2022-10-27 10:52:08 -04:00
|
|
|
// HISTORY see changelog.md
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2014-10-19 06:54:35 -04:00
|
|
|
|
|
|
|
#include "MS5611.h"
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2020-11-27 05:20:37 -05:00
|
|
|
// datasheet page 10
|
|
|
|
#define MS5611_CMD_READ_ADC 0x00
|
|
|
|
#define MS5611_CMD_READ_PROM 0xA0
|
|
|
|
#define MS5611_CMD_RESET 0x1E
|
|
|
|
#define MS5611_CMD_CONVERT_D1 0x40
|
|
|
|
#define MS5611_CMD_CONVERT_D2 0x50
|
|
|
|
|
2014-10-19 06:54:35 -04:00
|
|
|
|
|
|
|
/////////////////////////////////////////////////////
|
|
|
|
//
|
2022-10-27 10:52:08 -04:00
|
|
|
// PUBLIC
|
2014-10-19 06:54:35 -04:00
|
|
|
//
|
2023-11-14 10:25:27 -05:00
|
|
|
MS5611::MS5611(uint8_t deviceAddress, TwoWire * wire)
|
2014-10-19 06:54:35 -04:00
|
|
|
{
|
2022-01-16 04:03:30 -05:00
|
|
|
_address = deviceAddress;
|
2023-11-14 10:25:27 -05:00
|
|
|
_wire = wire;
|
2022-01-16 04:03:30 -05:00
|
|
|
_samplingRate = OSR_ULTRA_LOW;
|
|
|
|
_temperature = MS5611_NOT_READ;
|
|
|
|
_pressure = MS5611_NOT_READ;
|
|
|
|
_result = MS5611_NOT_READ;
|
|
|
|
_lastRead = 0;
|
2022-01-23 08:56:48 -05:00
|
|
|
_deviceID = 0;
|
2022-01-16 04:03:30 -05:00
|
|
|
_pressureOffset = 0;
|
|
|
|
_temperatureOffset = 0;
|
2022-01-25 09:26:38 -05:00
|
|
|
_compensation = true;
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-11-14 10:25:27 -05:00
|
|
|
bool MS5611::begin()
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
|
|
|
if ((_address < 0x76) || (_address > 0x77)) return false;
|
|
|
|
if (! isConnected()) return false;
|
|
|
|
|
2022-01-25 09:26:38 -05:00
|
|
|
return reset();
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool MS5611::isConnected()
|
|
|
|
{
|
|
|
|
_wire->beginTransmission(_address);
|
2022-01-16 04:03:30 -05:00
|
|
|
#ifdef ARDUINO_ARCH_NRF52840
|
|
|
|
// needed for NANO 33 BLE
|
|
|
|
_wire->write(0);
|
|
|
|
#endif
|
2021-01-29 06:31:58 -05:00
|
|
|
return (_wire->endTransmission() == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-27 10:52:08 -04:00
|
|
|
bool MS5611::reset(uint8_t mathMode)
|
2014-10-19 06:54:35 -04:00
|
|
|
{
|
2020-11-27 05:20:37 -05:00
|
|
|
command(MS5611_CMD_RESET);
|
2022-01-16 04:03:30 -05:00
|
|
|
uint32_t start = micros();
|
2022-10-27 10:52:08 -04:00
|
|
|
|
|
|
|
// while loop prevents blocking RTOS
|
2022-01-16 04:03:30 -05:00
|
|
|
while (micros() - start < 2800)
|
|
|
|
{
|
|
|
|
yield();
|
|
|
|
delayMicroseconds(10);
|
|
|
|
}
|
2022-10-27 10:52:08 -04:00
|
|
|
|
|
|
|
// initialize the C[] array
|
|
|
|
initConstants(mathMode);
|
|
|
|
|
|
|
|
// read factory calibrations from EEPROM.
|
2022-01-25 09:26:38 -05:00
|
|
|
bool ROM_OK = true;
|
2017-07-26 17:51:37 -04:00
|
|
|
for (uint8_t reg = 0; reg < 7; reg++)
|
|
|
|
{
|
2022-10-27 10:52:08 -04:00
|
|
|
// used indices match datasheet.
|
|
|
|
// C[0] == manufacturer - read but not used;
|
|
|
|
// C[7] == CRC - skipped.
|
2022-01-23 08:56:48 -05:00
|
|
|
uint16_t tmp = readProm(reg);
|
|
|
|
C[reg] *= tmp;
|
2022-10-27 10:52:08 -04:00
|
|
|
// _deviceID is a SHIFT XOR merge of 7 PROM registers, reasonable unique
|
2022-01-23 08:56:48 -05:00
|
|
|
_deviceID <<= 4;
|
|
|
|
_deviceID ^= tmp;
|
2022-10-27 10:52:08 -04:00
|
|
|
// Serial.println(readProm(reg));
|
2022-01-25 09:26:38 -05:00
|
|
|
if (reg > 0)
|
|
|
|
{
|
|
|
|
ROM_OK = ROM_OK && (tmp != 0);
|
|
|
|
}
|
2017-07-26 17:51:37 -04:00
|
|
|
}
|
2022-01-25 09:26:38 -05:00
|
|
|
return ROM_OK;
|
2014-10-19 06:54:35 -04:00
|
|
|
}
|
|
|
|
|
2020-11-27 05:20:37 -05:00
|
|
|
|
2014-10-19 06:54:35 -04:00
|
|
|
int MS5611::read(uint8_t bits)
|
|
|
|
{
|
2022-10-27 10:52:08 -04:00
|
|
|
// VARIABLES NAMES BASED ON DATASHEET
|
|
|
|
// ALL MAGIC NUMBERS ARE FROM DATASHEET
|
2020-11-27 05:20:37 -05:00
|
|
|
|
|
|
|
convert(MS5611_CMD_CONVERT_D1, bits);
|
2017-07-26 17:51:37 -04:00
|
|
|
if (_result) return _result;
|
2022-10-27 10:52:08 -04:00
|
|
|
// NOTE: D1 and D2 seem reserved in MBED (NANO BLE)
|
2022-01-04 15:01:14 -05:00
|
|
|
uint32_t _D1 = readADC();
|
2017-07-26 17:51:37 -04:00
|
|
|
if (_result) return _result;
|
|
|
|
|
2020-11-27 05:20:37 -05:00
|
|
|
convert(MS5611_CMD_CONVERT_D2, bits);
|
2017-07-26 17:51:37 -04:00
|
|
|
if (_result) return _result;
|
2022-01-04 15:01:14 -05:00
|
|
|
uint32_t _D2 = readADC();
|
2017-07-26 17:51:37 -04:00
|
|
|
if (_result) return _result;
|
2022-01-14 07:34:56 -05:00
|
|
|
|
2022-10-27 10:52:08 -04:00
|
|
|
// Serial.println(_D1);
|
|
|
|
// Serial.println(_D2);
|
2022-01-25 09:26:38 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
// TEST VALUES - comment lines above
|
2022-10-27 10:52:08 -04:00
|
|
|
// uint32_t _D1 = 9085466;
|
|
|
|
// uint32_t _D2 = 8569150;
|
2017-07-26 17:51:37 -04:00
|
|
|
|
2022-10-27 10:52:08 -04:00
|
|
|
// TEMP & PRESS MATH - PAGE 7/20
|
2022-01-04 15:01:14 -05:00
|
|
|
float dT = _D2 - C[5];
|
2017-07-26 17:51:37 -04:00
|
|
|
_temperature = 2000 + dT * C[6];
|
|
|
|
|
|
|
|
float offset = C[2] + dT * C[4];
|
|
|
|
float sens = C[1] + dT * C[3];
|
|
|
|
|
2022-01-25 09:26:38 -05:00
|
|
|
if (_compensation)
|
2017-07-26 17:51:37 -04:00
|
|
|
{
|
2022-10-27 10:52:08 -04:00
|
|
|
// SECOND ORDER COMPENSATION - PAGE 8/20
|
|
|
|
// COMMENT OUT < 2000 CORRECTION IF NOT NEEDED
|
|
|
|
// NOTE TEMPERATURE IS IN 0.01 C
|
2022-01-25 09:26:38 -05:00
|
|
|
if (_temperature < 2000)
|
2014-10-19 07:02:42 -04:00
|
|
|
{
|
2022-01-25 09:26:38 -05:00
|
|
|
float T2 = dT * dT * 4.6566128731E-10;
|
|
|
|
float t = (_temperature - 2000) * (_temperature - 2000);
|
|
|
|
float offset2 = 2.5 * t;
|
|
|
|
float sens2 = 1.25 * t;
|
2022-10-27 10:52:08 -04:00
|
|
|
// COMMENT OUT < -1500 CORRECTION IF NOT NEEDED
|
2022-01-25 09:26:38 -05:00
|
|
|
if (_temperature < -1500)
|
|
|
|
{
|
|
|
|
t = (_temperature + 1500) * (_temperature + 1500);
|
|
|
|
offset2 += 7 * t;
|
|
|
|
sens2 += 5.5 * t;
|
|
|
|
}
|
|
|
|
_temperature -= T2;
|
|
|
|
offset -= offset2;
|
|
|
|
sens -= sens2;
|
2014-10-19 07:02:42 -04:00
|
|
|
}
|
2022-10-27 10:52:08 -04:00
|
|
|
// END SECOND ORDER COMPENSATION
|
2017-07-26 17:51:37 -04:00
|
|
|
}
|
2014-10-19 07:02:42 -04:00
|
|
|
|
2022-01-04 15:01:14 -05:00
|
|
|
_pressure = (_D1 * sens * 4.76837158205E-7 - offset) * 3.051757813E-5;
|
2014-10-19 07:00:16 -04:00
|
|
|
|
2020-11-27 05:20:37 -05:00
|
|
|
_lastRead = millis();
|
|
|
|
return MS5611_READ_OK;
|
2014-10-19 06:54:35 -04:00
|
|
|
}
|
|
|
|
|
2014-10-19 07:00:16 -04:00
|
|
|
|
2021-12-24 06:14:45 -05:00
|
|
|
void MS5611::setOversampling(osr_t samplingRate)
|
|
|
|
{
|
|
|
|
_samplingRate = (uint8_t) samplingRate;
|
|
|
|
}
|
|
|
|
|
2022-01-16 04:03:30 -05:00
|
|
|
|
|
|
|
float MS5611::getTemperature() const
|
|
|
|
{
|
|
|
|
if (_temperatureOffset == 0) return _temperature * 0.01;
|
|
|
|
return _temperature * 0.01 + _temperatureOffset;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
float MS5611::getPressure() const
|
|
|
|
{
|
|
|
|
if (_pressureOffset == 0) return _pressure * 0.01;
|
|
|
|
return _pressure * 0.01 + _pressureOffset;
|
|
|
|
};
|
|
|
|
|
2022-10-27 10:52:08 -04:00
|
|
|
// EXPERIMENTAL
|
|
|
|
uint16_t MS5611::getManufacturer()
|
|
|
|
{
|
|
|
|
return readProm(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// EXPERIMENTAL
|
|
|
|
uint16_t MS5611::getSerialCode()
|
|
|
|
{
|
|
|
|
return readProm(7) >> 4;
|
|
|
|
}
|
|
|
|
|
2022-01-16 04:03:30 -05:00
|
|
|
|
2014-10-19 06:54:35 -04:00
|
|
|
/////////////////////////////////////////////////////
|
|
|
|
//
|
2022-10-27 10:52:08 -04:00
|
|
|
// PRIVATE
|
2014-10-19 06:54:35 -04:00
|
|
|
//
|
2017-07-26 17:51:37 -04:00
|
|
|
void MS5611::convert(const uint8_t addr, uint8_t bits)
|
2014-10-19 06:54:35 -04:00
|
|
|
{
|
2022-10-27 10:52:08 -04:00
|
|
|
// values from page 3 datasheet - MAX column (rounded up)
|
2022-01-16 04:03:30 -05:00
|
|
|
uint16_t del[5] = {600, 1200, 2300, 4600, 9100};
|
|
|
|
|
|
|
|
uint8_t index = bits;
|
|
|
|
if (index < 8) index = 8;
|
|
|
|
else if (index > 12) index = 12;
|
|
|
|
index -= 8;
|
|
|
|
uint8_t offset = index * 2;
|
2017-07-26 17:51:37 -04:00
|
|
|
command(addr + offset);
|
2022-01-16 04:03:30 -05:00
|
|
|
|
|
|
|
uint16_t waitTime = del[index];
|
|
|
|
uint32_t start = micros();
|
2022-10-27 10:52:08 -04:00
|
|
|
// while loop prevents blocking RTOS
|
2022-01-16 04:03:30 -05:00
|
|
|
while (micros() - start < waitTime)
|
|
|
|
{
|
|
|
|
yield();
|
|
|
|
delayMicroseconds(10);
|
|
|
|
}
|
2014-10-19 06:54:35 -04:00
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2014-10-19 06:54:35 -04:00
|
|
|
uint16_t MS5611::readProm(uint8_t reg)
|
|
|
|
{
|
2022-10-27 10:52:08 -04:00
|
|
|
// last EEPROM register is CRC - Page 13 datasheet.
|
2020-11-27 05:20:37 -05:00
|
|
|
uint8_t promCRCRegister = 7;
|
|
|
|
if (reg > promCRCRegister) return 0;
|
|
|
|
|
2017-07-26 17:51:37 -04:00
|
|
|
uint8_t offset = reg * 2;
|
2020-11-27 05:20:37 -05:00
|
|
|
command(MS5611_CMD_READ_PROM + offset);
|
2017-07-26 17:51:37 -04:00
|
|
|
if (_result == 0)
|
|
|
|
{
|
2022-01-16 04:03:30 -05:00
|
|
|
uint8_t length = 2;
|
|
|
|
int bytes = _wire->requestFrom(_address, length);
|
|
|
|
if (bytes >= length)
|
2014-10-19 06:54:35 -04:00
|
|
|
{
|
2022-01-16 04:03:30 -05:00
|
|
|
uint16_t value = _wire->read() * 256;
|
|
|
|
value += _wire->read();
|
|
|
|
return value;
|
2014-10-19 06:54:35 -04:00
|
|
|
}
|
|
|
|
return 0;
|
2017-07-26 17:51:37 -04:00
|
|
|
}
|
|
|
|
return 0;
|
2014-10-19 06:54:35 -04:00
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2014-10-19 07:02:42 -04:00
|
|
|
uint32_t MS5611::readADC()
|
2014-10-19 06:54:35 -04:00
|
|
|
{
|
2020-11-27 05:20:37 -05:00
|
|
|
command(MS5611_CMD_READ_ADC);
|
2017-07-26 17:51:37 -04:00
|
|
|
if (_result == 0)
|
|
|
|
{
|
2022-01-16 04:03:30 -05:00
|
|
|
uint8_t length = 3;
|
|
|
|
int bytes = _wire->requestFrom(_address, length);
|
|
|
|
if (bytes >= length)
|
2014-10-19 06:54:35 -04:00
|
|
|
{
|
2022-01-16 04:03:30 -05:00
|
|
|
uint32_t value = _wire->read() * 65536UL;
|
|
|
|
value += _wire->read() * 256UL;
|
|
|
|
value += _wire->read();
|
|
|
|
return value;
|
2014-10-19 06:54:35 -04:00
|
|
|
}
|
2015-03-06 14:25:48 -05:00
|
|
|
return 0UL;
|
2017-07-26 17:51:37 -04:00
|
|
|
}
|
|
|
|
return 0UL;
|
2014-10-19 06:54:35 -04:00
|
|
|
}
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
int MS5611::command(const uint8_t command)
|
2014-10-19 06:54:35 -04:00
|
|
|
{
|
2020-11-27 05:20:37 -05:00
|
|
|
yield();
|
2021-01-29 06:31:58 -05:00
|
|
|
_wire->beginTransmission(_address);
|
|
|
|
_wire->write(command);
|
|
|
|
_result = _wire->endTransmission();
|
|
|
|
return _result;
|
2014-10-19 06:54:35 -04:00
|
|
|
}
|
2017-07-26 17:51:37 -04:00
|
|
|
|
2021-12-22 04:13:21 -05:00
|
|
|
|
2022-10-27 10:52:08 -04:00
|
|
|
void MS5611::initConstants(uint8_t mathMode)
|
|
|
|
{
|
|
|
|
// constants that were multiplied in read() - datasheet page 8
|
|
|
|
// do this once and you save CPU cycles
|
|
|
|
//
|
|
|
|
// datasheet ms5611 | appNote
|
|
|
|
// mode = 0; | mode = 1
|
|
|
|
C[0] = 1;
|
|
|
|
C[1] = 32768L; // SENSt1 = C[1] * 2^15 | * 2^16
|
|
|
|
C[2] = 65536L; // OFFt1 = C[2] * 2^16 | * 2^17
|
|
|
|
C[3] = 3.90625E-3; // TCS = C[3] / 2^8 | / 2^7
|
|
|
|
C[4] = 7.8125E-3; // TCO = C[4] / 2^7 | / 2^6
|
|
|
|
C[5] = 256; // Tref = C[5] * 2^8 | * 2^8
|
|
|
|
C[6] = 1.1920928955E-7; // TEMPSENS = C[6] / 2^23 | / 2^23
|
|
|
|
|
|
|
|
if (mathMode == 1) // Appnote version for pressure.
|
|
|
|
{
|
|
|
|
C[1] = 65536L; // SENSt1
|
|
|
|
C[2] = 131072L; // OFFt1
|
|
|
|
C[3] = 7.8125E-3; // TCS
|
|
|
|
C[4] = 1.5625e-2; // TCO
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-27 05:20:37 -05:00
|
|
|
// -- END OF FILE --
|
2021-12-22 04:13:21 -05:00
|
|
|
|