2021-01-29 06:31:58 -05:00
|
|
|
//
|
|
|
|
// FILE: DS18B20_INT.cpp
|
|
|
|
// AUTHOR: Rob.Tillaart@gmail.com
|
2022-11-02 10:15:02 -04:00
|
|
|
// VERSION: 0.2.1
|
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
|
|
|
//
|
2022-11-02 10:15:02 -04:00
|
|
|
// HISTORY: see changelog.md
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
|
|
|
#include "DS18B20_INT.h"
|
|
|
|
|
2022-11-02 10:15:02 -04:00
|
|
|
|
|
|
|
// OneWire commands
|
2021-12-17 05:26:25 -05:00
|
|
|
#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
|
2022-06-24 11:29:55 -04: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
|
|
|
|
|
|
|
|
2021-12-17 05:26:25 -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;
|
2021-12-17 05:26:25 -05:00
|
|
|
_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)
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
2021-12-17 05:26:25 -05:00
|
|
|
_addressFound = false;
|
|
|
|
for (uint8_t rtr = retries; (rtr > 0) && (_addressFound == false); rtr--)
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
2021-12-17 05:26:25 -05:00
|
|
|
_oneWire->reset();
|
|
|
|
_oneWire->reset_search();
|
2021-01-29 06:31:58 -05:00
|
|
|
_deviceAddress[0] = 0x00;
|
2021-12-17 05:26:25 -05:00
|
|
|
_oneWire->search(_deviceAddress);
|
|
|
|
_addressFound = _deviceAddress[0] != 0x00 &&
|
|
|
|
_oneWire->crc8(_deviceAddress, 7) == _deviceAddress[7];
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
2021-12-17 05:26:25 -05:00
|
|
|
if (_addressFound)
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
2021-12-17 05:26:25 -05:00
|
|
|
_oneWire->reset();
|
|
|
|
_oneWire->select(_deviceAddress);
|
|
|
|
_oneWire->write(WRITESCRATCH);
|
2022-11-02 10:15:02 -04:00
|
|
|
// two dummy values for LOW & HIGH ALARM
|
2021-12-17 05:26:25 -05:00
|
|
|
_oneWire->write(0);
|
|
|
|
_oneWire->write(100);
|
2022-11-02 10:15:02 -04:00
|
|
|
_oneWire->write(_resolution); // lowest as we do only integer math.
|
2021-12-17 05:26:25 -05:00
|
|
|
_oneWire->reset();
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
2021-12-17 05:26:25 -05:00
|
|
|
return _addressFound;
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DS18B20_INT::requestTemperatures(void)
|
|
|
|
{
|
2021-12-17 05:26:25 -05:00
|
|
|
_oneWire->reset();
|
|
|
|
_oneWire->skip();
|
|
|
|
_oneWire->write(STARTCONVO, 0);
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool DS18B20_INT::isConversionComplete(void)
|
|
|
|
{
|
2021-12-17 05:26:25 -05:00
|
|
|
return (_oneWire->read_bit() == 1);
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int16_t DS18B20_INT::getTempC(void)
|
|
|
|
{
|
2022-06-24 11:29:55 -04:00
|
|
|
int16_t rawTemperature = _readRaw();
|
2021-01-29 06:31:58 -05:00
|
|
|
rawTemperature >>= 4;
|
|
|
|
if (rawTemperature < -55) return DEVICE_DISCONNECTED;
|
|
|
|
return rawTemperature;
|
|
|
|
}
|
|
|
|
|
2021-05-26 07:55:36 -04:00
|
|
|
|
2021-06-16 04:41:09 -04:00
|
|
|
bool DS18B20_INT::getAddress(uint8_t* buf)
|
|
|
|
{
|
2021-12-17 05:26:25 -05:00
|
|
|
if (_addressFound)
|
2021-06-16 04:41:09 -04:00
|
|
|
{
|
|
|
|
for (uint8_t i = 0; i < 8; i++)
|
|
|
|
{
|
|
|
|
buf[i] = _deviceAddress[i];
|
|
|
|
}
|
|
|
|
}
|
2021-12-17 05:26:25 -05:00
|
|
|
return _addressFound;
|
2021-06-16 04:41:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-06-24 11:29:55 -04:00
|
|
|
void DS18B20_INT::setResolution(uint8_t bits)
|
|
|
|
{
|
|
|
|
uint8_t newRes = 0;
|
|
|
|
switch (bits)
|
|
|
|
{
|
|
|
|
case 12: newRes = TEMP_12_BIT; break;
|
|
|
|
case 11: newRes = TEMP_11_BIT; break;
|
|
|
|
case 10: newRes = TEMP_10_BIT; break;
|
|
|
|
default: newRes = TEMP_9_BIT; break;
|
|
|
|
}
|
|
|
|
if (newRes != _resolution)
|
|
|
|
{
|
|
|
|
_resolution = newRes;
|
|
|
|
begin();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
int16_t rawTemperature = _readRaw();
|
|
|
|
// rawTemperature = rawTemperature * 100 / 16;
|
|
|
|
rawTemperature *= 25;
|
|
|
|
rawTemperature >>= 2;
|
|
|
|
// use at own risk.
|
|
|
|
// if (rawTemperature < -5500) return DEVICE_DISCONNECTED * 100;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
// -- END OF FILE --
|
2021-12-17 05:26:25 -05:00
|
|
|
|