Created
December 18, 2014 04:20
-
-
Save tuxcuiabano/2834ca84600a89eba245 to your computer and use it in GitHub Desktop.
[Arduíno] Testando One Wire 18B20
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <OneWire.h> | |
#include <Arduino.h> | |
int SensorPin = 10; | |
OneWire ds(SensorPin); | |
void setup(void) { | |
Serial.begin(9600); | |
} | |
void loop(void) { | |
float temp = getTemp( ); | |
Serial.println("temperarua"); | |
Serial.println(temp); | |
delay(100); | |
} | |
float getTemp(){ | |
byte data[12]; | |
byte addr[8]; | |
if ( !ds.search(addr)) { | |
//no more sensors on chain, reset search | |
ds.reset_search(); | |
return -1000; | |
} | |
if ( OneWire::crc8( addr, 7) != addr[7]) { | |
Serial.println("CRC is not valid!"); | |
return -2000; | |
} | |
if ( addr[0] != 0x10 && addr[0] != 0x28) { | |
Serial.print("Device is not recognized"); | |
return -3000; | |
} | |
ds.reset(); | |
ds.select(addr); | |
ds.write(0x44,1); | |
byte present = ds.reset(); | |
ds.select(addr); | |
ds.write(0xBE); | |
for (int i = 0; i < 9; i++) { | |
data[i] = ds.read(); | |
} | |
ds.reset_search(); | |
byte MSB = data[1]; | |
byte LSB = data[0]; | |
float TRead = ((MSB << 8) | LSB); | |
float Temperature = TRead / 16; | |
if (data[1] == 0xFF) | |
{ | |
Temperature = Temperature * -1; | |
} | |
return Temperature; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment