2
2
// FILE: MS5611.cpp
3
3
// AUTHOR: Rob Tillaart
4
4
// Erni - testing/fixes
5
- // VERSION: 0.1.03
5
+ // VERSION: 0.1.04
6
6
// PURPOSE: MS5611 Temperature & Humidity library for Arduino
7
7
// URL:
8
8
//
9
9
// 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
10
15
// 0.1.03 changed math to float [test version]
11
16
// 0.1.02 fixed bug return value read()
12
17
// fixed bug #bits D2
@@ -39,8 +44,10 @@ MS5611::MS5611(uint8_t address)
39
44
void MS5611::init ()
40
45
{
41
46
reset ();
42
- for (int reg = 0 ; reg < 8 ; reg++)
47
+ for (int reg = 0 ; reg < 7 ; reg++)
43
48
{
49
+ // C[0] not used; this way indices match datasheet.
50
+ // C[7] == CRC skipped.
44
51
C[reg] = readProm (reg);
45
52
}
46
53
}
@@ -50,21 +57,47 @@ int MS5611::read(uint8_t bits)
50
57
// VARIABLES NAMES BASED ON DATASHEET
51
58
convert (0x40 , bits);
52
59
if (_result) return _result;
53
- int32_t D1 = readADC ();
60
+ uint32_t D1 = readADC ();
54
61
if (_result) return _result;
55
62
56
63
convert (0x50 , bits);
57
64
if (_result) return _result;
58
- int32_t D2 = readADC ();
65
+ uint32_t D2 = readADC ();
59
66
if (_result) return _result;
60
67
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 ;
64
74
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 ;
68
101
69
102
return 0 ;
70
103
}
@@ -108,7 +141,7 @@ uint16_t MS5611::readProm(uint8_t reg)
108
141
return 0 ;
109
142
}
110
143
111
- int32_t MS5611::readADC ()
144
+ uint32_t MS5611::readADC ()
112
145
{
113
146
command (0x00 );
114
147
if (_result == 0 )
0 commit comments