Hoi friends,
i am trying to read Temperature sensors DS1820 (the very old one) and the new DS18x20 (DS18S20, DS18B20) on the same bus.
Due they have different parameters i played around to get the read all together on the same bus (pin).
Here is my code so far:
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire ds(17); //pin für ds1820
/*
0x10, 0x98, 0xB4, 0x3C, 00x0, 00x8, 00x0, 0x97
0x10, 0x67, 0xC0, 0x3C, 00x0, 00x8, 00x0, 0xB3
0x28, 0x53, 0x91, 0xCA, [color=brown]00x4, 00x0, 00x0[/color], 0xAF
*/
//DeviceAdresses
DeviceAddress sensor1 = { 0x10, 0x98, 0xB4, 0x3C, 0x0, 0x8, 0x0, 0x97 };
DeviceAddress sensor2 = { 0x10, 0x67, 0xC0, 0x3C, 0x0, 0x8, 0x0, 0xB3 };
DeviceAddress sensor3 = { 0x28, 0x53, 0x91, 0xCA, [color=brown]0x4, 0x0, 0x0[/color], 0xAF };
// DeviceAddress sensor3 = { 0x28, 0x53, 0x91, 0xCA, [color=brown]00x4, 00x0, 00x0[/color], 0xAF }; - causes error while compiling
char sensor1Name[] = "DS1820 - Resistor : ";
char sensor2Name[] = "DS1820 + Resistor : ";
char sensor3Name[] = "DS18B20 + Resistor : ";
void setup(void)
{
Serial.begin(9600);
}
void writeTimeToScratchpad(byte* address)
{
//reset the bus
ds.reset();
//select our sensor
ds.select(address);
//CONVERT T function call (44h) which puts the temperature into the scratchpad
ds.write(0x44,1);
//sleep a second for the write to take place
delay(1000);
}
void readTimeFromScratchpad(byte* address, byte* data)
{
//reset the bus
ds.reset();
//select our sensor
ds.select(address);
//read the scratchpad (BEh)
ds.write(0xBE);
for (byte i=0;i<9;i++){
data[i] = ds.read();
}
}
float getTemperature(byte* address)
{
int tr;
byte data[12];
writeTimeToScratchpad(address);
readTimeFromScratchpad(address,data);
//put in temp all the 8 bits of LSB (least significant byte)
tr = data[0];
//check for negative temperature
if (data[1] > 0x80)
{
tr = !tr + 1; //two's complement adjustment
tr = tr * -1; //flip value negative.
}
//COUNT PER Celsius degree (10h)
int cpc = data[7];
//COUNT REMAIN (0Ch)
int cr = data[6];
//drop bit 0
tr = tr >> 1;
return tr - (float)0.25 + (cpc - cr)/(float)cpc;
}
void loop(void)
{
float temp1 = getTemperature(sensor1);
float temp2 = getTemperature(sensor2);
float temp3 = getTemperature(sensor3);
Serial.print(sensor1Name);
Serial.print(temp1);
Serial.println(" Celsius");
Serial.print(sensor2Name);
Serial.print(temp2);
Serial.println(" Celsius");
Serial.print(sensor3Name);
Serial.print(temp3);
Serial.println(" Celsius");
Serial.println();
delay(1000);
}
As you can see at the brown marked parts the values of the new DS18B20 (begins with 28 instead of 10) i had to remode the leading 0(zero) to fit the belongings of the compiler - if not, it causes an error even when i test the code.
WITH these manipulations 8) the sketch runs, but gives wrong result (the other two shows about correct 21.50 degree celsius) with 30.5 degress celsius.
When i touch it short, it jumps up to 62 degree celsius.
So, i know something about the MSB and LSB but not enough to get them work together.
Did anyone know what to do to get it work ?