#include <OneWire.h>
OneWire ds(10);
uint8_t dsROM[8];
uint8_t dsData[9];
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
ds.reset();
ds.write(0x33); //Read Rom code
Serial.print("ROM code: ");
for (int i = 0; i < 8; i++)
{
dsROM[i] = ds.read();
Serial.print(dsROM[i], HEX);
Serial.print(" ");
}
Serial.println();
//ds18B20 -> set 12bit res
if(dsROM[0]==0x28) {
ds.reset();
ds.write(0xCC); // skip rom
ds.write(0x4E); //write S
ds.write(0x00); // TH
ds.write(0x00); // TL
ds.write(0b01111111); //CFG 12bit res.
}
}
void loop() {
// put your main code here, to run repeatedly:
ds.reset();
ds.write(0x33); //Read Rom code
Serial.print("ROM code: ");
for (int i = 0; i < 8; i++)
{
dsROM[i] = ds.read();
Serial.print(dsROM[i], HEX);
Serial.print(" ");
}
Serial.println();
ds.reset();
ds.write(0xCC); //skip rom
ds.write(0x44); // Convert (počinje mjeriti temperaturu
delay(1000); // treba mu barem 750ms
ds.reset();
ds.write(0xCC); // skip rom
ds.write(0xBE); // read ScratchPad
Serial.print("Scratchpad: ");
for (int i = 0; i < 9; i++)
{
dsData[i] = ds.read();
Serial.print(dsData[i], HEX);
Serial.print(" ");
}
Serial.println();
// ds18s20
if(dsROM[0]==0x10) {
Serial.println("ds18S20");
int16_t intTemp=dsData[1];
intTemp<<=8;
intTemp|=dsData[0];
float floatTemp = (float)(intTemp>>1) - 0.25 + (float)(dsData[7]-dsData[6])/dsData[7];
Serial.println(floatTemp,3);
}
//ds18B20
if(dsROM[0]==0x28) {
Serial.println("ds18B20");
int16_t intTemp=dsData[1];
intTemp<<=8;
intTemp|=dsData[0];
float floatTemp = intTemp;
floatTemp/=16;
Serial.println(floatTemp,3);
}
Serial.println("---------");
}