Skip to content

Commit

Permalink
fix: Restored DS18S20 support
Browse files Browse the repository at this point in the history
  • Loading branch information
milesburton committed Jan 21, 2025
1 parent 3cfcb26 commit 4839194
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
35 changes: 35 additions & 0 deletions DallasTemperature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ extern "C" {

#define NO_ALARM_HANDLER ((AlarmHandler *)0)

// DSROM FIELDS
#define DSROM_FAMILY 0
#define DSROM_CRC 7

DallasTemperature::DallasTemperature() {
_wire = nullptr;
devices = 0;
Expand Down Expand Up @@ -554,6 +558,37 @@ int32_t DallasTemperature::calculateTemperature(const uint8_t* deviceAddress, ui
| neg;
}

/*
DS1820 and DS18S20 have a 9-bit temperature register.
Resolutions greater than 9-bit can be calculated using the data from
the temperature, and COUNT REMAIN and COUNT PER °C registers in the
scratchpad. The resolution of the calculation depends on the model.
While the COUNT PER °C register is hard-wired to 16 (10h) in a
DS18S20, it changes with temperature in DS1820.
After reading the scratchpad, the TEMP_READ value is obtained by
truncating the 0.5°C bit (bit 0) from the temperature data. The
extended resolution temperature can then be calculated using the
following equation:
COUNT_PER_C - COUNT_REMAIN
TEMPERATURE = TEMP_READ - 0.25 + --------------------------
COUNT_PER_C
Hagai Shatz simplified this to integer arithmetic for a 12 bits
value for a DS18S20, and James Cameron added legacy DS1820 support.
See - http://myarduinotoy.blogspot.co.uk/2013/02/12bit-result-from-ds18s20.html
*/

if ((deviceAddress[DSROM_FAMILY] == DS18S20MODEL) && (scratchPad[COUNT_PER_C] != 0)) {
fpTemperature = (((fpTemperature & 0xfff0) << 3) - 32
+ (((scratchPad[COUNT_PER_C] - scratchPad[COUNT_REMAIN]) << 7)
/ scratchPad[COUNT_PER_C])) | neg;
}

return fpTemperature;
}

Expand Down
2 changes: 1 addition & 1 deletion DallasTemperature.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef DallasTemperature_h
#define DallasTemperature_h

#define DALLASTEMPLIBVERSION "4.0.1"
#define DALLASTEMPLIBVERSION "4.0.2"

// Configuration
#ifndef REQUIRESNEW
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
{
"paulstoffregen/OneWire": "^2.3.5"
},
"version": "4.0.1",
"version": "4.0.2",
"frameworks": "arduino",
"platforms": "*"
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=DallasTemperature
version=4.0.1
version=4.0.2
author=Miles Burton <mail@milesburton.com>, Tim Newsome <nuisance@casualhacker.net>, Guil Barros <gfbarros@bappos.com>, Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Miles Burton <mail@milesburton.com>
sentence=Arduino library for Dallas/Maxim temperature ICs
Expand Down

0 comments on commit 4839194

Please sign in to comment.