//#include <util/delay.h>
//_delay_us(480);
const int OneWirePin = 2; // the number of the input/output One-Wire pin
byte OW_Reset(void)
{
byte presence;
pinMode(OneWirePin,OUTPUT);
digitalWrite(OneWirePin,LOW);
delayMicroseconds(480);
digitalWrite(OneWirePin,HIGH);
pinMode(OneWirePin,INPUT);
presence=digitalRead(OneWirePin);
delayMicroseconds(480);//413
return(presence);
}
void OW_WriteBit(char bitval)
{
pinMode(OneWirePin,OUTPUT);
digitalWrite(OneWirePin,LOW);
if(bitval==1)
digitalWrite(OneWirePin,HIGH);
delayMicroseconds(100);//100
digitalWrite(OneWirePin,HIGH);
}
void OW_WriteByte(char val)
{
byte i;
byte temp;
for(i=0; i<8 ; i++)
{
temp=val>>i;
temp&=0x01;
OW_WriteBit(temp);
}
delayMicroseconds(100);
}
byte OW_ReadBit(void)
{
pinMode(OneWirePin,OUTPUT);
digitalWrite(OneWirePin,LOW);
digitalWrite(OneWirePin,HIGH);
delayMicroseconds(10);//15,work=10
pinMode(OneWirePin,INPUT);
return(digitalRead(OneWirePin));
}
byte OW_ReadByte(void)
{
byte i;
byte value=0;
for(i=0;i<8;i++)
{
if(OW_ReadBit())
value|=0x01<<i;
delayMicroseconds(100); //150
}
return(value);
}
void setup() //Setup Function
{
Serial.begin(9600);
}
void loop(void)
{
byte get[10],k,res[5]={"0"};
int temp,temp_lsb,temp_msb;
byte type_s;
if(OW_Reset())
{
OW_WriteByte(0xCC);
OW_WriteByte(0x44);
delay(1000); //120 ,750ms 12 bit
OW_Reset();
OW_WriteByte(0xCC);
OW_WriteByte(0xBE);
for(k=0;k<9;k++)
{
get[k]=OW_ReadByte();
}
Serial.print(get[0],HEX);
Serial.print(" ");
Serial.print(get[1],HEX);
Serial.print(" ");
Serial.print(get[2],HEX);
Serial.print(" ");
Serial.print(get[3],HEX);
Serial.print(" ");
Serial.print(get[4],HEX);
Serial.print(" ");
Serial.print(get[5],HEX);
Serial.print(" ");
Serial.print(get[6],HEX);
Serial.print(" ");
Serial.print(get[7],HEX);
Serial.print(" ");
Serial.print(get[8],HEX);
Serial.print(" ");
//Serial.print("temp=");
//Serial.println((float) get[0]/2);*/
Serial.print("temparature=");
Serial.println( ((float)get[0]/2) - 0.0625 + ( ((float)get[7]-(float)get[6]) / (float)get[7] ) );
}
//delay(750);
}