11//
22// FILE: DS18B20_INT.cpp
33// AUTHOR: Rob.Tillaart
4- // VERSION: 0.3.4
4+ // VERSION: 0.3.5
55// DATE: 2017-07-25
66// PURPOSE: library for DS18B20 temperature sensor - integer only.
77// URL: https://github.com/RobTillaart/DS18B20_INT
@@ -51,8 +51,8 @@ bool DS18B20_INT::isConnected(uint8_t retries)
5151 _oneWire->reset_search ();
5252 _deviceAddress[0 ] = 0x00 ;
5353 _oneWire->search (_deviceAddress);
54- _addressFound = _deviceAddress[0 ] != 0x00 &&
55- _oneWire->crc8 (_deviceAddress, 7 ) == _deviceAddress[7 ];
54+ _addressFound = ( _deviceAddress[0 ] != 0x00 ) &&
55+ ( _oneWire->crc8 (_deviceAddress, 7 ) == _deviceAddress[7 ]) ;
5656 }
5757 return _addressFound;
5858}
@@ -85,16 +85,25 @@ bool DS18B20_INT::isConversionComplete(void)
8585}
8686
8787
88- int16_t DS18B20_INT::getTempC (bool connectCheck )
88+ int16_t DS18B20_INT::getTempC (bool checkConnect )
8989{
90- if (connectCheck)
90+ ScratchPad scratchPad;
91+ if (checkConnect)
9192 {
9293 if (isConnected (3 ) == false )
9394 {
9495 return DEVICE_DISCONNECTED;
9596 }
9697 }
97- int16_t rawTemperature = _readRaw ();
98+ readScratchPad (scratchPad, 2 );
99+ // Power On Reset 85C error cannot be tested here
100+ // the 127.94 error can be checked here
101+ if ((scratchPad[1 ] == 0x07 ) && (scratchPad[0 ] == 0xFF ))
102+ {
103+ return DEVICE_GND_ERROR;
104+ }
105+
106+ int16_t rawTemperature = (((int16_t )scratchPad[1 ]) << 8 ) | scratchPad[0 ];
98107 rawTemperature >>= 4 ;
99108 if (rawTemperature < -55 )
100109 {
@@ -123,18 +132,26 @@ uint8_t DS18B20_INT::getResolution()
123132
124133int16_t DS18B20_INT::getTempCentiC (void )
125134{
135+ ScratchPad scratchPad;
126136 if (isConnected (3 ) == false )
127137 {
128- return DEVICE_DISCONNECTED;
138+ return DEVICE_DISCONNECTED * 100 ;
139+ }
140+ readScratchPad (scratchPad, 2 );
141+ // Power On Reset 85C error cannot be tested here
142+ // the 127.94 error can be checked here
143+ if ((scratchPad[1 ] == 0x07 ) && (scratchPad[0 ] == 0xFF ))
144+ {
145+ return DEVICE_GND_ERROR * 100 ;
129146 }
130- int16_t rawTemperature = _readRaw () ;
147+ int16_t rawTemperature = ((( int16_t )scratchPad[ 1 ]) << 8 ) | scratchPad[ 0 ] ;
131148 // rawTemperature = rawTemperature * 100 / 16;
132149 rawTemperature *= 25 ;
133- rawTemperature >>= 2 ;
150+ rawTemperature >>= 4 ;
134151 // use at own risk. (not tested)
135152 if (rawTemperature < -5500 )
136153 {
137- return DEVICE_DISCONNECTED * 100 ;
154+ return DEVICE_DISCONNECTED;
138155 }
139156 return rawTemperature;
140157}
@@ -144,16 +161,17 @@ int16_t DS18B20_INT::getTempCentiC(void)
144161//
145162// PRIVATE
146163//
147- int16_t DS18B20_INT::_readRaw ( void )
164+ void DS18B20_INT::readScratchPad ( uint8_t *scratchPad, uint8_t fields )
148165{
149166 _oneWire->reset ();
150167 _oneWire->select (_deviceAddress);
151168 _oneWire->write (READSCRATCH);
152169
153- int16_t rawTemperature = ((int16_t )_oneWire->read ());
154- rawTemperature |= _oneWire->read () << 8 ;
170+ for (uint8_t i = 0 ; i < fields; i++)
171+ {
172+ scratchPad[i] = _oneWire->read ();
173+ }
155174 _oneWire->reset ();
156- return rawTemperature;
157175}
158176
159177
0 commit comments