+ 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
This commit is contained in:
rob tillaart 2014-10-19 13:00:16 +02:00
parent 8d24e1bef7
commit e4db5d21e6
3 changed files with 86 additions and 34 deletions

View File

@ -1,11 +1,18 @@
//
// FILE: MS5611.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.01
// VERSION: 0.1.02
// PURPOSE: MS5611 Temperature & Humidity library for Arduino
// URL:
//
// HISTORY:
// 0.1.02 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()
// 0.1.01 small refactoring
// 0.1.00 added temperature and Pressure code
// 0.0.00 initial version by Rob Tillaart (15-okt-2014)
@ -33,38 +40,38 @@ void MS5611::init()
for (int reg = 0; reg < 8; reg++)
{
C[reg] = readProm(reg);
delay(10);
}
}
int MS5611::read(uint8_t bits)
{
convertD1(bits);
// VARIABLES NAMES BASED ON DATASHEET
convert(0x40, bits);
if (_result) return _result;
delay(10);
int32_t D1 = readADC();
if (_result) return _result;
delay(10);
convertD2(8);
convert(0x50, bits);
if (_result) return _result;
delay(10);
int32_t D2 = readADC();
if (_result) return _result;
delay(10);
// PAGE 7/20 of the datasheet
// PAGE 7/20 DATASHEET
int32_t dT = D2 - C[5] * 256L;
temperature = 2000 + (dT * C[6])/8388608L;
_temperature = 2000 + (dT * C[6])/8388608L;
// float is faster and smaller footprint (700 bytes).
// no substantial loss in accuracy TODO verify
int64_t offset = C[2] * 65536L + (C[4] * dT ) / 128L;
int64_t sens = C[1] * 32768L + (C[3] * dT ) / 256L;
pressure = ((D1 * sens)/2097152L - offset) / 32768L;
_pressure = ((D1 * sens)/2097152L - offset) / 32768L;
// TODO second order compensation
// PAGE 8/20 DATASHEET
return 0;
}
int MS5611::getLastResult()
{
return _result;
}
/////////////////////////////////////////////////////
//
@ -73,27 +80,23 @@ int MS5611::getLastResult()
void MS5611::reset()
{
command(0x1E);
delay(3);
}
void MS5611::convertD1(uint8_t bits)
void MS5611::convert(uint8_t addr, uint8_t bits)
{
uint8_t del[5] = {1,2,3,5,10};
bits = constrain(bits, 8, 12);
int offset = (bits - 8) * 2;
command(0x40 + offset);
}
void MS5611::convertD2(uint8_t bits)
{
bits = constrain(bits, 8, 12);
int offset = (bits - 8) * 2;
command(0x50 + offset);
uint8_t offset = (bits - 8) * 2;
command(addr + offset);
delay(del[offset/2]);
}
uint16_t MS5611::readProm(uint8_t reg)
{
reg = constrain(reg, 0, 7);
reg = reg * 2;
command(0xA0 + reg);
uint8_t offset = reg * 2;
command(0xA0 + offset);
if (_result == 0)
{
int nr = Wire.requestFrom(_address, (uint8_t)2);

View File

@ -1,7 +1,7 @@
//
// FILE: MS5611.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.01
// VERSION: 0.1.02
// PURPOSE: MS5611 Temperature & Pressure library for Arduino
// URL:
//
@ -13,12 +13,15 @@
#define MS5611_h
#if ARDUINO < 100
#error "VERSION NOT SUPPPORTED"
#include <WProgram.h>
#else
#include <Arduino.h>
#endif
#define MS5611_LIB_VERSION "0.1.01"
#define MS5611_LIB_VERSION "0.1.02"
#define MS5611_READ_OK 0
class MS5611
{
@ -27,19 +30,20 @@ public:
void init();
int read(uint8_t bits = 8);
int32_t temperature;
int32_t pressure;
int getLastResult();
inline int32_t getTemperature() { return _temperature; };
inline int32_t getPressure() { return _pressure; };
inline int getLastResult() { return _result; };
private:
void reset();
void convertD1(uint8_t bits);
void convertD2(uint8_t bits);
void convert(uint8_t ADDR, uint8_t bits);
int32_t readADC();
uint16_t readProm(uint8_t reg);
void command(uint8_t command);
uint8_t _address;
int32_t _temperature;
int32_t _pressure;
int _result;
uint16_t C[8];
};

View File

@ -0,0 +1,45 @@
//
// FILE: MS5611.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.02
// PURPOSE: demo application
// DATE: 2014-okt-16
// URL:
//
// Released to the public domain
//
#include "MS5611.h"
#include <Wire.h>
MS5611 MS5611(0x77);
void setup()
{
Wire.begin();
Serial.begin(115200);
Serial.print("MS5611 test version: ");
Serial.println(MS5611_LIB_VERSION);
MS5611.init();
}
void loop()
{
int result = MS5611.read();
if (result != 0)
{
Serial.print("Error in read: ");
Serial.println(result);
}
else
{
Serial.println("T:\t");
Serial.println(MS5611.getTemperature() * 0.01, 2); // print as float
Serial.println("P:\t");
Serial.println(MS5611.getPressure() * 0.01, 2); // print as float
}
delay(5000);
}