I have been trying for awhile to interface with a DS18s20. Was unsucessful with a Pic so I bought an uno. I found the onewire library and used the example and it worked fine. I wanted to try to program it myself to get experience with interfacing. I used the main code and rewrote the write, read and reset subroutines and set the code up for one sensor. To my amazement it also worked except when you hold the DS18s20 with your finger to see if the temperature display increases. It does but seems to saturate after a few degrees increase and the dispalay jumps to 127. If you take your finger off the DS18s20 it comes back down after a few minutes. It does not do this with the onewire library routine so it must be something I left off. Thought it might be timing but I checked the timing and it looks ok. Any ideas on what would cause this behavior?
#include <OneWire.h>
#include <LiquidCrystal.h>
// LCD=======================================================
//initialize the library with the numbers of the interface pins
// works but goes to 127 if you hold ds18s20 with finger
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define LCD_WIDTH 16
#define LCD_HEIGHT 2
void setup(void)
{
lcd.begin(LCD_WIDTH, LCD_HEIGHT,1);
lcd.setCursor(0,0);
lcd.print("DS1820 Test");
Serial.begin(9600);
}
int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract,i,y;
char buf[20];
void loop(void)
{
byte data[12];
reset();
write(0xCC);
write(0x44); // start conversion, with parasite power on at the end
digitalWrite(9,HIGH);
delay(1000); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.
reset();
write(0xCC);
write(0xBE); // Read Scratchpad
for ( i = 0; i < 9; i++)
{ // we need 9 bytes
data[i] = read();
// Serial.print(i);
// Serial.println(data[i]);
}
LowByte = data[0];
HighByte = data[1];
TReading = (HighByte << 8) + LowByte;
SignBit = TReading & 0x8000; // test most sig bit
if (SignBit) // negative
{
TReading = (TReading ^ 0xffff) + 1; // 2's comp
}
Tc_100 = (TReading*100/2);
Whole = Tc_100 / 100; // separate off the whole and fractional portions
Fract = Tc_100 % 100;
lcd.clear();
lcd.setCursor(0,0);
lcd.print(Whole);
lcd.print(".");
lcd.print(Fract);
}
void reset(void)
{
digitalWrite(9,LOW);
pinMode(9,OUTPUT);
delayMicroseconds(500);
pinMode(9,INPUT);
delayMicroseconds(420);
}
int write(int v)
{
for (i = 0; i <8; i++) {
y = bitRead(v, i);
if (y==1)
{
digitalWrite(9,LOW);
pinMode(9,OUTPUT);
delayMicroseconds(10);
digitalWrite(9,HIGH);
delayMicroseconds(55);
pinMode(9,INPUT);
digitalWrite(9,LOW);
}
else
{
digitalWrite(9,LOW);
pinMode(9,OUTPUT);
delayMicroseconds(65);
digitalWrite(9,HIGH);
delayMicroseconds(5);
pinMode(9,INPUT);
digitalWrite(9,LOW);
}
}
}
int read()
{
int val=0,i,val1=0;
for (i=0; i<8; i++){
pinMode(9,OUTPUT);
digitalWrite(9,LOW);
pinMode(9,INPUT);
val = digitalRead(9);
delayMicroseconds(53);
if (val == 1)
bitSet(val1,i);
}
return val1;
}