GY-63_MS5611/libraries/MS5611/MS5611.h

140 lines
3.9 KiB
C
Raw Normal View History

2020-11-27 05:20:37 -05:00
#pragma once
2014-10-19 06:54:35 -04:00
//
// FILE: MS5611.h
// AUTHOR: Rob Tillaart
// Erni - testing/fixes
2023-11-14 10:25:27 -05:00
// VERSION: 0.4.0
2020-11-27 05:20:37 -05:00
// PURPOSE: Arduino library for MS5611 temperature and pressure sensor
// URL: https://github.com/RobTillaart/MS5611
2014-10-19 06:54:35 -04:00
2021-01-29 06:31:58 -05:00
2020-11-27 05:20:37 -05:00
#include "Arduino.h"
#include "Wire.h"
2014-10-19 06:54:35 -04:00
2022-01-14 07:34:56 -05:00
// 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
2022-01-16 04:03:30 -05:00
// PS | o O | O = opening PS = protocol select
2022-01-14 07:34:56 -05:00
// +--------+
//
2022-01-23 08:56:48 -05:00
// PS to VCC ==> I2C (GY-63 board has internal pull up, so not needed)
2022-01-14 07:34:56 -05:00
// PS to GND ==> SPI
// CS to VCC ==> 0x76
// CS to GND ==> 0x77
2022-01-16 04:03:30 -05:00
2023-11-14 10:25:27 -05:00
#define MS5611_LIB_VERSION (F("0.4.0"))
2021-01-29 06:31:58 -05:00
2022-01-23 08:56:48 -05:00
#ifndef MS5611_DEFAULT_ADDRESS
#define MS5611_DEFAULT_ADDRESS 0x77
#endif
2021-01-29 06:31:58 -05:00
2021-12-22 04:13:21 -05:00
#define MS5611_READ_OK 0
2022-10-27 10:52:08 -04:00
#define MS5611_ERROR_2 2 // low level I2C error
2021-12-22 04:13:21 -05:00
#define MS5611_NOT_READ -999
2021-01-29 06:31:58 -05:00
2014-10-19 06:54:35 -04:00
2021-12-24 06:14:45 -05:00
enum osr_t
{
2022-01-16 04:03:30 -05:00
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
2021-12-24 06:14:45 -05:00
};
2014-10-19 06:54:35 -04:00
class MS5611
{
public:
2023-11-14 10:25:27 -05:00
explicit MS5611(uint8_t deviceAddress = MS5611_DEFAULT_ADDRESS, TwoWire *wire = &Wire);
2021-01-29 06:31:58 -05:00
2023-11-14 10:25:27 -05:00
bool begin();
2021-01-29 06:31:58 -05:00
bool isConnected();
2020-11-27 05:20:37 -05:00
2022-10-27 10:52:08 -04:00
// reset command + get constants
// mathMode = 0 (default), 1 = factor 2 fix.
// returns false if ROM constants are 0;
bool reset(uint8_t mathMode = 0);
2020-11-27 05:20:37 -05:00
2022-10-27 10:52:08 -04:00
// the actual reading of the sensor;
// returns MS5611_READ_OK upon success
2021-12-24 06:14:45 -05:00
int read(uint8_t bits);
2022-10-27 10:52:08 -04:00
// wrapper, uses the preset oversampling rate.
2022-01-16 04:03:30 -05:00
inline int read() { return read( (uint8_t) _samplingRate); };
2021-12-24 06:14:45 -05:00
2022-10-27 10:52:08 -04:00
// sets oversampling to a value between 8 and 12
2021-12-24 06:14:45 -05:00
void setOversampling(osr_t samplingRate);
2022-10-27 10:52:08 -04:00
// oversampling rate is in osr_t
2021-12-24 06:14:45 -05:00
osr_t getOversampling() const { return (osr_t) _samplingRate; };
2020-11-27 05:20:37 -05:00
2022-10-27 10:52:08 -04:00
// temperature is in ²C
2022-01-16 04:03:30 -05:00
float getTemperature() const;
2020-11-27 05:20:37 -05:00
2022-10-27 10:52:08 -04:00
// pressure is in mBar
2022-01-16 04:03:30 -05:00
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; };
2020-11-27 05:20:37 -05:00
2022-10-27 10:52:08 -04:00
// to check for failure
2021-12-24 06:14:45 -05:00
int getLastResult() const { return _result; };
2014-10-19 06:54:35 -04:00
2022-10-27 10:52:08 -04:00
// last time in millis() when the sensor has been read.
2022-01-16 04:03:30 -05:00
uint32_t lastRead() const { return _lastRead; };
2023-11-14 10:25:27 -05:00
// _deviceID is a SHIFT XOR merge of 7 PROM registers, reasonable unique
2022-01-23 08:56:48 -05:00
uint32_t getDeviceID() const { return _deviceID; };
2022-01-25 09:26:38 -05:00
void setCompensation(bool flag = true) { _compensation = flag; };
bool getCompensation() { return _compensation; };
2022-10-27 10:52:08 -04:00
// develop functions.
2022-01-16 04:03:30 -05:00
/*
2022-01-23 08:56:48 -05:00
void setAddress(uint8_t address) { _address = address; }; // RANGE CHECK + isConnected() !
2022-01-16 04:03:30 -05:00
uint8_t getAddress() const { return _address; };
2022-01-23 08:56:48 -05:00
uint8_t detectAddress() { todo }; // works with only one on the bus?
2022-01-16 04:03:30 -05:00
*/
2021-12-22 04:13:21 -05:00
2022-10-27 10:52:08 -04:00
// EXPERIMENTAL
uint16_t getManufacturer();
uint16_t getSerialCode();
2014-10-19 06:54:35 -04:00
2022-10-27 10:52:08 -04:00
protected:
2021-01-29 06:31:58 -05:00
void convert(const uint8_t addr, uint8_t bits);
uint32_t readADC();
uint16_t readProm(uint8_t reg);
int command(const uint8_t command);
2022-10-27 10:52:08 -04:00
void initConstants(uint8_t mathMode);
uint8_t _address;
2021-12-24 06:14:45 -05:00
uint8_t _samplingRate;
int32_t _temperature;
int32_t _pressure;
2022-01-16 04:03:30 -05:00
float _pressureOffset;
float _temperatureOffset;
int _result;
float C[7];
2020-11-27 05:20:37 -05:00
uint32_t _lastRead;
2022-01-23 08:56:48 -05:00
uint32_t _deviceID;
2022-01-25 09:26:38 -05:00
bool _compensation;
2021-12-22 04:13:21 -05:00
2021-01-29 06:31:58 -05:00
TwoWire * _wire;
2014-10-19 06:54:35 -04:00
};
2021-12-22 04:13:21 -05:00
2023-11-14 10:25:27 -05:00
// -- END OF FILE --
2021-12-22 04:13:21 -05:00