Here is some code that I use to read the temperature that does not use a library.
It is much smaller than using the 1 wire library but only works with a single sensor on the bus.
It also does not use any floating point so that saves space as well.
void OneWireReset(int Pin) // reset. Should improve to act as a presence pulse
{
digitalWrite(Pin, LOW);
pinMode(Pin, OUTPUT); // bring low for 500 us
delayMicroseconds(500);
pinMode(Pin, INPUT);
delayMicroseconds(500);
}
void OneWireOutByte(int Pin, byte d) // output byte d (least sig bit first).
{
byte n;
for(n=8; n!=0; n--)
{
if ((d & 0x01) == 1) // test least sig bit
{
digitalWrite(Pin, LOW);
pinMode(Pin, OUTPUT);
delayMicroseconds(5);
pinMode(Pin, INPUT);
delayMicroseconds(60);
}
else
{
digitalWrite(Pin, LOW);
pinMode(Pin, OUTPUT);
delayMicroseconds(60);
pinMode(Pin, INPUT);
}
d=d>>1; // now the next bit is in the least sig bit position.
}
}
byte OneWireInByte(int Pin) // read byte, least sig byte first
{
byte d, n, b;
for (n=0; n<8; n++)
{
digitalWrite(Pin, LOW);
pinMode(Pin, OUTPUT);
delayMicroseconds(5);
pinMode(Pin, INPUT);
delayMicroseconds(5);
b = digitalRead(Pin);
delayMicroseconds(50);
d = (d >> 1) | (b<<7); // shift d to right and insert b in most sig bit position
}
return(d);
}
/*
* Returns temp in C times 100.
* i.e. 12.34 degress C is returned as 1234
*/
int getCurrentTemp()
{
int HighByte, LowByte, TReading, Tc_100, sign;
OneWireReset(TEMP_PIN);
OneWireOutByte(TEMP_PIN, 0xcc); // SKIP ROM (broadcast to all devices)
OneWireOutByte(TEMP_PIN, 0x44); // CONVERT (perform temperature conversion, strong pullup for one sec)
OneWireReset(TEMP_PIN);
OneWireOutByte(TEMP_PIN, 0xcc); // SKIP ROM (broadcast to all devices)
OneWireOutByte(TEMP_PIN, 0xbe); // READ SRATCHPAD
LowByte = OneWireInByte(TEMP_PIN);
HighByte = OneWireInByte(TEMP_PIN);
TReading = (HighByte << 8) + LowByte;
#if 0
sign = TReading & 0x8000; // test most sig bit
if (sign) // negative
{
TReading = (TReading ^ 0xffff) + 1; // 2's comp
}
#endif
Tc_100 = (6 * TReading) + TReading / 4; // multiply by (100 * 0.0625) or 6.25
return(Tc_100);
}
It uses integers so you have to do a small calculation to get things ready to print.
So here is how you use it.
Tc_100 = getCurrentTemp();
if(Tc_100 < 0)
{
Tc_100 = -Tc_100;
sign = '-';
}
else
{
sign = '+';
}
if(units == 'F')
Tc_100 = ((Tc_100 * 9 ) / 5) + 3200;
T_whole = Tc_100 / 100; // separate off the whole and fractional portions
T_fract = Tc_100 % 100;
Tc_100, T_whole, and T_fract are all declared as int
units and sign are declared as char
--- bill