GY-63_MS5611/libraries/DS18B20_INT/DS18B20_INT.cpp

186 lines
3.6 KiB
C++
Raw Normal View History

2021-01-29 06:31:58 -05:00
//
// FILE: DS18B20_INT.cpp
// AUTHOR: Rob.Tillaart@gmail.com
2023-02-04 07:31:19 -05:00
// VERSION: 0.2.2
2021-01-29 06:31:58 -05:00
// DATE: 2017-07-25
// PUPROSE: library for DS18B20 temperature sensor - integer only.
// URL: https://github.com/RobTillaart/DS18B20_INT
2022-06-24 11:29:55 -04:00
// https://github.com/RobTillaart/DS18B20_RT
2021-01-29 06:31:58 -05:00
#include "DS18B20_INT.h"
2022-11-02 10:15:02 -04:00
// OneWire commands
#define STARTCONVO 0x44
#define READSCRATCH 0xBE
#define WRITESCRATCH 0x4E
2021-01-29 06:31:58 -05:00
2021-06-16 04:41:09 -04:00
2022-11-02 10:15:02 -04:00
// Device resolution
2023-02-04 07:31:19 -05:00
#define TEMP_9_BIT 0x1F // 9 bit
#define TEMP_10_BIT 0x3F // 10 bit
#define TEMP_11_BIT 0x5F // 11 bit
#define TEMP_12_BIT 0x7F // 12 bit
2021-01-29 06:31:58 -05:00
DS18B20_INT::DS18B20_INT(OneWire* ow)
2021-01-29 06:31:58 -05:00
{
2022-06-24 11:29:55 -04:00
_oneWire = ow;
_addressFound = false;
2022-06-24 11:29:55 -04:00
_resolution = TEMP_9_BIT;
2021-01-29 06:31:58 -05:00
}
2021-05-26 07:55:36 -04:00
2021-06-16 04:41:09 -04:00
bool DS18B20_INT::begin(uint8_t retries)
2023-02-04 07:31:19 -05:00
{
isConnected(retries);
if (_addressFound)
{
_setResolution();
}
return _addressFound;
}
bool DS18B20_INT::isConnected(uint8_t retries)
2021-01-29 06:31:58 -05:00
{
_addressFound = false;
for (uint8_t rtr = retries; (rtr > 0) && (_addressFound == false); rtr--)
2021-01-29 06:31:58 -05:00
{
_oneWire->reset();
_oneWire->reset_search();
2021-01-29 06:31:58 -05:00
_deviceAddress[0] = 0x00;
_oneWire->search(_deviceAddress);
_addressFound = _deviceAddress[0] != 0x00 &&
_oneWire->crc8(_deviceAddress, 7) == _deviceAddress[7];
2021-01-29 06:31:58 -05:00
}
return _addressFound;
2021-01-29 06:31:58 -05:00
}
void DS18B20_INT::requestTemperatures(void)
{
_oneWire->reset();
_oneWire->skip();
_oneWire->write(STARTCONVO, 0);
2021-01-29 06:31:58 -05:00
}
bool DS18B20_INT::isConversionComplete(void)
{
return (_oneWire->read_bit() == 1);
2021-01-29 06:31:58 -05:00
}
int16_t DS18B20_INT::getTempC(void)
{
2023-02-04 07:31:19 -05:00
if (isConnected(3) == false)
{
return DEVICE_DISCONNECTED;
}
2022-06-24 11:29:55 -04:00
int16_t rawTemperature = _readRaw();
2021-01-29 06:31:58 -05:00
rawTemperature >>= 4;
2023-02-04 07:31:19 -05:00
if (rawTemperature < -55)
{
return DEVICE_DISCONNECTED;
}
2021-01-29 06:31:58 -05:00
return rawTemperature;
}
2021-05-26 07:55:36 -04:00
2023-02-04 07:31:19 -05:00
bool DS18B20_INT::getAddress(uint8_t* buf)
2021-06-16 04:41:09 -04:00
{
if (_addressFound)
2021-06-16 04:41:09 -04:00
{
for (uint8_t i = 0; i < 8; i++)
{
buf[i] = _deviceAddress[i];
}
}
return _addressFound;
2021-06-16 04:41:09 -04:00
}
2023-02-04 07:31:19 -05:00
bool DS18B20_INT::setResolution(uint8_t bits)
2022-06-24 11:29:55 -04:00
{
switch (bits)
{
2023-02-04 07:31:19 -05:00
case 12: _resolution = TEMP_12_BIT; break;
case 11: _resolution = TEMP_11_BIT; break;
case 10: _resolution = TEMP_10_BIT; break;
default: _resolution = TEMP_9_BIT; break;
2022-06-24 11:29:55 -04:00
}
2023-02-04 07:31:19 -05:00
return begin();
2022-06-24 11:29:55 -04:00
}
uint8_t DS18B20_INT::getResolution()
{
switch (_resolution)
{
case TEMP_12_BIT: return 12;
case TEMP_11_BIT: return 11;
case TEMP_10_BIT: return 10;
case TEMP_9_BIT: return 9;
}
return 0;
}
int16_t DS18B20_INT::getTempCentiC(void)
{
2023-02-04 07:31:19 -05:00
if (isConnected(3) == false)
{
return DEVICE_DISCONNECTED;
}
2022-06-24 11:29:55 -04:00
int16_t rawTemperature = _readRaw();
// rawTemperature = rawTemperature * 100 / 16;
rawTemperature *= 25;
rawTemperature >>= 2;
2023-02-04 07:31:19 -05:00
// use at own risk. (not tested)
if (rawTemperature < -5500)
{
return DEVICE_DISCONNECTED * 100;
}
2022-06-24 11:29:55 -04:00
return rawTemperature;
}
//////////////////////////////////////////////////
//
// PRIVATE
//
int16_t DS18B20_INT::_readRaw(void)
{
_oneWire->reset();
_oneWire->select(_deviceAddress);
_oneWire->write(READSCRATCH);
int16_t rawTemperature = ((int16_t)_oneWire->read());
rawTemperature |= _oneWire->read() << 8;
_oneWire->reset();
return rawTemperature;
}
2023-02-04 07:31:19 -05:00
bool DS18B20_INT::_setResolution()
{
if (_addressFound)
{
_oneWire->reset();
_oneWire->select(_deviceAddress);
_oneWire->write(WRITESCRATCH);
// two dummy values for LOW & HIGH ALARM
_oneWire->write(0);
_oneWire->write(100);
// lowest as default as we do only integer math.
_oneWire->write(_resolution);
_oneWire->reset();
}
return _addressFound;
}
2022-06-24 11:29:55 -04:00
2021-01-29 06:31:58 -05:00
// -- END OF FILE --