Skip to content

Commit 0a3df91

Browse files
committed
+ 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
1 parent 2e5b971 commit 0a3df91

File tree

2 files changed

+49
-15
lines changed

2 files changed

+49
-15
lines changed

libraries/MS5611/MS5611.cpp

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22
// FILE: MS5611.cpp
33
// AUTHOR: Rob Tillaart
44
// Erni - testing/fixes
5-
// VERSION: 0.1.03
5+
// VERSION: 0.1.04
66
// PURPOSE: MS5611 Temperature & Humidity library for Arduino
77
// URL:
88
//
99
// HISTORY:
10+
// 0.1.04 changed float to double (for platforms which support it)
11+
// changed divisions in multiplications
12+
// fixed uint32_t readADC()
13+
// reduced size of C array by 1 float
14+
// added second order temperature compensation
1015
// 0.1.03 changed math to float [test version]
1116
// 0.1.02 fixed bug return value read()
1217
// fixed bug #bits D2
@@ -39,8 +44,10 @@ MS5611::MS5611(uint8_t address)
3944
void MS5611::init()
4045
{
4146
reset();
42-
for (int reg = 0; reg < 8; reg++)
47+
for (int reg = 0; reg < 7; reg++)
4348
{
49+
// C[0] not used; this way indices match datasheet.
50+
// C[7] == CRC skipped.
4451
C[reg] = readProm(reg);
4552
}
4653
}
@@ -50,21 +57,47 @@ int MS5611::read(uint8_t bits)
5057
// VARIABLES NAMES BASED ON DATASHEET
5158
convert(0x40, bits);
5259
if (_result) return _result;
53-
int32_t D1 = readADC();
60+
uint32_t D1 = readADC();
5461
if (_result) return _result;
5562

5663
convert(0x50, bits);
5764
if (_result) return _result;
58-
int32_t D2 = readADC();
65+
uint32_t D2 = readADC();
5966
if (_result) return _result;
6067

61-
// PAGE 7/20 of the datasheet
62-
float dT = D2 - (C[5] * 256L);
63-
_temperature = 2000 + (dT * C[6])/8388608L;
68+
// TODO the multiplications of these constants can be done in init()
69+
// but first they need to be verified.
70+
71+
// TEMP & PRESS MATH - PAGE 7/20
72+
double dT = D2 - C[5] * 256L;
73+
_temperature = 2000 + dT * C[6] * 1.1920928955E-7;
6474

65-
float offset = (C[2] * 65536L) + (C[4] * dT ) / 128L;
66-
float sens = C[1] * 32768L + (C[3] * dT ) / 256L;
67-
_pressure = (((D1 * sens)/2097152L) - offset) / 32768L;
75+
double offset = C[2] * 65536L + dT * C[4] * 7.8125E-3;
76+
double sens = C[1] * 32768L + dT * C[3] * 3.90625E-3;
77+
78+
// SECOND ORDER COMPENSATION - PAGE 8/20
79+
// COMMENT OUT < 20 CORRECTION IF NOT NEEDED
80+
if (_temperature < 20)
81+
{
82+
double T2 = dT * dT * 4.6566128731E-10;
83+
double t = _temperature - 2000;
84+
double offset2 = 2.5 * t * t;
85+
double sens2 = 1.25 * t * t * t;
86+
// COMMENT OUT < -15 CORRECTION IF NOT NEEDED
87+
if (_temperature < -15)
88+
{
89+
t = _temperature + 1500;
90+
t = t * t;
91+
offset2 += 7 * t;
92+
sens2 += 5.5 * t;
93+
}
94+
_temperature -= T2;
95+
offset -= offset2;
96+
sens -= sens2;
97+
}
98+
// END SECOND ORDER COMPENSATION
99+
100+
_pressure = (D1 * sens * 4.76837158205E-7 - offset) * 3.051757813E-5;
68101

69102
return 0;
70103
}
@@ -108,7 +141,7 @@ uint16_t MS5611::readProm(uint8_t reg)
108141
return 0;
109142
}
110143

111-
int32_t MS5611::readADC()
144+
uint32_t MS5611::readADC()
112145
{
113146
command(0x00);
114147
if (_result == 0)

libraries/MS5611/MS5611.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// FILE: MS5611.h
33
// AUTHOR: Rob Tillaart
44
// Erni - testing/fixes
5-
// VERSION: 0.1.03
5+
// VERSION: 0.1.04
66
// PURPOSE: MS5611 Temperature & Pressure library for Arduino
77
// URL:
88
//
@@ -20,7 +20,7 @@
2020
#include <Arduino.h>
2121
#endif
2222

23-
#define MS5611_LIB_VERSION "0.1.03"
23+
#define MS5611_LIB_VERSION "0.1.04"
2424

2525
#define MS5611_READ_OK 0
2626

@@ -38,15 +38,16 @@ class MS5611
3838
private:
3939
void reset();
4040
void convert(uint8_t ADDR, uint8_t bits);
41-
int32_t readADC();
41+
uint32_t readADC();
4242
uint16_t readProm(uint8_t reg);
4343
void command(uint8_t command);
4444

4545
uint8_t _address;
4646
int32_t _temperature;
4747
int32_t _pressure;
4848
int _result;
49-
float C[8];
49+
double C[7];
50+
5051
};
5152
#endif
5253
//

0 commit comments

Comments
 (0)