-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMS5611.h
123 lines (94 loc) · 3.15 KB
/
MS5611.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#pragma once
//
// FILE: MS5611.h
// AUTHOR: Rob Tillaart
// Erni - testing/fixes
// VERSION: 0.3.6
// PURPOSE: Arduino library for MS5611 temperature and pressure sensor
// URL: https://github.com/RobTillaart/MS5611
#include "Arduino.h"
#include "Wire.h"
// BREAKOUT MS5611 aka GY63 - see datasheet
//
// SPI I2C
// +--------+
// VCC VCC | o |
// GND GND | o |
// SCL | o |
// SDI SDA | o |
// CSO | o |
// SDO | o L | L = led
// 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.6"))
#define MS5611_READ_OK 0
#define MS5611_ERROR_2 2 // low level I2C error
#define MS5611_NOT_READ -999
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
};
class MS5611
{
public:
explicit MS5611(uint8_t deviceAddress);
#if defined (ESP8266) || defined(ESP32)
bool begin(uint8_t sda, uint8_t scl, TwoWire *wire = &Wire);
#endif
bool begin(TwoWire *wire = &Wire);
bool isConnected();
// reset command + get constants
void reset();
// the actual reading of the sensor;
// returns MS5611_READ_OK upon success
int read(uint8_t bits);
// 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);
// oversampling rate is in osr_t
osr_t getOversampling() const { return (osr_t) _samplingRate; };
// temperature is in ²C
float getTemperature() const;
// pressure is in mBar
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() 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:
void convert(const uint8_t addr, uint8_t bits);
uint32_t readADC();
uint16_t readProm(uint8_t reg);
int command(const uint8_t command);
uint8_t _address;
uint8_t _samplingRate;
int32_t _temperature;
int32_t _pressure;
float _pressureOffset;
float _temperatureOffset;
int _result;
float C[7];
uint32_t _lastRead;
TwoWire * _wire;
};
// -- END OF FILE --