Skip to content

Commit a567aa4

Browse files
ArndArnd
authored andcommitted
Fixed DS1822 bug
Corrected detection of DS1822 device on 1-Wire MicroLAN and corresponding temperature conversion error
1 parent f8d8056 commit a567aa4

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

DSFamily.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,15 @@ uint8_t DSFamily_Class::ScanForDevices() { //
9494
boolean DSFamily_Class::Read1WireScratchpad(const uint8_t deviceNumber, // //
9595
uint8_t buffer[9]) { // //
9696
_LastCommandWasConvert = false; // Set switch to false //
97-
SelectDevice(deviceNumber); // Reset the 1-wire, address device //
98-
write_byte(DS_READ_SCRATCHPAD); // Request device send Scratchpad //
99-
for (uint8_t i=0;i<9;i++) buffer[i] = read_byte(); // read all 9 bytes sent by DS //
100-
return(crc8(buffer,8) == buffer[8] ); // Return false if bad CRC checksum //
97+
bool CRCStatus = false; // default to a bad reading //
98+
uint8_t ErrorCounter = 0; // Count number of bad readings //
99+
while (!CRCStatus && ErrorCounter++ < 10) { // Loop until good read or overflow //
100+
SelectDevice(deviceNumber); // Reset the 1-wire, address device //
101+
write_byte(DS_READ_SCRATCHPAD); // Request device send Scratchpad //
102+
for (uint8_t i=0;i<9;i++) buffer[i] = read_byte(); // read all 9 bytes sent by DS //
103+
CRCStatus = crc8(buffer,8) == buffer[8]; // Check to see if result is valid //
104+
} // of loop until good read or number of errors exceeded // //
105+
return(CRCStatus); // Return false if bad CRC checksum //
101106
} // of method Read1WireScratchpad() //----------------------------------//
102107

103108
/*******************************************************************************************************************
@@ -118,7 +123,7 @@ int16_t DSFamily_Class::ReadDeviceTemp(const uint8_t deviceNumber, //
118123
} else if (_LastCommandWasConvert) while(read_bit()==0); // bit high when conversion finished//
119124
if ( deviceNumber < ThermometersFound && // on a successful read from the //
120125
Read1WireScratchpad(deviceNumber,dsBuffer)) { // device scratchpad //
121-
if (dsBuffer[0]==DS1822_FAMILY) { // The results returned by DS18S20 //
126+
if (ROM_NO[0]==DS1822_FAMILY) { // If DS1822 then temp is different //
122127
temperature = ((dsBuffer[1] << 8) | dsBuffer[0])<<3; // get the raw reading and apply //
123128
temperature = (temperature & 0xFFF0) + 12 - dsBuffer[6]; // value from "count remain" byte //
124129
} else temperature = (dsBuffer[1]<<8)|dsBuffer[0]; // Results come in 2s complement //
@@ -227,12 +232,11 @@ int8_t DSFamily_Class::GetDeviceCalibration(const uint8_t deviceNumber) { //
227232
** Method SelectDevice will reset the 1-Wire microLAN and select the device number specified **
228233
*******************************************************************************************************************/
229234
void DSFamily_Class::SelectDevice(const uint8_t deviceNumber) { // //
230-
uint8_t ROMBuffer[8]; // Array to hold unique DS ID //
231235
ParasiticWait(); // Wait for conversion if necessary //
232236
for (uint8_t i=0;i<8;i++) // //
233-
ROMBuffer[i]=EEPROM.read(i+E2END-((deviceNumber+1)*8)); // Read the EEPROM byte //
237+
ROM_NO[i]=EEPROM.read(i+E2END-((deviceNumber+1)*8)); // Read the EEPROM byte //
234238
reset(); // Reset 1-wire communications //
235-
select(ROMBuffer); // Select only current device //
239+
select(ROM_NO); // Select only current device //
236240
} // of method SelectDevice() //----------------------------------//
237241

238242
/*******************************************************************************************************************

DSFamily.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
** **
5252
** Vers. Date Developer Comments **
5353
** ====== ========== =================== ======================================================================== **
54+
** 1.0.4 2016-12-29 [email protected] Added error loop to Read1WireScratchpad(), corrected DS18S20 call in **
55+
ReadDeviceTemp() function to avoid false temperatures **
5456
** 1.0.3 2016-12-16 [email protected] Added optional CalibrationTemp to Calibrate function **
5557
** 1.0.2 2016-12-03 [email protected] Added optional ReadDeviceTemp "WaitSwitch", minimized conversion delays **
5658
** 1.0.1 2016-12-02 [email protected] Added delays for ReadDeviceTemp() and when a parasitic device is present **

0 commit comments

Comments
 (0)