Skip to content

Commit e4db5d2

Browse files
committed
+ 0.1.02 (first stable version)
+ 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() + added example/test application
1 parent 8d24e1b commit e4db5d2

File tree

3 files changed

+86
-34
lines changed

3 files changed

+86
-34
lines changed

libraries/MS5611/MS5611.cpp

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
//
22
// FILE: MS5611.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.01
4+
// VERSION: 0.1.02
55
// PURPOSE: MS5611 Temperature & Humidity library for Arduino
66
// URL:
77
//
88
// HISTORY:
9+
// 0.1.02 fixed bug return value read()
10+
// fixed bug #bits D2
11+
// added MS5611_READ_OK
12+
// added inline getters for temp & pres & lastresult.
13+
// adjusted delay's based on datasheet
14+
// merged convert functions
15+
// fixed offset in readProm()
916
// 0.1.01 small refactoring
1017
// 0.1.00 added temperature and Pressure code
1118
// 0.0.00 initial version by Rob Tillaart (15-okt-2014)
@@ -33,67 +40,63 @@ void MS5611::init()
3340
for (int reg = 0; reg < 8; reg++)
3441
{
3542
C[reg] = readProm(reg);
36-
delay(10);
3743
}
3844
}
3945

4046
int MS5611::read(uint8_t bits)
4147
{
42-
convertD1(bits);
48+
// VARIABLES NAMES BASED ON DATASHEET
49+
convert(0x40, bits);
4350
if (_result) return _result;
44-
delay(10);
4551
int32_t D1 = readADC();
4652
if (_result) return _result;
47-
delay(10);
48-
convertD2(8);
53+
54+
convert(0x50, bits);
4955
if (_result) return _result;
50-
delay(10);
5156
int32_t D2 = readADC();
5257
if (_result) return _result;
53-
delay(10);
5458

55-
// PAGE 7/20 of the datasheet
59+
// PAGE 7/20 DATASHEET
5660
int32_t dT = D2 - C[5] * 256L;
57-
temperature = 2000 + (dT * C[6])/8388608L;
61+
_temperature = 2000 + (dT * C[6])/8388608L;
5862

63+
// float is faster and smaller footprint (700 bytes).
64+
// no substantial loss in accuracy TODO verify
5965
int64_t offset = C[2] * 65536L + (C[4] * dT ) / 128L;
6066
int64_t sens = C[1] * 32768L + (C[3] * dT ) / 256L;
61-
pressure = ((D1 * sens)/2097152L - offset) / 32768L;
62-
}
67+
_pressure = ((D1 * sens)/2097152L - offset) / 32768L;
6368

64-
int MS5611::getLastResult()
65-
{
66-
return _result;
69+
// TODO second order compensation
70+
// PAGE 8/20 DATASHEET
71+
72+
return 0;
6773
}
6874

75+
6976
/////////////////////////////////////////////////////
7077
//
7178
// PRIVATE
7279
//
7380
void MS5611::reset()
7481
{
7582
command(0x1E);
83+
delay(3);
7684
}
7785

78-
void MS5611::convertD1(uint8_t bits)
79-
{
80-
bits = constrain(bits, 8, 12);
81-
int offset = (bits - 8) * 2;
82-
command(0x40 + offset);
83-
}
84-
85-
void MS5611::convertD2(uint8_t bits)
86+
void MS5611::convert(uint8_t addr, uint8_t bits)
8687
{
88+
uint8_t del[5] = {1,2,3,5,10};
8789
bits = constrain(bits, 8, 12);
88-
int offset = (bits - 8) * 2;
89-
command(0x50 + offset);
90+
uint8_t offset = (bits - 8) * 2;
91+
command(addr + offset);
92+
delay(del[offset/2]);
9093
}
9194

9295
uint16_t MS5611::readProm(uint8_t reg)
9396
{
9497
reg = constrain(reg, 0, 7);
95-
reg = reg * 2;
96-
command(0xA0 + reg);
98+
uint8_t offset = reg * 2;
99+
command(0xA0 + offset);
97100
if (_result == 0)
98101
{
99102
int nr = Wire.requestFrom(_address, (uint8_t)2);

libraries/MS5611/MS5611.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: MS5611.h
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.01
4+
// VERSION: 0.1.02
55
// PURPOSE: MS5611 Temperature & Pressure library for Arduino
66
// URL:
77
//
@@ -13,12 +13,15 @@
1313
#define MS5611_h
1414

1515
#if ARDUINO < 100
16+
#error "VERSION NOT SUPPPORTED"
1617
#include <WProgram.h>
1718
#else
1819
#include <Arduino.h>
1920
#endif
2021

21-
#define MS5611_LIB_VERSION "0.1.01"
22+
#define MS5611_LIB_VERSION "0.1.02"
23+
24+
#define MS5611_READ_OK 0
2225

2326
class MS5611
2427
{
@@ -27,19 +30,20 @@ class MS5611
2730

2831
void init();
2932
int read(uint8_t bits = 8);
30-
int32_t temperature;
31-
int32_t pressure;
32-
int getLastResult();
33+
inline int32_t getTemperature() { return _temperature; };
34+
inline int32_t getPressure() { return _pressure; };
35+
inline int getLastResult() { return _result; };
3336

3437
private:
3538
void reset();
36-
void convertD1(uint8_t bits);
37-
void convertD2(uint8_t bits);
39+
void convert(uint8_t ADDR, uint8_t bits);
3840
int32_t readADC();
3941
uint16_t readProm(uint8_t reg);
4042
void command(uint8_t command);
4143

4244
uint8_t _address;
45+
int32_t _temperature;
46+
int32_t _pressure;
4347
int _result;
4448
uint16_t C[8];
4549
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// FILE: MS5611.ino
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.02
5+
// PURPOSE: demo application
6+
// DATE: 2014-okt-16
7+
// URL:
8+
//
9+
// Released to the public domain
10+
//
11+
12+
#include "MS5611.h"
13+
#include <Wire.h>
14+
15+
MS5611 MS5611(0x77);
16+
17+
void setup()
18+
{
19+
Wire.begin();
20+
21+
Serial.begin(115200);
22+
Serial.print("MS5611 test version: ");
23+
Serial.println(MS5611_LIB_VERSION);
24+
25+
MS5611.init();
26+
}
27+
28+
void loop()
29+
{
30+
int result = MS5611.read();
31+
if (result != 0)
32+
{
33+
Serial.print("Error in read: ");
34+
Serial.println(result);
35+
}
36+
else
37+
{
38+
Serial.println("T:\t");
39+
Serial.println(MS5611.getTemperature() * 0.01, 2); // print as float
40+
Serial.println("P:\t");
41+
Serial.println(MS5611.getPressure() * 0.01, 2); // print as float
42+
}
43+
44+
delay(5000);
45+
}

0 commit comments

Comments
 (0)