Skip to content

Commit e46ceec

Browse files
committed
2 parents 8550b9a + 9a1a2e4 commit e46ceec

File tree

4 files changed

+253
-19
lines changed

4 files changed

+253
-19
lines changed

DallasTemperature.cpp

Lines changed: 93 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// This library is free software; you can redistribute it and/or
1+
// This library is free software; you can redistribute it and/or
22
// modify it under the terms of the GNU Lesser General Public
33
// License as published by the Free Software Foundation; either
44
// version 2.1 of the License, or (at your option) any later version.
@@ -93,6 +93,7 @@ void DallasTemperature::setOneWire(OneWire* _oneWire) {
9393
bitResolution = 9;
9494
waitForConversion = true;
9595
checkForConversion = true;
96+
autoSaveScratchPad = true;
9697

9798
}
9899

@@ -216,20 +217,10 @@ void DallasTemperature::writeScratchPad(const uint8_t* deviceAddress,
216217
if (deviceAddress[0] != DS18S20MODEL)
217218
_wire->write(scratchPad[CONFIGURATION]);
218219

219-
_wire->reset();
220-
221-
// save the newly written values to eeprom
222-
_wire->select(deviceAddress);
223-
_wire->write(COPYSCRATCH, parasite);
224-
delay(20); // <--- added 20ms delay to allow 10ms long EEPROM write operation (as specified by datasheet)
225-
226-
if (parasite) {
227-
activateExternalPullup();
228-
delay(10); // 10ms delay
229-
deactivateExternalPullup();
230-
}
220+
if (autoSaveScratchPad)
221+
saveScratchPad(deviceAddress);
222+
else
231223
_wire->reset();
232-
233224
}
234225

235226
// returns true if parasite mode is used (2 wire)
@@ -474,6 +465,94 @@ int16_t DallasTemperature::millisToWaitForConversion(uint8_t bitResolution) {
474465

475466
}
476467

468+
// Sends command to one device to save values from scratchpad to EEPROM by index
469+
// Returns true if no errors were encountered, false indicates failure
470+
bool DallasTemperature::saveScratchPadByIndex(uint8_t deviceIndex) {
471+
472+
DeviceAddress deviceAddress;
473+
if (!getAddress(deviceAddress, deviceIndex)) return false;
474+
475+
return saveScratchPad(deviceAddress);
476+
477+
}
478+
479+
// Sends command to one or more devices to save values from scratchpad to EEPROM
480+
// If optional argument deviceAddress is omitted the command is send to all devices
481+
// Returns true if no errors were encountered, false indicates failure
482+
bool DallasTemperature::saveScratchPad(const uint8_t* deviceAddress) {
483+
484+
if (_wire->reset() == 0)
485+
return false;
486+
487+
if (deviceAddress == nullptr)
488+
_wire->skip();
489+
else
490+
_wire->select(deviceAddress);
491+
492+
_wire->write(COPYSCRATCH,parasite);
493+
494+
// Specification: NV Write Cycle Time is typically 2ms, max 10ms
495+
// Waiting 20ms to allow for sensors that take longer in practice
496+
if (!parasite) {
497+
delay(20);
498+
} else {
499+
activateExternalPullup();
500+
delay(20);
501+
deactivateExternalPullup();
502+
}
503+
504+
return _wire->reset() == 1;
505+
506+
}
507+
508+
// Sends command to one device to recall values from EEPROM to scratchpad by index
509+
// Returns true if no errors were encountered, false indicates failure
510+
bool DallasTemperature::recallScratchPadByIndex(uint8_t deviceIndex) {
511+
512+
DeviceAddress deviceAddress;
513+
if (!getAddress(deviceAddress, deviceIndex)) return false;
514+
515+
return recallScratchPad(deviceAddress);
516+
517+
}
518+
519+
// Sends command to one or more devices to recall values from EEPROM to scratchpad
520+
// If optional argument deviceAddress is omitted the command is send to all devices
521+
// Returns true if no errors were encountered, false indicates failure
522+
bool DallasTemperature::recallScratchPad(const uint8_t* deviceAddress) {
523+
524+
if (_wire->reset() == 0)
525+
return false;
526+
527+
if (deviceAddress == nullptr)
528+
_wire->skip();
529+
else
530+
_wire->select(deviceAddress);
531+
532+
_wire->write(RECALLSCRATCH,parasite);
533+
534+
// Specification: Strong pullup only needed when writing to EEPROM (and temp conversion)
535+
unsigned long start = millis();
536+
while (_wire->read_bit() == 0) {
537+
// Datasheet doesn't specify typical/max duration, testing reveals typically within 1ms
538+
if (millis() - start > 20) return false;
539+
yield();
540+
}
541+
542+
return _wire->reset() == 1;
543+
544+
}
545+
546+
// Sets the autoSaveScratchPad flag
547+
void DallasTemperature::setAutoSaveScratchPad(bool flag) {
548+
autoSaveScratchPad = flag;
549+
}
550+
551+
// Gets the autoSaveScratchPad flag
552+
bool DallasTemperature::getAutoSaveScratchPad() {
553+
return autoSaveScratchPad;
554+
}
555+
477556
void DallasTemperature::activateExternalPullup() {
478557
if(useExternalPullup)
479558
digitalWrite(pullupPin, LOW);

DallasTemperature.h

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
// Model IDs
2929
#define DS18S20MODEL 0x10 // also DS1820
30-
#define DS18B20MODEL 0x28
30+
#define DS18B20MODEL 0x28 // also MAX31820
3131
#define DS1822MODEL 0x22
3232
#define DS1825MODEL 0x3B
3333
#define DS28EA00MODEL 0x42
@@ -140,7 +140,29 @@ class DallasTemperature {
140140
// Is a conversion complete on the wire? Only applies to the first sensor on the wire.
141141
bool isConversionComplete(void);
142142

143-
int16_t millisToWaitForConversion(uint8_t);
143+
int16_t millisToWaitForConversion(uint8_t);
144+
145+
// Sends command to one device to save values from scratchpad to EEPROM by index
146+
// Returns true if no errors were encountered, false indicates failure
147+
bool saveScratchPadByIndex(uint8_t);
148+
149+
// Sends command to one or more devices to save values from scratchpad to EEPROM
150+
// Returns true if no errors were encountered, false indicates failure
151+
bool saveScratchPad(const uint8_t* = nullptr);
152+
153+
// Sends command to one device to recall values from EEPROM to scratchpad by index
154+
// Returns true if no errors were encountered, false indicates failure
155+
bool recallScratchPadByIndex(uint8_t);
156+
157+
// Sends command to one or more devices to recall values from EEPROM to scratchpad
158+
// Returns true if no errors were encountered, false indicates failure
159+
bool recallScratchPad(const uint8_t* = nullptr);
160+
161+
// Sets the autoSaveScratchPad flag
162+
void setAutoSaveScratchPad(bool);
163+
164+
// Gets the autoSaveScratchPad flag
165+
bool getAutoSaveScratchPad(void);
144166

145167
#if REQUIRESALARMS
146168

@@ -237,6 +259,9 @@ class DallasTemperature {
237259
// used to requestTemperature to dynamically check if a conversion is complete
238260
bool checkForConversion;
239261

262+
// used to determine if values will be saved from scratchpad to EEPROM on every scratchpad write
263+
bool autoSaveScratchPad;
264+
240265
// count of devices on the bus
241266
uint8_t devices;
242267

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
//
2+
// FILE: SaveRecallScratchPad.ino
3+
// AUTHOR: GitKomodo
4+
// VERSION: 0.0.1
5+
// PURPOSE: Show DallasTemperature lib functionality to
6+
// save/recall ScratchPad values to/from EEPROM
7+
//
8+
// HISTORY:
9+
// 0.0.1 = 2020-02-18 initial version
10+
//
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 sensors(&oneWire);
19+
DeviceAddress deviceAddress;
20+
21+
void setup()
22+
{
23+
Serial.begin(9600);
24+
Serial.println(__FILE__);
25+
Serial.println("Dallas Temperature Demo");
26+
27+
sensors.begin();
28+
29+
// Get ID of first sensor (at index 0)
30+
sensors.getAddress(deviceAddress,0);
31+
32+
// By default configuration and alarm/userdata registers are also saved to EEPROM
33+
// when they're changed. Sensors recall these values automatically when powered up.
34+
35+
// Turn OFF automatic saving of configuration and alarm/userdata registers to EEPROM
36+
sensors.setAutoSaveScratchPad(false);
37+
38+
// Change configuration and alarm/userdata registers on the scratchpad
39+
int8_t resolution = 12;
40+
sensors.setResolution(deviceAddress,resolution);
41+
int16_t userdata = 24680;
42+
sensors.setUserData(deviceAddress,userdata);
43+
44+
// Save configuration and alarm/userdata registers to EEPROM
45+
sensors.saveScratchPad(deviceAddress);
46+
47+
// saveScratchPad can also be used without a parameter to save the configuration
48+
// and alarm/userdata registers of ALL connected sensors to EEPROM:
49+
//
50+
// sensors.saveScratchPad();
51+
//
52+
// Or the configuration and alarm/userdata registers of a sensor can be saved to
53+
// EEPROM by index:
54+
//
55+
// sensors.saveScratchPadByIndex(0);
56+
57+
// Print current values on the scratchpad (resolution = 12, userdata = 24680)
58+
printValues();
59+
60+
}
61+
62+
void loop(){
63+
64+
// Change configuration and alarm/userdata registers on the scratchpad
65+
int8_t resolution = 10;
66+
sensors.setResolution(deviceAddress,resolution);
67+
int16_t userdata = 12345;
68+
sensors.setUserData(deviceAddress,userdata);
69+
70+
// Print current values on the scratchpad (resolution = 10, userdata = 12345)
71+
printValues();
72+
73+
delay(2000);
74+
75+
// Recall configuration and alarm/userdata registers from EEPROM
76+
sensors.recallScratchPad(deviceAddress);
77+
78+
// recallScratchPad can also be used without a parameter to recall the configuration
79+
// and alarm/userdata registers of ALL connected sensors from EEPROM:
80+
//
81+
// sensors.recallScratchPad();
82+
//
83+
// Or the configuration and alarm/userdata registers of a sensor can be recalled
84+
// from EEPROM by index:
85+
//
86+
// sensors.recallScratchPadByIndex(0);
87+
88+
// Print current values on the scratchpad (resolution = 12, userdata = 24680)
89+
printValues();
90+
91+
delay(2000);
92+
93+
}
94+
95+
void printValues() {
96+
97+
Serial.println();
98+
Serial.println("Current values on the scratchpad:");
99+
100+
Serial.print("Resolution:\t");
101+
Serial.println(sensors.getResolution(deviceAddress));
102+
103+
Serial.print("User data:\t");
104+
Serial.println(sensors.getUserData(deviceAddress));
105+
106+
}

keywords.txt

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,44 @@ DeviceAddress KEYWORD1
1414
# Methods and Functions (KEYWORD2)
1515
#######################################
1616

17+
setOneWire KEYWORD2
18+
setPullupPin KEYWORD2
1719
setResolution KEYWORD2
1820
getResolution KEYWORD2
21+
getTemp KEYWORD2
1922
getTempC KEYWORD2
2023
toFahrenheit KEYWORD2
2124
getTempF KEYWORD2
2225
getTempCByIndex KEYWORD2
2326
getTempFByIndex KEYWORD2
27+
rawToCelsius KEYWORD2
28+
rawToFahrenheit KEYWORD2
2429
setWaitForConversion KEYWORD2
2530
getWaitForConversion KEYWORD2
2631
requestTemperatures KEYWORD2
2732
requestTemperaturesByAddress KEYWORD2
2833
requestTemperaturesByIndex KEYWORD2
34+
setCheckForConversion KEYWORD2
35+
getCheckForConversion KEYWORD2
36+
isConversionComplete KEYWORD2
37+
millisToWaitForConversion KEYWORD2
2938
isParasitePowerMode KEYWORD2
3039
begin KEYWORD2
3140
getDeviceCount KEYWORD2
41+
getDS18Count KEYWORD2
3242
getAddress KEYWORD2
3343
validAddress KEYWORD2
44+
validFamily KEYWORD2
3445
isConnected KEYWORD2
3546
readScratchPad KEYWORD2
3647
writeScratchPad KEYWORD2
3748
readPowerSupply KEYWORD2
49+
saveScratchPadByIndex KEYWORD2
50+
saveScratchPad KEYWORD2
51+
recallScratchPadByIndex KEYWORD2
52+
recallScratchPad KEYWORD2
53+
setAutoSaveScratchPad KEYWORD2
54+
getAutoSaveScratchPad KEYWORD2
3855
setHighAlarmTemp KEYWORD2
3956
setLowAlarmTemp KEYWORD2
4057
getHighAlarmTemp KEYWORD2
@@ -43,12 +60,19 @@ resetAlarmSearch KEYWORD2
4360
alarmSearch KEYWORD2
4461
hasAlarm KEYWORD2
4562
toCelsius KEYWORD2
46-
processAlarmss KEYWORD2
47-
setAlarmHandlers KEYWORD2
48-
defaultAlarmHandler KEYWORD2
63+
processAlarms KEYWORD2
64+
setAlarmHandler KEYWORD2
65+
hasAlarmHandler KEYWORD2
66+
setUserData KEYWORD2
67+
setUserDataByIndex KEYWORD2
68+
getUserData KEYWORD2
69+
getUserDataByIndex KEYWORD2
4970
calculateTemperature KEYWORD2
5071

5172
#######################################
5273
# Constants (LITERAL1)
5374
#######################################
5475

76+
DEVICE_DISCONNECTED_C LITERAL1
77+
DEVICE_DISCONNECTED_F LITERAL1
78+
DEVICE_DISCONNECTED_RAW LITERAL1

0 commit comments

Comments
 (0)