GY-63_MS5611/libraries/DS18B20_RT/DS18B20.cpp

176 lines
3.8 KiB
C++
Raw Normal View History

2021-01-29 06:31:58 -05:00
//
// FILE: DS18B20.cpp
// AUTHOR: Rob.Tillaart@gmail.com
2021-12-17 05:59:45 -05:00
// VERSION: 0.1.12
2021-01-29 06:31:58 -05:00
// DATE: 2017-07-25
// PUPROSE: library for DS18B20 temperature sensor with minimal footprint
2021-12-17 05:59:45 -05:00
// URL: https://github.com/RobTillaart/DS18B20_RT
2021-01-29 06:31:58 -05:00
//
// HISTORY:
2021-05-05 08:32:50 -04:00
// 0.1.0 2017-07-25 initial version
// 0.1.1 2020-02-18 added getAddress()
// 0.1.2 2020-04-11 #pragma once, refactor
// 0.1.3 2020-04-22 #1 fix library.json file
// 0.1.4 2020-04-23 #2 add retry in begin() to support Wemos
// 0.1.5 2020-04-29 #4 added set/getConfig + DEVICE_CRC_ERROR + example
// 0.1.6 2020-06-07 fix library.json
2021-12-17 05:59:45 -05:00
// 0.1.7 2020-12-20 add Arduino CI + unit test
2021-05-05 08:32:50 -04:00
// 0.1.8 2021-04-08 clear scratchpad before read + update readme.md
2021-05-26 07:54:24 -04:00
// 0.1.9 2021-05-26 add oneWire.reset() in begin()
2021-12-17 05:59:45 -05:00
// 0.1.10 2021-06-14 add retries parameter to begin()
2021-10-17 04:29:55 -04:00
// 0.1.11 2021-10-03 add dependency + fix build-CI
2021-12-17 05:59:45 -05:00
// 0.1.12 2021-12-17 update library.json, license, minor edits
2021-01-29 06:31:58 -05:00
#include "DS18B20.h"
// OneWire commands
2021-12-17 05:59:45 -05:00
#define STARTCONVO 0x44
#define READSCRATCH 0xBE
#define WRITESCRATCH 0x4E
2021-01-29 06:31:58 -05:00
// Scratchpad locations
2021-12-17 05:59:45 -05:00
#define TEMP_LSB 0
#define TEMP_MSB 1
#define HIGH_ALARM_TEMP 2
#define LOW_ALARM_TEMP 3
#define CONFIGURATION 4
#define INTERNAL_BYTE 5
#define COUNT_REMAIN 6
#define COUNT_PER_C 7
#define SCRATCHPAD_CRC 8
2021-01-29 06:31:58 -05:00
// Device resolution
2021-12-17 05:59:45 -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
2021-12-17 05:59:45 -05:00
DS18B20::DS18B20(OneWire* ow)
2021-01-29 06:31:58 -05:00
{
2021-12-17 05:59:45 -05:00
_oneWire = ow;
_addressFound = false;
2021-01-29 06:31:58 -05:00
_config = DS18B20_CLEAR;
}
2021-06-23 10:41:54 -04:00
bool DS18B20::begin(uint8_t retries)
2021-01-29 06:31:58 -05:00
{
_config = DS18B20_CLEAR;
2021-12-17 05:59:45 -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:59:45 -05:00
_oneWire->reset();
_oneWire->reset_search();
2021-01-29 06:31:58 -05:00
_deviceAddress[0] = 0x00;
2021-12-17 05:59:45 -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:59:45 -05:00
return _addressFound;
2021-01-29 06:31:58 -05:00
}
void DS18B20::readScratchPad(uint8_t *scratchPad, uint8_t fields)
{
2021-12-17 05:59:45 -05:00
_oneWire->reset();
_oneWire->select(_deviceAddress);
_oneWire->write(READSCRATCH);
2021-01-29 06:31:58 -05:00
for (uint8_t i = 0; i < fields; i++)
{
2021-12-17 05:59:45 -05:00
scratchPad[i] = _oneWire->read();
2021-01-29 06:31:58 -05:00
}
2021-12-17 05:59:45 -05:00
_oneWire->reset();
2021-01-29 06:31:58 -05:00
}
bool DS18B20::isConversionComplete(void)
{
2021-12-17 05:59:45 -05:00
return (_oneWire->read_bit() == 1);
2021-01-29 06:31:58 -05:00
}
void DS18B20::requestTemperatures(void)
{
2021-12-17 05:59:45 -05:00
_oneWire->reset();
_oneWire->skip();
_oneWire->write(STARTCONVO, 0);
2021-01-29 06:31:58 -05:00
}
float DS18B20::getTempC(void)
{
ScratchPad scratchPad;
2021-05-05 08:32:50 -04:00
for (int i = 0; i < 9; i++) scratchPad[i] = 0;
2021-01-29 06:31:58 -05:00
if (_config & DS18B20_CRC)
{
readScratchPad(scratchPad, 9);
2021-12-17 05:59:45 -05:00
if (_oneWire->crc8(scratchPad, 8) != scratchPad[SCRATCHPAD_CRC])
2021-01-29 06:31:58 -05:00
{
return DEVICE_CRC_ERROR;
}
}
else
{
readScratchPad(scratchPad, 2);
}
int16_t rawTemperature = (((int16_t)scratchPad[TEMP_MSB]) << 8) | scratchPad[TEMP_LSB];
float temp = 0.0625 * rawTemperature;
2021-05-05 08:32:50 -04:00
if (temp < -55)
{
return DEVICE_DISCONNECTED;
}
2021-01-29 06:31:58 -05:00
return temp;
}
2021-12-17 05:59:45 -05:00
void DS18B20::setResolution(uint8_t resolution)
2021-01-29 06:31:58 -05:00
{
2021-12-17 05:59:45 -05:00
_oneWire->reset();
_oneWire->select(_deviceAddress);
_oneWire->write(WRITESCRATCH);
2021-01-29 06:31:58 -05:00
// two dummy values for LOW & HIGH ALARM
2021-12-17 05:59:45 -05:00
_oneWire->write(0);
_oneWire->write(100);
switch (resolution)
2021-01-29 06:31:58 -05:00
{
case 12:
2021-12-17 05:59:45 -05:00
_oneWire->write(TEMP_12_BIT);
2021-01-29 06:31:58 -05:00
break;
case 11:
2021-12-17 05:59:45 -05:00
_oneWire->write(TEMP_11_BIT);
2021-01-29 06:31:58 -05:00
break;
case 10:
2021-12-17 05:59:45 -05:00
_oneWire->write(TEMP_10_BIT);
2021-01-29 06:31:58 -05:00
break;
case 9:
default:
2021-12-17 05:59:45 -05:00
_oneWire->write(TEMP_9_BIT);
2021-01-29 06:31:58 -05:00
break;
}
2021-12-17 05:59:45 -05:00
_oneWire->reset();
2021-01-29 06:31:58 -05:00
}
bool DS18B20::getAddress(uint8_t* buf)
{
2021-12-17 05:59:45 -05:00
if (_addressFound)
2021-01-29 06:31:58 -05:00
{
2021-06-23 10:41:54 -04:00
for (uint8_t i = 0; i < 8; i++)
{
buf[i] = _deviceAddress[i];
}
2021-01-29 06:31:58 -05:00
}
2021-12-17 05:59:45 -05:00
return _addressFound;
2021-01-29 06:31:58 -05:00
}
2021-06-23 10:41:54 -04:00
2021-01-29 06:31:58 -05:00
// -- END OF FILE --
2021-12-17 05:59:45 -05:00