Skip to content

Commit 0b69c9b

Browse files
author
Arnd
authored
Minimized conversion wait times
If the last command issued on the 1-Wire is a "convert temperature", then reads can be done until the value returned is 1, which means that the conversion is finished. The conversion times in the manuals are maximum values, actual conversion times are much faster and the only reason to delay the maximum times are when one or more devices are parasitically powered.
1 parent 8e68828 commit 0b69c9b

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

DSFamily.cpp

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ DSFamily_Class::~DSFamily_Class() { } //
6767
*******************************************************************************************************************/
6868
uint8_t DSFamily_Class::ScanForDevices() { // //
6969
uint8_t tempTherm[8]; // Temporary DS address array //
70+
_LastCommandWasConvert = false; // Set switch to false //
7071
reset_search(); // Reset the search status //
7172
ThermometersFound = 0; // Reset the number of thermometers //
7273
while (search(tempTherm)) { // Use the 1-Wire "search" method //
@@ -92,6 +93,7 @@ uint8_t DSFamily_Class::ScanForDevices() { //
9293
*******************************************************************************************************************/
9394
boolean DSFamily_Class::Read1WireScratchpad(const uint8_t deviceNumber, // //
9495
uint8_t buffer[9]) { // //
96+
_LastCommandWasConvert = false; // Set switch to false //
9597
SelectDevice(deviceNumber); // Reset the 1-wire, address device //
9698
write_byte(DS_READ_SCRATCHPAD); // Request device send Scratchpad //
9799
for (uint8_t i=0;i<9;i++) buffer[i] = read_byte(); // read all 9 bytes sent by DS //
@@ -110,8 +112,10 @@ int16_t DSFamily_Class::ReadDeviceTemp(const uint8_t deviceNumber, //
110112
const bool raw=false) { // //
111113
uint8_t dsBuffer[9]; // Buffer to hold scratchpad return //
112114
int16_t temperature = DS_BAD_TEMPERATURE; // Holds return value //
113-
if ((_ConvStartTime+ConversionMillis)>millis()) // If a conversion is still running //
115+
if (Parasitic || !_LastCommandWasConvert) { // Wait a fixed time in parasite or //
116+
if ((_ConvStartTime+ConversionMillis)>millis()) // If a conversion is still running //
114117
delay(millis()-(_ConvStartTime+ConversionMillis)); // then wait until it is finished //
118+
} else if (_LastCommandWasConvert) while(read_bit()==0); // bit high when conversion finished//
115119
if ( deviceNumber < ThermometersFound && // on a successful read from the //
116120
Read1WireScratchpad(deviceNumber,dsBuffer)) { // device scratchpad //
117121
if (dsBuffer[0]==DS1822_FAMILY) { // The results returned by DS18S20 //
@@ -126,17 +130,21 @@ int16_t DSFamily_Class::ReadDeviceTemp(const uint8_t deviceNumber, //
126130

127131
/*******************************************************************************************************************
128132
** method DeviceStartConvert() to start the sampling and conversion on a device. At maximum resolution this **
129-
** conversion can take 750ms. If the optional parameter is not specified then all device conversions are started **
130-
** at the same time **
133+
** conversion can take 750ms. If the optional deviceNumber is not specified then all device conversions are **
134+
** started at the same time. If the optional WaitSwitch parameter is set to "true" then call doesn't return until **
135+
** the conversion has completed **
131136
*******************************************************************************************************************/
132-
void DSFamily_Class::DeviceStartConvert(const uint8_t deviceNumber=UINT8_MAX){// //
137+
void DSFamily_Class::DeviceStartConvert(const uint8_t deviceNumber=UINT8_MAX, // //
138+
const bool WaitSwitch = false) { // //
133139
ParasiticWait(); // Wait for conversion if necessary //
134140
if (deviceNumber==UINT8_MAX) { // If no parameter specified, use //
135141
reset(); // Reset 1-wire network //
136142
write_byte(DS_SKIP_ROM); // Tell all devices to listen //
137143
} else SelectDevice(deviceNumber); // of if-then all devices or just one // //
138144
write_byte(DS_START_CONVERT); // Initiate temperature conversion //
139145
_ConvStartTime = millis(); // Store start time of conversion //
146+
_LastCommandWasConvert = true; // Set switch to true //
147+
if (!Parasitic && WaitSwitch) while(read_bit()==0); // Read bit goes high when finished //
140148
} // of method DeviceStartConvert //----------------------------------//
141149

142150
/*******************************************************************************************************************
@@ -161,6 +169,7 @@ void DSFamily_Class::Calibrate(const uint8_t iterations=30) { //
161169
int64_t tempSum = 0; // Stores interim values //
162170
int8_t offset = 0; // Stores the computed offset value //
163171
uint8_t ThermometersUsed = min(DS_MAX_THERMOMETERS,ThermometersFound); // Use the lower of the 2 values //
172+
_LastCommandWasConvert = false; // Set switch to false //
164173
for (uint8_t i=0;i<iterations;i++) { // Loop to get a good sampling //
165174
for(uint8_t x=0;x<ThermometersUsed;x++) { // process each thermometer //
166175
stats1[x] += ReadDeviceTemp(x,true); // read raw temperature, no offset //
@@ -182,6 +191,7 @@ void DSFamily_Class::Calibrate(const uint8_t iterations=30) { //
182191
void DSFamily_Class::SetDeviceCalibration(const uint8_t deviceNumber, // //
183192
const int8_t offset) { // //
184193
uint8_t dsBuffer[9]; // Temporary scratchpad buffer //
194+
_LastCommandWasConvert = false; // Set switch to false //
185195
Read1WireScratchpad(deviceNumber,dsBuffer); // Read from the device scratchpad //
186196
SelectDevice(deviceNumber); // Reset 1-wire, address device //
187197
write_byte(DS_WRITE_SCRATCHPAD); // Write scratchpad, send 3 bytes //
@@ -199,6 +209,7 @@ void DSFamily_Class::SetDeviceCalibration(const uint8_t deviceNumber, //
199209
int8_t DSFamily_Class::GetDeviceCalibration(const uint8_t deviceNumber) { // //
200210
int8_t offset = INT8_MIN; // Default to an invalid value //
201211
uint8_t dsBuffer[9]; // Temporary scratchpad buffer //
212+
_LastCommandWasConvert = false; // Set switch to false //
202213
Read1WireScratchpad(deviceNumber,dsBuffer); // Read from the device scratchpad //
203214
if (dsBuffer[2]^dsBuffer[3]==0xFF) offset = (int8_t)dsBuffer[2]; // Use the calibration value //
204215
return (offset); // Return the computed offset //
@@ -221,6 +232,7 @@ void DSFamily_Class::SelectDevice(const uint8_t deviceNumber) { //
221232
*******************************************************************************************************************/
222233
void DSFamily_Class::GetDeviceROM(const uint8_t deviceNumber, // //
223234
uint8_t ROMBuffer[8]) { // Array to hold unique DS ID //
235+
_LastCommandWasConvert = false; // Set switch to false //
224236
for (uint8_t i=0;i<8;i++) // loop for each byte in the buffer //
225237
ROMBuffer[i]=EEPROM.read(i+E2END-((deviceNumber+1)*8)); // Read the EEPROM byte //
226238
} // of method GetDeviceROM() //----------------------------------//
@@ -287,6 +299,7 @@ int16_t DSFamily_Class::AvgTemperature(const uint8_t skipDeviceNumber=UINT8_MAX)
287299
void DSFamily_Class::SetDeviceResolution(const uint8_t deviceNumber, // Set resolution to 9,10,11 or 12 b//
288300
uint8_t resolution) { // //
289301
uint8_t dsBuffer[9]; // Buffer to hold scratchpad values //
302+
_LastCommandWasConvert = false; // Set switch to false //
290303
if(resolution<9 || resolution>12) resolution = 12; // Default to full resolution //
291304
switch (resolution) { // Now select which action to do //
292305
case 12: // When 12 bits of precision used //
@@ -319,11 +332,12 @@ void DSFamily_Class::SetDeviceResolution(const uint8_t deviceNumber, //
319332
** Method SetUserBytes to set the user bytes 1 and 2 to the calibration computed **
320333
*******************************************************************************************************************/
321334
uint8_t DSFamily_Class::GetDeviceResolution(const uint8_t deviceNumber) { // //
322-
uint8_t resolution; // Store the return value //
323-
uint8_t dsBuffer[9]; // Hold scratchpad return values //
324-
Read1WireScratchpad(deviceNumber,dsBuffer); // Read from the device scratchpad //
325-
resolution = (dsBuffer[DS_CONFIG_BYTE]>>5)+9; // get bits 6&7 from the config byte//
326-
return(resolution); // //
335+
uint8_t resolution; // Store the return value //
336+
uint8_t dsBuffer[9]; // Hold scratchpad return values //
337+
_LastCommandWasConvert = false; // Set switch to false //
338+
Read1WireScratchpad(deviceNumber,dsBuffer); // Read from the device scratchpad //
339+
resolution = (dsBuffer[DS_CONFIG_BYTE]>>5)+9; // get bits 6&7 from the config byte//
340+
return(resolution); // //
327341
} // of method GetDeviceResolution() //----------------------------------//
328342

329343
/*******************************************************************************************************************

DSFamily.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
** **
5252
** Vers. Date Developer Comments **
5353
** ====== ========== =================== ======================================================================== **
54+
** 1.0.2 2016-12-03 [email protected] Added optional ReadDeviceTemp "WaitSwitch", minimized conversion delays **
5455
** 1.0.1 2016-12-02 [email protected] Added delays for ReadDeviceTemp() and when a parasitic device is present **
5556
** 1.0.0 2016-12-01 [email protected] Initial release **
5657
** 1.0.b5 2016-11-30 [email protected] Moved 1-Wire calls to private, refactored some of the calls **
@@ -136,7 +137,8 @@
136137
uint8_t ScanForDevices (); // Scan/rescan the 1-Wire microLAN //
137138
int16_t ReadDeviceTemp (const uint8_t deviceNumber, // Return the temperature //
138139
const bool raw=false); // optionally using the raw value //
139-
void DeviceStartConvert (const uint8_t deviceNumber=UINT8_MAX); // Start conversion on device //
140+
void DeviceStartConvert (const uint8_t deviceNumber=UINT8_MAX, // Start conversion on device //
141+
const bool WaitSwitch=false); // optionally wait for it to finish //
140142
void Calibrate (const uint8_t iterations=30); // Calibrate to read identically //
141143
int8_t GetDeviceCalibration(const uint8_t deviceNumber); // Get the device's calibration //
142144
void SetDeviceCalibration(const uint8_t deviceNumber, // Set calibration bytes 1 & 2 //
@@ -156,6 +158,7 @@
156158
void ParasiticWait(); // Wait for conversion if parasitic //
157159
uint8_t _MaxThermometers; // Devices fit (EEPROM-ReserveRom) //
158160
uint32_t _ConvStartTime; // Conversion start time //
161+
bool _LastCommandWasConvert=false; // Unset when other commands issued //
159162
IO_REG_TYPE bitmask; // //
160163
volatile IO_REG_TYPE *baseReg; // //
161164
unsigned char ROM_NO[8]; // global search state //

0 commit comments

Comments
 (0)