[2.0.7] DS18B20 lib Error

Discussion about Z-Uno product. Visit http://z-uno.z-wave.me for more details.
Post Reply
michap
Posts: 437
Joined: 26 Mar 2013 10:35
Contact:

[2.0.7] DS18B20 lib Error

Post by michap »

Hi,

new DS18B20 lib have a mistake inside for negative values.
We have now here in Berlin again negative temperature values outside - so I see it just now only....

Temperature is falling down to 0.00 degree - then is coming up a value about 35.9 degree
(using the original sample code)

Scanning is working great - have connected 7 sensors wthout problems.

Michael
p0lyg0n1
Posts: 242
Joined: 04 Aug 2016 07:14

Re: [2.0.7] DS18B20 lib Error

Post by p0lyg0n1 »

Looks like I forgot something. :?
A lots of copy-pastes...

So, the quick fix is (please test it with real ds18b20 and negative temperature):


Modify ZUNO_DS18B20.cpp:

line 127:
temp = dallas_data[1];
instead of
temp = dallas_data[1] & 0x07;

remove/comment lines 149,150:

// if(dallas_data[1] & 0x80)
// temp = -temp;



So the whole code of getTempC100 will be:

Code: Select all

int DS18B20Sensor::getTempC100(byte * addr)
{
	int temp = BAD_TEMP;
	byte dallas_data[9];
	byte i;

	if (!my_ow->reset())
        return temp;
   
   	if(addr)
    	my_ow->select(addr);
    else
    	my_ow->skip();

    my_ow->write(0x44);

    #if DEBUG_TEMP_CONV
    Serial0.print( " Current delay:");
    Serial0.print(current_delay);
    Serial0.print( " Current resolution:");
    Serial0.print(current_resolution);
    #endif

    delay(int(current_delay*10)); 

    if(!my_ow->reset())
    	return temp;

    if(addr)
    	my_ow->select(addr);
    else
    	my_ow->skip();
        
    my_ow->write(0xBE);         // Read Scratchpad

    #if DEBUG_TEMP_CONV
    Serial0.print( " RAW:{ ");
    #endif

    for ( i = 0; i < 9; i++) 
    {           
      // we need 9 bytes
      dallas_data[i] = my_ow->read();
      #if DEBUG_TEMP_CONV
      Serial0.print(dallas_data[i], HEX);
      Serial0.print(" ");
      #endif
    }
    #if DEBUG_TEMP_CONV
    Serial0.println("}");
    #endif

    temp = dallas_data[1]; // & 0x07; // For all other bits we have 1 when temperature < 0
    temp <<= 8;
    temp |= dallas_data[0];

    // extract current resolution from control register
    current_resolution = (dallas_data[4] >> 5) & 0x03;
    // current_delay ~ 10*2^resolution
    current_delay = 10;
    current_delay <<= current_resolution;
    // fill low bits with zeros
    // using current resolution
    temp &= ~((1 << (3 - current_resolution))-1);

    // We use fixed point calculations here
    // so the following sequence
    // temp /= 0.16 
    // will be
    // 16/100 => 4/25 => temp *= 25; tem/=4;
    temp *= 25;
    temp >>= 2;

    // Fix sign of value
    // if(dallas_data[1] & 0x80)
    //    temp = -temp;
    

    return temp;
}
It have to work...
michap
Posts: 437
Joined: 26 Mar 2013 10:35
Contact:

Re: [2.0.7] DS18B20 lib Error

Post by michap »

Hi,

yes, I already made the same change in my lib before - was only not sure - is it 100% right ;)
Wanted tomorrow doublecheck at first..

So this changes are working fine.

Michael
michap
Posts: 437
Joined: 26 Mar 2013 10:35
Contact:

Re: [2.0.7] DS18B20 lib Error

Post by michap »

Additional short hint:

a) In current lib all sensors should use same resolution to be sure to get right results.

@p0lyg0n1: defined resolution from register of last sensor will be used for the next one ? - an array could help.
Or request at first resolution and then define delay and request temperature.

b) for first reading (as sample using for sleeping device) will be taken the resolution 9bit in any case

@p0lyg0n1: Additional suggestion: For reducing operation time for sleeping devices maybe good idea to have the option to set sensor parameter resolution direct in code (same as address)

Michael
p0lyg0n1
Posts: 242
Joined: 04 Aug 2016 07:14

Re: [2.0.7] DS18B20 lib Error

Post by p0lyg0n1 »

I think I will add a method to set resolution manually. The default value have to be 12bit(factory default). Thank you for your help, Michael.
deg026
Posts: 18
Joined: 06 Jan 2017 08:23

Re: [2.0.7] DS18B20 lib Error

Post by deg026 »

confirm the same bug with negative values of temperature sensor is still exist in current setup
now will try to solve it with ZUNO_DS18B20 lib fix!

i think you must update distributive, somebody can not be so good in search and didn't find this thread
Tnx!
User avatar
PoltoS
Posts: 7562
Joined: 26 Jan 2011 19:36

Re: [2.0.7] DS18B20 lib Error

Post by PoltoS »

We will release it with 2.0.8
deg026
Posts: 18
Joined: 06 Jan 2017 08:23

Re: [2.0.7] DS18B20 lib Error

Post by deg026 »

i think i found another small bug about negative temp values in your examples sketch
"Multiple DS18B20 temperature sensors"
for store temperature it use WORD type o variable

word temperature[N_SENSOR]; // Here we store temperatures

but as i know it is unsigned, right?
so i think it must be int, like that:

int temperature[N_SENSOR];
User avatar
PoltoS
Posts: 7562
Joined: 26 Jan 2011 19:36

Re: [2.0.7] DS18B20 lib Error

Post by PoltoS »

If your final (signed) result is returned as WORD, Z-Wave controller will still interpret it as signed word. So this should be ok. But indeed, this confuses a bit.
Post Reply