Code:
const int OneWirePin = 2; // the number of the input/output One-Wire pin
void Onewire_Write_bit(bool _bit_)
{
if(_bit_)
{
digitalWrite(OneWirePin,LOW);
delayMicroseconds(15);
digitalWrite(OneWirePin,HIGH);
delayMicroseconds(45);
}else{
digitalWrite(OneWirePin,LOW);
delayMicroseconds(60);
}
}
void Onewire_Write_byte(unsigned char output_data)
{
int iBit=0;
for(int iloop = 0 ; iloop < 8 ; iloop++)
{
iBit = (output_data & 0x01);
Onewire_Write_bit(iBit);
Onewire_start_of_slot();
output_data = output_data >> 1 ;
}
}
void Onewire_start_of_slot(void)
{
digitalWrite(OneWirePin,HIGH);
delayMicroseconds(1);
}
void Onewire_Reset(void)
{
//pinMode(OneWirePin,OUTPUT);
digitalWrite(OneWirePin,HIGH);
delay(1); //delay = 1 mS
digitalWrite(OneWirePin,LOW); //master reset t=480uS
delayMicroseconds(480);
digitalWrite(OneWirePin,HIGH); //start of slot t=1uS บัสวาง (ระหวาง บิต)
delayMicroseconds(1);
for(byte i=0 ; i<4 ; i++)
{
pinMode(OneWirePin,INPUT);
if(digitalRead(OneWirePin)==0)
{
//digitalWrite(3,LOW);
}
delayMicroseconds(60);
}
delayMicroseconds(240);
//pinMode(OneWirePin,OUTPUT);
//digitalWrite(OneWirePin,HIGH);
//digitalWrite(3,HIGH);
}
void setup() //Setup Function
{
pinMode(OneWirePin,OUTPUT);
pinMode(3,OUTPUT);
digitalWrite(OneWirePin,HIGH);
digitalWrite(3,HIGH);
}
void loop(void)
{
Onewire_Reset();
Onewire_Write_byte(0xCC);
Onewire_Write_byte(0x44);
Onewire_Reset();
Onewire_Write_byte(0xCC);
Onewire_Write_byte(0xBE);
//Onewire_Reset();
//pinMode(OneWirePin,OUTPUT);
//digitalWrite(OneWirePin,HIGH);
/*byte data[9]; //9 Byte Array Savd Temperature Code
int temp;
if(ds.reset()) //If Device ACK on Reset
{
ds.write(0xCC); //Skip ROM Command
ds.write(0x44); //Start Conversion Temp
ds.reset(); //Re-Start New Command
ds.write(0xCC); //Skip ROM Command
ds.write(0xBE); //Read Scratchpad RAM(Temperature Data)
for ( int i = 0; i<9; i++) //9 Byte Read Scratchpad RAM
{
data[i] = ds.read();
}
if (ds.crc8(data,8) != data[8]) //Verify 9 Byte CRC(0..8) By CRC-8
{
Serial.println("Read Device CRC.....Error");
}
else
{
Serial.print("Scratchpad = ");
for ( int i=0; i<9; i++) //9 Byte Print
{
if (data[i] < 0x10)
{
Serial.print("0"); //Insert 1st Zero
}
Serial.print(data[i], HEX); //2nd Digit HEX
Serial.print(" "); //Seperate Byte By Space
}
Serial.print(" --> Temperature = ");
temp=(data[1]<<8)+data[0]; //data[1]MSB+data[0]LSB
if(data[1]!=0x00) //Verify Signed of Temperature : + or -
{
Serial.print("-");
temp = (~temp)+1; //2'Complement
}
else
{
Serial.print("+");
}
Serial.print(DoubleToAscii((double)temp*0.5)); //0.5 Degree / Step
Serial.println(" C");
}
}
else
{
Serial.println("Search Device...Not Found");*/
//}
digitalWrite(3,HIGH);
delay(1000); //Wait 1 Second RepeatR
digitalWrite(3,LOW);
delay(1000); //Wait 1 Second RepeatR
}