Skip to content

Commit

Permalink
Merge pull request #267 from milesburton/fix/DS1820-support-accidenta…
Browse files Browse the repository at this point in the history
…lly-removed

Fix/ds1820 support accidentally removed
  • Loading branch information
milesburton authored Jan 21, 2025
2 parents 3cfcb26 + ec506be commit dafac43
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 4 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
1 change: 1 addition & 0 deletions examples/Simple/Simple.ino
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ void loop(void)
Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
delay(1500);
// After we got the temperatures, we can print them here.
// We use the function ByIndex, and as an example get the temperature from the first sensor only.
float tempC = sensors.getTempCByIndex(0);
Expand Down
1 change: 1 addition & 0 deletions examples/Single/Single.ino
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ void loop(void)
Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
delay(1500);

// It responds almost immediately. Let's print out the data
printTemperature(insideThermometer); // Use a simple function to print out the data
Expand Down
2 changes: 1 addition & 1 deletion examples/WaitForConversion/WaitForConversion.ino
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,5 @@ void loop(void)
Serial.println(sensors.getTempCByIndex(0));
Serial.println("\n\n\n\n");

delay(5000);
delay(1500);
}
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 dafac43

Please sign in to comment.