Skip to content

Commit c0bf9f0

Browse files
authored
Merge pull request #150 from RobTillaart/master
fix #149 rename .pde examples to .ino
2 parents 104fae7 + ad511a4 commit c0bf9f0

File tree

10 files changed

+174
-90
lines changed

10 files changed

+174
-90
lines changed

DallasTemperature.cpp

Lines changed: 100 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ extern "C" {
1515

1616
// OneWire commands
1717
#define STARTCONVO 0x44 // Tells device to take a temperature reading and put it on the scratchpad
18-
#define COPYSCRATCH 0x48 // Copy EEPROM
19-
#define READSCRATCH 0xBE // Read EEPROM
20-
#define WRITESCRATCH 0x4E // Write to EEPROM
21-
#define RECALLSCRATCH 0xB8 // Reload from last known
18+
#define COPYSCRATCH 0x48 // Copy scratchpad to EEPROM
19+
#define READSCRATCH 0xBE // Read from scratchpad
20+
#define WRITESCRATCH 0x4E // Write to scratchpad
21+
#define RECALLSCRATCH 0xB8 // Recall from EEPROM to scratchpad
2222
#define READPOWERSUPPLY 0xB4 // Determine if device needs parasite power
2323
#define ALARMSEARCH 0xEC // Query bus for devices with an alarm condition
2424

@@ -39,22 +39,21 @@ extern "C" {
3939
#define TEMP_11_BIT 0x5F // 11 bit
4040
#define TEMP_12_BIT 0x7F // 12 bit
4141

42+
#define MAX_CONVERSION_TIMEOUT 750
43+
44+
// Alarm handler
4245
#define NO_ALARM_HANDLER ((AlarmHandler *)0)
4346

44-
DallasTemperature::DallasTemperature()
45-
{
47+
48+
DallasTemperature::DallasTemperature() {
4649
#if REQUIRESALARMS
4750
setAlarmHandler(NO_ALARM_HANDLER);
4851
#endif
4952
useExternalPullup = false;
5053
}
51-
DallasTemperature::DallasTemperature(OneWire* _oneWire)
52-
{
54+
55+
DallasTemperature::DallasTemperature(OneWire* _oneWire) : DallasTemperature() {
5356
setOneWire(_oneWire);
54-
#if REQUIRESALARMS
55-
setAlarmHandler(NO_ALARM_HANDLER);
56-
#endif
57-
useExternalPullup = false;
5857
}
5958

6059
bool DallasTemperature::validFamily(const uint8_t* deviceAddress) {
@@ -74,8 +73,8 @@ bool DallasTemperature::validFamily(const uint8_t* deviceAddress) {
7473
* Constructs DallasTemperature with strong pull-up turned on. Strong pull-up is mandated in DS18B20 datasheet for parasitic
7574
* power (2 wires) setup. (https://datasheets.maximintegrated.com/en/ds/DS18B20.pdf, p. 7, section 'Powering the DS18B20').
7675
*/
77-
DallasTemperature::DallasTemperature(OneWire* _oneWire, uint8_t _pullupPin) : DallasTemperature(_oneWire){
78-
setPullupPin(_pullupPin);
76+
DallasTemperature::DallasTemperature(OneWire* _oneWire, uint8_t _pullupPin) : DallasTemperature(_oneWire) {
77+
setPullupPin(_pullupPin);
7978
}
8079

8180
void DallasTemperature::setPullupPin(uint8_t _pullupPin) {
@@ -109,19 +108,19 @@ void DallasTemperature::begin(void) {
109108
while (_wire->search(deviceAddress)) {
110109

111110
if (validAddress(deviceAddress)) {
112-
113-
if (!parasite && readPowerSupply(deviceAddress))
114-
parasite = true;
115-
116-
bitResolution = max(bitResolution, getResolution(deviceAddress));
117-
118111
devices++;
112+
119113
if (validFamily(deviceAddress)) {
120114
ds18Count++;
115+
116+
if (!parasite && readPowerSupply(deviceAddress))
117+
parasite = true;
118+
119+
uint8_t b = getResolution(deviceAddress);
120+
if (b > bitResolution) bitResolution = b;
121121
}
122122
}
123123
}
124-
125124
}
126125

127126
// returns the number of devices found on the bus
@@ -260,69 +259,85 @@ void DallasTemperature::setResolution(uint8_t newResolution) {
260259

261260
bitResolution = constrain(newResolution, 9, 12);
262261
DeviceAddress deviceAddress;
263-
for (int i = 0; i < devices; i++) {
262+
for (uint8_t i = 0; i < devices; i++) {
264263
getAddress(deviceAddress, i);
265264
setResolution(deviceAddress, bitResolution, true);
266265
}
267-
268266
}
269267

268+
/* PROPOSAL */
269+
270270
// set resolution of a device to 9, 10, 11, or 12 bits
271271
// if new resolution is out of range, 9 bits is used.
272272
bool DallasTemperature::setResolution(const uint8_t* deviceAddress,
273-
uint8_t newResolution, bool skipGlobalBitResolutionCalculation) {
274-
275-
// ensure same behavior as setResolution(uint8_t newResolution)
276-
newResolution = constrain(newResolution, 9, 12);
277-
278-
// return when stored value == new value
279-
if (getResolution(deviceAddress) == newResolution)
280-
return true;
281-
282-
ScratchPad scratchPad;
283-
if (isConnected(deviceAddress, scratchPad)) {
284-
285-
// DS1820 and DS18S20 have no resolution configuration register
286-
if (deviceAddress[0] != DS18S20MODEL) {
287-
288-
switch (newResolution) {
289-
case 12:
290-
scratchPad[CONFIGURATION] = TEMP_12_BIT;
291-
break;
292-
case 11:
293-
scratchPad[CONFIGURATION] = TEMP_11_BIT;
294-
break;
295-
case 10:
296-
scratchPad[CONFIGURATION] = TEMP_10_BIT;
297-
break;
298-
case 9:
299-
default:
300-
scratchPad[CONFIGURATION] = TEMP_9_BIT;
301-
break;
302-
}
303-
writeScratchPad(deviceAddress, scratchPad);
304-
305-
// without calculation we can always set it to max
306-
bitResolution = max(bitResolution, newResolution);
307-
308-
if (!skipGlobalBitResolutionCalculation
309-
&& (bitResolution > newResolution)) {
310-
bitResolution = newResolution;
311-
DeviceAddress deviceAddr;
312-
for (int i = 0; i < devices; i++) {
313-
getAddress(deviceAddr, i);
314-
bitResolution = max(bitResolution,
315-
getResolution(deviceAddr));
316-
}
317-
}
318-
}
319-
return true; // new value set
320-
}
321-
322-
return false;
323-
273+
uint8_t newResolution, bool skipGlobalBitResolutionCalculation) {
274+
275+
bool success = false;
276+
277+
// DS1820 and DS18S20 have no resolution configuration register
278+
if (deviceAddress[0] == DS18S20MODEL)
279+
{
280+
success = true;
281+
}
282+
else
283+
{
284+
285+
// handle the sensors with configuration register
286+
newResolution = constrain(newResolution, 9, 12);
287+
uint8_t newValue = 0;
288+
ScratchPad scratchPad;
289+
290+
// we can only update the sensor if it is connected
291+
if (isConnected(deviceAddress, scratchPad))
292+
{
293+
switch (newResolution) {
294+
case 12:
295+
newValue = TEMP_12_BIT;
296+
break;
297+
case 11:
298+
newValue = TEMP_11_BIT;
299+
break;
300+
case 10:
301+
newValue = TEMP_10_BIT;
302+
break;
303+
case 9:
304+
default:
305+
newValue = TEMP_9_BIT;
306+
break;
307+
}
308+
309+
// if it needs to be updated we write the new value
310+
if (scratchPad[CONFIGURATION] != newValue)
311+
{
312+
scratchPad[CONFIGURATION] = newValue;
313+
writeScratchPad(deviceAddress, scratchPad);
314+
}
315+
// done
316+
success = true;
317+
}
318+
}
319+
320+
// do we need to update the max resolution used?
321+
if (skipGlobalBitResolutionCalculation == false)
322+
{
323+
bitResolution = newResolution;
324+
if (devices > 1)
325+
{
326+
for (uint8_t i = 0; i < devices; i++)
327+
{
328+
if (bitResolution == 12) break;
329+
DeviceAddress deviceAddr;
330+
getAddress(deviceAddr, i);
331+
uint8_t b = getResolution(deviceAddr);
332+
if (b > bitResolution) bitResolution = b;
333+
}
334+
}
335+
}
336+
337+
return success;
324338
}
325339

340+
326341
// returns the global resolution
327342
uint8_t DallasTemperature::getResolution() {
328343
return bitResolution;
@@ -356,6 +371,7 @@ uint8_t DallasTemperature::getResolution(const uint8_t* deviceAddress) {
356371

357372
}
358373

374+
359375
// sets the value of the waitForConversion flag
360376
// TRUE : function requestTemperature() etc returns when conversion is ready
361377
// FALSE: function requestTemperature() etc returns immediately (USE WITH CARE!!)
@@ -429,16 +445,16 @@ bool DallasTemperature::requestTemperaturesByAddress(
429445
// Continue to check if the IC has responded with a temperature
430446
void DallasTemperature::blockTillConversionComplete(uint8_t bitResolution) {
431447

432-
unsigned long delms = millisToWaitForConversion(bitResolution);
433-
if (checkForConversion && !parasite) {
434-
unsigned long start = millis();
435-
while (!isConversionComplete() && (millis() - start < delms))
436-
yield();
437-
} else {
438-
activateExternalPullup();
439-
delay(delms);
440-
deactivateExternalPullup();
441-
}
448+
if (checkForConversion && !parasite) {
449+
unsigned long start = millis();
450+
while (!isConversionComplete() && (millis() - start < MAX_CONVERSION_TIMEOUT ))
451+
yield();
452+
} else {
453+
unsigned long delms = millisToWaitForConversion(bitResolution);
454+
activateExternalPullup();
455+
delay(delms);
456+
deactivateExternalPullup();
457+
}
442458

443459
}
444460

@@ -485,9 +501,7 @@ float DallasTemperature::getTempCByIndex(uint8_t deviceIndex) {
485501
if (!getAddress(deviceAddress, deviceIndex)) {
486502
return DEVICE_DISCONNECTED_C;
487503
}
488-
489504
return getTempC((uint8_t*) deviceAddress);
490-
491505
}
492506

493507
// Fetch temperature for device index
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,4 @@ void loop(void)
159159
*/
160160

161161
}
162-
162+
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,4 @@ void loop(void)
141141

142142
delay(1000);
143143
}
144-
144+

examples/Timing/Timing.ino

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//
2+
// FILE: Timing.ino
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.0.2
5+
// PURPOSE: show performance of DallasTemperature lib
6+
// compared to datasheet times per resolution
7+
//
8+
// HISTORY:
9+
// 0.0.1 = 2017-07-25 initial version
10+
// 0.0.2 = 2020-02-13 updates to work with current lib version
11+
12+
#include <OneWire.h>
13+
#include <DallasTemperature.h>
14+
15+
#define ONE_WIRE_BUS 2
16+
17+
OneWire oneWire(ONE_WIRE_BUS);
18+
DallasTemperature sensor(&oneWire);
19+
20+
uint32_t start, stop;
21+
22+
23+
void setup()
24+
{
25+
Serial.begin(9600);
26+
Serial.println(__FILE__);
27+
Serial.print("DallasTemperature Library version: ");
28+
Serial.println(DALLASTEMPLIBVERSION);
29+
30+
sensor.begin();
31+
}
32+
33+
void loop()
34+
{
35+
float ti[4] = { 94, 188, 375, 750 };
36+
37+
Serial.println();
38+
Serial.println("Test takes about 30 seconds for 4 resolutions");
39+
Serial.println("RES\tTIME\tACTUAL\tGAIN");
40+
for (int r = 9; r < 13; r++)
41+
{
42+
sensor.setResolution(r);
43+
44+
uint32_t duration = run(20);
45+
float avgDuration = duration / 20.0;
46+
47+
Serial.print(r);
48+
Serial.print("\t");
49+
Serial.print(ti[r - 9]);
50+
Serial.print("\t");
51+
Serial.print(avgDuration, 2);
52+
Serial.print("\t");
53+
Serial.print(avgDuration * 100 / ti[r - 9], 1);
54+
Serial.println("%");
55+
}
56+
delay(1000);
57+
}
58+
59+
uint32_t run(int runs)
60+
{
61+
float t;
62+
start = millis();
63+
for (int i = 0; i < runs; i++)
64+
{
65+
sensor.requestTemperatures();
66+
t = sensor.getTempCByIndex(0);
67+
}
68+
stop = millis();
69+
return stop - start;
70+
}

examples/WaitForConversion/WaitForConversion.pde renamed to examples/WaitForConversion/WaitForConversion.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,4 @@ void loop(void)
6363
Serial.println("\n\n\n\n");
6464

6565
delay(5000);
66-
}
66+
}

examples/WaitForConversion2/WaitForConversion2.pde renamed to examples/WaitForConversion2/WaitForConversion2.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,4 @@ void loop(void)
7777
// for the demo we just count the idle time in millis
7878
delay(1);
7979
idle++;
80-
}
80+
}

0 commit comments

Comments
 (0)