This program is specifically for the DS18S20P. But it is probably easier to get a non P(arasite) version working. To avoid misunderstandings let's start with the DS18S20P connections.
GND and Vdd to GND, Dq to pin 6 AND to pin 7 AND via a 4k7 resistor to +5V on arduino board.
Thanks to yerg2k to get me started and jims to get me thinking.
Comments are welcome.
/* DS18S20P Temperature chip i/o
* ---------------
*
* See http://pdfserv.maxim-ic.com/en/ds/DS1820-DS1820S.pdf for the datasheet.
*
* (copyleft) 2006 by Derek Yerger - Free to distribute freely.
* (sorry Derek, great inspiration but I rewrote it completely)
* heavily modified by bigengineer
* inspired by: http://microsyl.com/
* and Dallas 1-wire datasheets
*
*/
#define DQ 7 //data pin
#define PU 6 //separate pin for pull up
#define pin13 13
#define VERSION 01
void setup(void)
{
// initialize inputs/outputs
// start serial port
pinMode(DQ,INPUT);
pinMode(PU,INPUT);
pinHigh();
Serial.begin(9600);
}
byte reset()
{
pinLow();
delayMicroseconds(500);
pinIn();
delayMicroseconds(70);
if (digitalRead(DQ) == LOW)
{
delayMicroseconds(500);
return(1);
}
return(0);
}
void WriteByte(byte data)
{
byte i;
for(i=0;i<=7;i++)
{
pinLow();
if (data & 0x01) //write 1
{
delayMicroseconds(7);
pinIn();
delayMicroseconds(70);
}
else //write 0
{
delayMicroseconds(70);
pinIn();
delayMicroseconds(7);
}
data>>=1;
}
}
void convertTemp()
{
byte i;
byte data = 0x44;
for(i=0;i<=7;i++)
{
pinLow();
if (data & 0x01) //write 1
{
delayMicroseconds(7);
pinIn();
delayMicroseconds(70);
}
else //write 0
{
delayMicroseconds(70);
pinIn();
delayMicroseconds(7);
}
data>>=1;
}
pullupHigh(); //pull pin 6 high for the conversion
delay(750); //conversion time is 750 milliseconds
pullupIn(); //switch off pin 6
pinIn();
}
byte readByte()
{
/* timing is critical here. But timing values are different
* from the datasheets. These values are found by trial & error.
* The delay's should be somewhere around 15 uS.
*/
byte data=0;
byte i;
byte bit;
for(i=0;i<8;i++)
{
pinLow();
delayMicroseconds(1); // > 2 doesn't work
pinIn();
delayMicroseconds(1); // >5 doesn't work
bit = digitalRead(DQ) & 0x01;
data >>= 1;
if (bit) data |= 0x80;
delayMicroseconds(50); //doesn't seem to be necessary
}
return(data);
}
void pinHigh()
{
pinMode(DQ,OUTPUT);
digitalWrite(DQ,HIGH);
}
void pinLow()
{
pinMode(DQ,OUTPUT);
digitalWrite(DQ,LOW);
}
void pinIn()
{
pinMode(DQ,INPUT);
//pullupIn(); //om de resistor pull-up te laten werken
}
void pullupHigh()
{
pinMode(PU,OUTPUT);
digitalWrite(PU,HIGH);
digitalWrite(pin13, HIGH);
}
void pullupLow()
{
pinMode(PU,OUTPUT);
digitalWrite(PU,LOW);
digitalWrite(pin13, LOW);
}
void pullupIn()
{
pinMode(PU,INPUT); //necessary for resistor pull-up
digitalWrite(pin13, LOW);
}
void readRom()
{
byte j;
byte pad[9];
reset();
WriteByte(0x33);
for(j=0;j<8;j++)
{
pad[j] = readByte();
}
for(j=0;j<8;j++)
{
Serial.print(pad[j], HEX);
Serial.print(" ");
}
Serial.println("read rom");
}
void readScratchpad()
{
byte j;
byte pad[9];
int msb,lsb;
WriteByte(0xBE);
for(j=0;j<9;j++)
{
pad[j] = readByte();
}
for(j=0;j<9;j++)
{
Serial.print(pad[j], HEX);
Serial.print(" ");
}
Serial.print("read scratchpad ");
msb = pad[1];
lsb = pad[0];
if (msb <= 0x80)lsb = lsb/2;
msb = msb & 0x80;
if (msb >=0x80) lsb = (~lsb)+1;
if (msb >=0x80) lsb = lsb/2;
if (msb >=0x80) lsb = ((-1)*lsb);
Serial.print("T = ");
Serial.print(lsb);
Serial.print(" ");
}
void loop(void)
{
readRom();
reset();
WriteByte(0xCC);
convertTemp();
reset();
WriteByte(0xCC);
readScratchpad();
Serial.print("version: ");
Serial.println(VERSION);
delay(1000); // Lets not flood.
}
[edit]changed typo that cosinekitty discovered[/edit]