0.2.0 DS18B20_INT

This commit is contained in:
rob tillaart 2022-06-24 17:29:55 +02:00
parent e5fdde4027
commit cf514217af
8 changed files with 162 additions and 20 deletions

View File

@ -1,10 +1,11 @@
// //
// FILE: DS18B20_INT.cpp // FILE: DS18B20_INT.cpp
// AUTHOR: Rob.Tillaart@gmail.com // AUTHOR: Rob.Tillaart@gmail.com
// VERSION: 0.1.7 // VERSION: 0.2.0
// DATE: 2017-07-25 // DATE: 2017-07-25
// PUPROSE: library for DS18B20 temperature sensor - integer only. // PUPROSE: library for DS18B20 temperature sensor - integer only.
// URL: https://github.com/RobTillaart/DS18B20_INT // URL: https://github.com/RobTillaart/DS18B20_INT
// https://github.com/RobTillaart/DS18B20_RT
// //
// HISTORY: // HISTORY:
// 0.1.0 2017-07-25 initial version // 0.1.0 2017-07-25 initial version
@ -15,6 +16,8 @@
// 0.1.5 2021-06-16 add retries parameter to begin() // 0.1.5 2021-06-16 add retries parameter to begin()
// 0.1.6 2021-10-03 add dependency + fix build-CI // 0.1.6 2021-10-03 add dependency + fix build-CI
// 0.1.7 2021-12-17 update library.json, license, minor edits // 0.1.7 2021-12-17 update library.json, license, minor edits
// 0.2.0 2022-06-23 fix #10 getTempCentiC() (thanks negroKiordi)
// fix reading sensor
#include "DS18B20_INT.h" #include "DS18B20_INT.h"
@ -26,13 +29,17 @@
// Device resolution // Device resolution
#define TEMP_9_BIT 0x1F // 9 bit #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
DS18B20_INT::DS18B20_INT(OneWire* ow) DS18B20_INT::DS18B20_INT(OneWire* ow)
{ {
_oneWire = ow; _oneWire = ow;
_addressFound = false; _addressFound = false;
_resolution = TEMP_9_BIT;
} }
@ -57,7 +64,7 @@ bool DS18B20_INT::begin(uint8_t retries)
// two dummy values for LOW & HIGH ALARM // two dummy values for LOW & HIGH ALARM
_oneWire->write(0); _oneWire->write(0);
_oneWire->write(100); _oneWire->write(100);
_oneWire->write(TEMP_9_BIT); // lowest as we do only integer math. _oneWire->write(_resolution); // lowest as we do only integer math.
_oneWire->reset(); _oneWire->reset();
} }
return _addressFound; return _addressFound;
@ -80,12 +87,7 @@ bool DS18B20_INT::isConversionComplete(void)
int16_t DS18B20_INT::getTempC(void) int16_t DS18B20_INT::getTempC(void)
{ {
_oneWire->reset(); int16_t rawTemperature = _readRaw();
_oneWire->select(_deviceAddress);
_oneWire->write(READSCRATCH);
int16_t rawTemperature = ((int16_t)_oneWire->read()) << 8;
rawTemperature |= _oneWire->read();
_oneWire->reset();
rawTemperature >>= 4; rawTemperature >>= 4;
if (rawTemperature < -55) return DEVICE_DISCONNECTED; if (rawTemperature < -55) return DEVICE_DISCONNECTED;
return rawTemperature; return rawTemperature;
@ -105,5 +107,66 @@ bool DS18B20_INT::getAddress(uint8_t* buf)
} }
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;
}
// -- END OF FILE -- // -- END OF FILE --

View File

@ -2,11 +2,12 @@
// //
// FILE: DS18B20_INT.h // FILE: DS18B20_INT.h
// AUTHOR: Rob.Tillaart@gmail.com // AUTHOR: Rob.Tillaart@gmail.com
// VERSION: 0.1.7 // VERSION: 0.2.0
// DATE: 2017-07-25 // DATE: 2017-07-25
// PUPROSE: Minimalistic library for DS18B20 temperature sensor // PUPROSE: Minimalistic library for DS18B20 temperature sensor
// uses only integer math (no float to minimize footprint) // uses only integer math (no float to minimize footprint)
// URL: https://github.com/RobTillaart/DS18B20_INT // URL: https://github.com/RobTillaart/DS18B20_INT
// https://github.com/RobTillaart/DS18B20_RT
// //
// BOTTOM VIEW // BOTTOM VIEW
@ -20,7 +21,7 @@
// //
#define DS18B20_INT_LIB_VERSION (F("0.1.7")) #define DS18B20_INT_LIB_VERSION (F("0.2.0"))
#include "Arduino.h" #include "Arduino.h"
#include "OneWire.h" #include "OneWire.h"
@ -41,10 +42,18 @@ public:
bool isConversionComplete(void); bool isConversionComplete(void);
bool getAddress(uint8_t* buf); bool getAddress(uint8_t* buf);
void setResolution(uint8_t bits = 9);
uint8_t getResolution();
int16_t getTempCentiC(void);
private: private:
DeviceAddress _deviceAddress; DeviceAddress _deviceAddress;
OneWire* _oneWire; OneWire* _oneWire;
bool _addressFound; bool _addressFound;
uint8_t _resolution;
int16_t _readRaw();
}; };

View File

@ -24,6 +24,8 @@ I'm a great fan of the above library however some time ago I needed to strip it
to save a few dozen bytes. I reworked that minimalistic version into a library and I to save a few dozen bytes. I reworked that minimalistic version into a library and I
added a number of Arduino examples to help you get started. added a number of Arduino examples to help you get started.
This library is also related to - https://github.com/RobTillaart/DS18B20_RT
## Interface ## Interface
@ -37,10 +39,29 @@ returns true if all is OK. there will be a number of retries to connect, default
- **void requestTemperatures()** trigger temperature conversion. - **void requestTemperatures()** trigger temperature conversion.
- **bool isConversionComplete()** check if conversion is complete. - **bool isConversionComplete()** check if conversion is complete.
- **int16_t getTempC()** returns temperature in whole degrees only. -55..125 - **int16_t getTempC()** returns temperature in whole degrees only. -55..125
-127 = DEVICE_DISCONNECTED or -127 = DEVICE_DISCONNECTED
- **bool getAddress()** returns true if the sensor is configured (available). - **bool getAddress()** returns true if the sensor is configured (available).
### CentiC part
The following functions are experimental since 0.2.0
They allow to use a higher resolution while not using floats.
This keeps the library small.
- **void setResolution(uint8_t bits = 9)** sets the internal resolution to 9, 10, 11 or 12 bits.
Other numbers will be mapped on 9.
This will affect the conversion time for a measurement.
Internally it will call **begin()** to set the new resolution if needed.
- **void getResolution()** returns the bits set, default 9.
Convenience function.
- **getTempCentiC(void)** returns the measured temperature times 100. -5500..12500
So 10.62°C will be returned as 1062.
**Warning** The DEVICE_DISCONNECTED is not tested for, but is commented in the code.
Use at own risk.
## Operation ## Operation
This library supports only one DS18B20 per Arduino/ MCU pin. This library supports only one DS18B20 per Arduino/ MCU pin.
@ -60,8 +81,6 @@ This library supports only one DS18B20 per Arduino/ MCU pin.
Connect a pull-up resistor 4.7 KOhm between pin3 and pin2. Connect a pull-up resistor 4.7 KOhm between pin3 and pin2.
When the wires are longer this resistor needs to be smaller. When the wires are longer this resistor needs to be smaller.
Check examples.
### Pull up resistor ### Pull up resistor
@ -78,7 +97,7 @@ Note: thicker wires require smaller resistors (typically 1 step in E12 series)
| 100cm (3'4") | 3K3 | 2K2 | | 100cm (3'4") | 3K3 | 2K2 |
| 200cm (6'8") | 2K2 | 1K0 | | 200cm (6'8") | 2K2 | 1K0 |
| 500cm (16'8") | 1K0 | \* | | 500cm (16'8") | 1K0 | \* |
| longer | * | \* | | longer | \* | \* |
\* = no info, smaller \* = no info, smaller
@ -93,4 +112,7 @@ and all people who contributed to that lib.
- add examples - add examples
- a multi sensor == multiple pins, no bus - a multi sensor == multiple pins, no bus
- add rounding for **getTempC()**.
- now it truncates, so it can be 0.5°C off.
- add "0.5" to raw and truncate

View File

@ -1,7 +1,6 @@
// //
// FILE: DS18B20_INT.ino // FILE: DS18B20_INT.ino
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// VERSION: 0.0.1
// PURPOSE: Minimalistic demo // PURPOSE: Minimalistic demo
@ -43,4 +42,3 @@ void loop()
// -- END OF FILE -- // -- END OF FILE --

View File

@ -0,0 +1,46 @@
//
// FILE: DS18B20_INT_getTempCentiC.ino
// AUTHOR: Rob Tillaart
// PURPOSE: Minimalistic demo
#include "DS18B20_INT.h"
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DS18B20_INT sensor(&oneWire);
uint32_t start, stop;
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
sensor.begin();
sensor.setResolution(12);
Serial.println(sensor.getResolution());
}
void loop()
{
int n = 0;
start = millis();
sensor.requestTemperatures();
while (!sensor.isConversionComplete()) n++;
int t = sensor.getTempCentiC();
stop = millis();
Serial.print(stop - start);
Serial.print("\t");
Serial.print(n);
Serial.print("\t");
Serial.println(t);
Serial.println();
delay(1000);
}
// -- END OF FILE --

View File

@ -11,6 +11,10 @@ requestTemperatures KEYWORD2
isConversionComplete KEYWORD2 isConversionComplete KEYWORD2
getAddress KEYWORD2 getAddress KEYWORD2
setResolution KEYWORD2
getResolution KEYWORD2
getTempCentiC KEYWORD2
# Constants (LITERAL1) # Constants (LITERAL1)
DS18B20_INT_LIB_VERSION LITERAL1 DS18B20_INT_LIB_VERSION LITERAL1

View File

@ -23,7 +23,7 @@
"version": "^2.3.5" "version": "^2.3.5"
} }
], ],
"version": "0.1.7", "version": "0.2.0",
"license": "MIT", "license": "MIT",
"frameworks": "arduino", "frameworks": "arduino",
"platforms": "*", "platforms": "*",

View File

@ -1,5 +1,5 @@
name=DS18B20_int name=DS18B20_int
version=0.1.7 version=0.2.0
author=Rob Tillaart <rob.tillaart@gmail.com> author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com> maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Library for DS18B20 restricted to a single sensor per pin. sentence=Library for DS18B20 restricted to a single sensor per pin.