Skip to content

Commit

Permalink
fix #33 factor 2 pressure (#34)
Browse files Browse the repository at this point in the history
- add RP2040 support to build-CI
- add CHANGELOG.md
- add getManufacturer()
- add getSerialCode()
- add parameter mathMode to **reset()** to switch between pressure math.
  • Loading branch information
RobTillaart authored Oct 27, 2022
1 parent bbc74ab commit 23e4a33
Show file tree
Hide file tree
Showing 9 changed files with 408 additions and 121 deletions.
18 changes: 18 additions & 0 deletions .arduino-ci.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
platforms:
rpipico:
board: rp2040:rp2040:rpipico
package: rp2040:rp2040
gcc:
features:
defines:
- ARDUINO_ARCH_RP2040
warnings:
flags:

packages:
rp2040:rp2040:
url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json

compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
Expand All @@ -9,3 +24,6 @@ compile:
- esp32
# - esp8266
# - mega2560
- rpipico


111 changes: 111 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# Change Log MS5611

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.3.9] - 2022-10-26
- add RP2040 support to build-CI
- add CHANGELOG.md
- add getManufacturer()
- add getSerialCode()
- add parameter mathMode to **reset()** to switch between pressure math.
- see readme for details.

## [0.3.8] - 2022-01-24
- reset() returns bool indicating successful ROM read
- get/setCompensation() to enable/disable compensation.

## [0.3.7] - 2022-01-22
- fix #26 added getPromHash() == **getDeviceID()**
- add getDeviceID(), to provide a sort of unique device ID (experimental) based
upon uniqueness of the factory calibration values.
- fix #24 default all examples address 0x77
- default address for constructor, can be set as define on the command line.
MS5611_DEFAULT_ADDRESS.

## [0.3.6] - 2022-01-15
- add setOffset functions; minor refactor;
- The timing for convert is adjusted from TYPICAL to MAX
- datasheet page 3. - see issue #23
- The **write(0)** in **isConnected()** is made conditional explicit for the NANO 33 BLE.

## [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

## [0.3.2] - 2021-12-24
- add get/set oversampling, read() (thanks to LyricPants66133)

## [0.3.1] - 2021-12-21
- update library.json, readme, license, minor edits

## [0.3.0] - 2021-01-27
- fix #9 math error (thanks to Emiel Steerneman)
- previous versions ==> **obsolete**.
- temperature is a float expressed in degrees Celsius.
- pressure is a float expressed in mBar.
- add Wire1..WireN support (e.g. teensy)
- changed getTemperature() and getPressure()
- add reset()

----

## [0.2.2] - 2021-01-01
- add Arduino-CI + unit tests + isConnected()

## [0.2.1] - 2020-06-28
- fix #1 min macro compile error

## [0.2.0] - 2020-06-21
- refactor; #pragma once;

----

## [0.1.8] - 2018-07-01
- fix #109 incorrect constants (thanks to flauth)

## [0.1.7] = 2017-07-26
- revert double to float (issue 33)

## [0.1.6] - 2015-07-12
- refactor

## [0.1.05] - 2015-03-07
- moved 6 float multiplies to init() ==> adds ~70 bytes !!!
- moved the MS5611_LIB_VERSION to PROGMEM

## [0.1.04] - 2014-10-19
- changed float to double (for platforms which support it)
- changed divisions in multiplications
- fixed uint32_t readADC()
- reduced size of C array by 1 float
- added second order temperature compensation

## [0.1.03] - 2014-10-17
- changed math to float (test version)

## [0.1.02] - 2014-10-17
- fixed bug return value read()
- fixed bug #bits D2
- added MS5611_READ_OK
- added inline getters for temp & pres & lastResult.
- adjusted delay's based on datasheet
- merged convert functions
- fixed offset in readProm()

## [0.1.01] - 2014-10-16
- small refactoring

## [0.1.00]
- added temperature and Pressure code

## [0.0.00] - 2014-10-15
- initial version
147 changes: 69 additions & 78 deletions MS5611.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,11 @@
// FILE: MS5611.cpp
// AUTHOR: Rob Tillaart
// Erni - testing/fixes
// VERSION: 0.3.8
// VERSION: 0.3.9
// PURPOSE: MS5611 Temperature & Humidity library for Arduino
// URL: https://github.com/RobTillaart/MS5611
//
// HISTORY:
// 0.3.8 2022-01-24 reset() returns bool
// get/setCompensation()
// 0.3.7 2022-01-22 fix #26 added getPromHash()
// fix #24 default all examples address 0x77
// 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
// 0.3.2 2021-12-24 add get/set oversampling, read() (thanks to LyricPants66133)
// 0.3.1 2021-12-21 update library.json, readme, license, minor edits
// 0.3.0 2021-01-27 fix #9 math error (thanks to Emiel Steerneman)
// add Wire1..WireN support (e.g. teensy)
// changed getTemperature() and getPressure()
// add reset()
//
// 0.2.2 2021-01-01 add Arduino-CI + unit tests + isConnected()
// 0.2.1 2020-06-28 fix #1 min macro compile error
// 0.2.0 2020-06-21 refactor; #pragma once;
//
// 0.1.8 fix #109 incorrect constants (thanks to flauth)
// 0.1.7 revert double to float (issue 33)
// 0.1.6 2015-07-12 refactor
// 0.1.05 moved 6 float multiplies to init [adds ~70 bytes !!!]
// moved the MS5611_LIB_VERSION to PROGMEM
// 0.1.04 changed float to double (for platforms which support it)
// changed divisions in multiplications
// fixed uint32_t readADC()
// reduced size of C array by 1 float
// added second order temperature compensation
// 0.1.03 changed math to float [test version]
// 0.1.02 fixed bug return value read()
// fixed bug #bits D2
// added MS5611_READ_OK
// added inline getters for temp & pres & lastResult.
// adjusted delay's based on datasheet
// merged convert functions
// fixed offset in readProm()
// 0.1.01 small refactoring
// 0.1.00 added temperature and Pressure code
// 0.0.00 initial version by Rob Tillaart (15-okt-2014)
// HISTORY see changelog.md


#include "MS5611.h"
Expand All @@ -63,7 +22,7 @@

/////////////////////////////////////////////////////
//
// PUBLIC
// PUBLIC
//
MS5611::MS5611(uint8_t deviceAddress)
{
Expand Down Expand Up @@ -121,38 +80,34 @@ bool MS5611::isConnected()
}


bool MS5611::reset()
bool MS5611::reset(uint8_t mathMode)
{
command(MS5611_CMD_RESET);
uint32_t start = micros();
// while loop prevents blocking RTOS

// 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;
C[1] = 32768L; // SENSt1 = C[1] * 2^15
C[2] = 65536L; // OFFt1 = C[2] * 2^16
C[3] = 3.90625E-3; // TCS = C[3] / 2^6
C[4] = 7.8125E-3; // TCO = C[4] / 2^7
C[5] = 256; // Tref = C[5] * 2^8
C[6] = 1.1920928955E-7; // TEMPSENS = C[6] / 2^23
// read factory calibrations from EEPROM.

// initialize the C[] array
initConstants(mathMode);

// read factory calibrations from EEPROM.
bool ROM_OK = true;
for (uint8_t reg = 0; reg < 7; reg++)
{
// used indices match datasheet.
// C[0] == manufacturer - read but not used;
// C[7] == CRC - skipped.
// used indices match datasheet.
// C[0] == manufacturer - read but not used;
// C[7] == CRC - skipped.
uint16_t tmp = readProm(reg);
C[reg] *= tmp;
// _deviceID is a simple SHIFT XOR merge of PROM data
// _deviceID is a SHIFT XOR merge of 7 PROM registers, reasonable unique
_deviceID <<= 4;
_deviceID ^= tmp;
// Serial.println(readProm(reg));
// Serial.println(readProm(reg));
if (reg > 0)
{
ROM_OK = ROM_OK && (tmp != 0);
Expand All @@ -164,12 +119,12 @@ bool MS5611::reset()

int MS5611::read(uint8_t bits)
{
// VARIABLES NAMES BASED ON DATASHEET
// ALL MAGIC NUMBERS ARE FROM DATASHEET
// VARIABLES NAMES BASED ON DATASHEET
// ALL MAGIC NUMBERS ARE FROM DATASHEET

convert(MS5611_CMD_CONVERT_D1, bits);
if (_result) return _result;
// NOTE: D1 and D2 seem reserved in MBED (NANO BLE)
// NOTE: D1 and D2 seem reserved in MBED (NANO BLE)
uint32_t _D1 = readADC();
if (_result) return _result;

Expand All @@ -178,14 +133,14 @@ int MS5611::read(uint8_t bits)
uint32_t _D2 = readADC();
if (_result) return _result;

// Serial.println(_D1);
// Serial.println(_D2);
// Serial.println(_D1);
// Serial.println(_D2);

// TEST VALUES - comment lines above
// uint32_t _D1 = 9085466;
// uint32_t _D2 = 8569150;
// uint32_t _D1 = 9085466;
// uint32_t _D2 = 8569150;

// TEMP & PRESS MATH - PAGE 7/20
// TEMP & PRESS MATH - PAGE 7/20
float dT = _D2 - C[5];
_temperature = 2000 + dT * C[6];

Expand All @@ -194,16 +149,16 @@ int MS5611::read(uint8_t bits)

if (_compensation)
{
// SECOND ORDER COMPENSATION - PAGE 8/20
// COMMENT OUT < 2000 CORRECTION IF NOT NEEDED
// NOTE TEMPERATURE IS IN 0.01 C
// SECOND ORDER COMPENSATION - PAGE 8/20
// COMMENT OUT < 2000 CORRECTION IF NOT NEEDED
// NOTE TEMPERATURE IS IN 0.01 C
if (_temperature < 2000)
{
float T2 = dT * dT * 4.6566128731E-10;
float t = (_temperature - 2000) * (_temperature - 2000);
float offset2 = 2.5 * t;
float sens2 = 1.25 * t;
// COMMENT OUT < -1500 CORRECTION IF NOT NEEDED
// COMMENT OUT < -1500 CORRECTION IF NOT NEEDED
if (_temperature < -1500)
{
t = (_temperature + 1500) * (_temperature + 1500);
Expand All @@ -214,7 +169,7 @@ int MS5611::read(uint8_t bits)
offset -= offset2;
sens -= sens2;
}
// END SECOND ORDER COMPENSATION
// END SECOND ORDER COMPENSATION
}

_pressure = (_D1 * sens * 4.76837158205E-7 - offset) * 3.051757813E-5;
Expand Down Expand Up @@ -243,14 +198,26 @@ float MS5611::getPressure() const
return _pressure * 0.01 + _pressureOffset;
};

// EXPERIMENTAL
uint16_t MS5611::getManufacturer()
{
return readProm(0);
}

// EXPERIMENTAL
uint16_t MS5611::getSerialCode()
{
return readProm(7) >> 4;
}


/////////////////////////////////////////////////////
//
// PRIVATE
// PRIVATE
//
void MS5611::convert(const uint8_t addr, uint8_t bits)
{
// values from page 3 datasheet - MAX column (rounded up)
// values from page 3 datasheet - MAX column (rounded up)
uint16_t del[5] = {600, 1200, 2300, 4600, 9100};

uint8_t index = bits;
Expand All @@ -262,7 +229,7 @@ void MS5611::convert(const uint8_t addr, uint8_t bits)

uint16_t waitTime = del[index];
uint32_t start = micros();
// while loop prevents blocking RTOS
// while loop prevents blocking RTOS
while (micros() - start < waitTime)
{
yield();
Expand All @@ -273,7 +240,7 @@ void MS5611::convert(const uint8_t addr, uint8_t bits)

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

Expand Down Expand Up @@ -325,5 +292,29 @@ int MS5611::command(const uint8_t command)
}


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

// -- END OF FILE --

Loading

0 comments on commit 23e4a33

Please sign in to comment.