Skip to content

Commit

Permalink
add offset functions, refactor (#22)
Browse files Browse the repository at this point in the history
* add offset functions, refactor
* update convert timing - use MAX - issue 23
  • Loading branch information
RobTillaart authored Jan 16, 2022
1 parent 62b8543 commit 9ad86d3
Show file tree
Hide file tree
Showing 10 changed files with 496 additions and 73 deletions.
95 changes: 68 additions & 27 deletions MS5611.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
// FILE: MS5611.cpp
// AUTHOR: Rob Tillaart
// Erni - testing/fixes
// VERSION: 0.3.5
// VERSION: 0.3.6
// PURPOSE: MS5611 Temperature & Humidity library for Arduino
// URL: https://github.com/RobTillaart/MS5611
//
// HISTORY:
// 0.3.6 2022-01-15 add setOffset functions; minor refactor;
// adjust convert timing to max - see issue #23
// 0.3.5 2022-01-13 fix isConnected() for NANO 33 BLE
// 0.3.4 2021-12-29 fix #16 compilation for MBED
// 0.3.3 2021-12-25 Update oversampling timings to reduce time spent waiting
Expand Down Expand Up @@ -61,12 +63,14 @@
//
MS5611::MS5611(uint8_t deviceAddress)
{
_address = deviceAddress;
_samplingRate = OSR_ULTRA_LOW;
_temperature = MS5611_NOT_READ;
_pressure = MS5611_NOT_READ;
_result = MS5611_NOT_READ;
_lastRead = 0;
_address = deviceAddress;
_samplingRate = OSR_ULTRA_LOW;
_temperature = MS5611_NOT_READ;
_pressure = MS5611_NOT_READ;
_result = MS5611_NOT_READ;
_lastRead = 0;
_pressureOffset = 0;
_temperatureOffset = 0;
}


Expand Down Expand Up @@ -105,15 +109,24 @@ bool MS5611::begin(TwoWire * wire)
bool MS5611::isConnected()
{
_wire->beginTransmission(_address);
_wire->write(0); // needed for NANO 33 BLE
#ifdef ARDUINO_ARCH_NRF52840
// needed for NANO 33 BLE
_wire->write(0);
#endif
return (_wire->endTransmission() == 0);
}


void MS5611::reset()
{
command(MS5611_CMD_RESET);
delayMicroseconds(2800);
uint32_t start = micros();
// while loop prevents blocking RTOS
while (micros() - start < 2800)
{
yield();
delayMicroseconds(10);
}
// constants that were multiplied in read()
// do this once and you save CPU cycles
C[0] = 1;
Expand Down Expand Up @@ -195,38 +208,65 @@ void MS5611::setOversampling(osr_t samplingRate)
_samplingRate = (uint8_t) samplingRate;
}


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;
};


/////////////////////////////////////////////////////
//
// PRIVATE
//
void MS5611::convert(const uint8_t addr, uint8_t bits)
{
//Values from page 2 datasheet
uint16_t del[5] = {500, 1100, 2100, 4100, 8220};

bits = constrain(bits, 8, 12);
uint8_t offset = (bits - 8) * 2;
// values from page 3 datasheet - MAX column (rounded up)
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;
command(addr + offset);
delayMicroseconds(del[offset/2]);

uint16_t waitTime = del[index];
uint32_t start = micros();
// while loop prevents blocking RTOS
while (micros() - start < waitTime)
{
yield();
delayMicroseconds(10);
}
}


uint16_t MS5611::readProm(uint8_t reg)
{
// last EEPROM register is CRC - Page13 datasheet.
// last EEPROM register is CRC - Page 13 datasheet.
uint8_t promCRCRegister = 7;
if (reg > promCRCRegister) return 0;

uint8_t offset = reg * 2;
command(MS5611_CMD_READ_PROM + offset);
if (_result == 0)
{
int nr = _wire->requestFrom(_address, (uint8_t)2);
if (nr >= 2)
uint8_t length = 2;
int bytes = _wire->requestFrom(_address, length);
if (bytes >= length)
{
uint16_t val = _wire->read() * 256;
val += _wire->read();
return val;
uint16_t value = _wire->read() * 256;
value += _wire->read();
return value;
}
return 0;
}
Expand All @@ -239,13 +279,14 @@ uint32_t MS5611::readADC()
command(MS5611_CMD_READ_ADC);
if (_result == 0)
{
int nr = _wire->requestFrom(_address, (uint8_t)3);
if (nr >= 3)
uint8_t length = 3;
int bytes = _wire->requestFrom(_address, length);
if (bytes >= length)
{
uint32_t val = _wire->read() * 65536UL;
val += _wire->read() * 256UL;
val += _wire->read();
return val;
uint32_t value = _wire->read() * 65536UL;
value += _wire->read() * 256UL;
value += _wire->read();
return value;
}
return 0UL;
}
Expand Down
42 changes: 29 additions & 13 deletions MS5611.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// FILE: MS5611.h
// AUTHOR: Rob Tillaart
// Erni - testing/fixes
// VERSION: 0.3.5
// VERSION: 0.3.6
// PURPOSE: Arduino library for MS5611 temperature and pressure sensor
// URL: https://github.com/RobTillaart/MS5611

Expand All @@ -21,15 +21,16 @@
// SDI SDA | o |
// CSO | o |
// SDO | o L | L = led
// PS | o O | O = opening PS
// PS | o O | O = opening PS = protocol select
// +--------+
//
// PS to VCC ==> I2C
// PS to GND ==> SPI
// CS to VCC ==> 0x76
// CS to GND ==> 0x77

#define MS5611_LIB_VERSION (F("0.3.5"))

#define MS5611_LIB_VERSION (F("0.3.6"))


#define MS5611_READ_OK 0
Expand All @@ -39,11 +40,11 @@

enum osr_t
{
OSR_ULTRA_HIGH = 12, // 10 millis
OSR_HIGH = 11, // 5 millis
OSR_STANDARD = 10, // 3 millis
OSR_LOW = 9, // 2 millis
OSR_ULTRA_LOW = 8 // 1 millis Default = backwards compatible
OSR_ULTRA_HIGH = 12, // 10 millis
OSR_HIGH = 11, // 5 millis
OSR_STANDARD = 10, // 3 millis
OSR_LOW = 9, // 2 millis
OSR_ULTRA_LOW = 8 // 1 millis Default = backwards compatible
};


Expand All @@ -64,7 +65,8 @@ class MS5611
// the actual reading of the sensor;
// returns MS5611_READ_OK upon success
int read(uint8_t bits);
inline int read() { return read( (uint8_t) _samplingRate); }; // uses the preset oversampling
// wrapper, uses the preset oversampling rate.
inline int read() { return read( (uint8_t) _samplingRate); };

// sets oversampling to a value between 8 and 12
void setOversampling(osr_t samplingRate);
Expand All @@ -73,16 +75,28 @@ class MS5611
osr_t getOversampling() const { return (osr_t) _samplingRate; };

// temperature is in ²C
float getTemperature() const { return _temperature * 0.01; };
float getTemperature() const;

// pressure is in mBar
float getPressure() const { return _pressure * 0.01; };
float getPressure() const;

// OFFSET - 0.3.6
void setPressureOffset(float offset = 0) { _pressureOffset = offset; };
float getPressureOffset() { return _pressureOffset; };
void setTemperatureOffset(float offset = 0) { _temperatureOffset = offset; };
float getTemperatureOffset() { return _temperatureOffset; };

// to check for failure
int getLastResult() const { return _result; };

// last time in millis() that the sensor has been read.
uint32_t lastRead() { return _lastRead; };
// last time in millis() when the sensor has been read.
uint32_t lastRead() const { return _lastRead; };

// develop functions.
/*
void setAddress(uint8_t address) { _address = address; }; // RANGE CHECK !!!
uint8_t getAddress() const { return _address; };
*/


private:
Expand All @@ -95,6 +109,8 @@ class MS5611
uint8_t _samplingRate;
int32_t _temperature;
int32_t _pressure;
float _pressureOffset;
float _temperatureOffset;
int _result;
float C[7];
uint32_t _lastRead;
Expand Down
Loading

0 comments on commit 9ad86d3

Please sign in to comment.