Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. float getTemp() {
  2. //returns the temperature from one DS18S20 in DEG Celsius
  3.  
  4. byte data[12];
  5. byte addr[8];
  6.  
  7. if ( !ds.search(addr)) {
  8. //no more sensors on chain, reset search
  9. ds.reset_search();
  10. return -1000;
  11. }
  12.  
  13. if ( OneWire::crc8( addr, 7) != addr[7]) {
  14. // Serial.println("CRC is not valid!");
  15. return -1000;
  16. }
  17.  
  18. if ( addr[0] != 0x10 && addr[0] != 0x28) {
  19. // Serial.print("Device is not recognized");
  20. return -1000;
  21. }
  22.  
  23. ds.reset();
  24. ds.select(addr);
  25. ds.write(0x44, 1); // start conversion, with parasite power on at the end
  26.  
  27. byte present = ds.reset();
  28. ds.select(addr);
  29. ds.write(0xBE); // Read Scratchpad
  30.  
  31.  
  32. for (int i = 0; i < 9; i++) { // we need 9 bytes
  33. data[i] = ds.read();
  34. }
  35.  
  36. ds.reset_search();
  37.  
  38. byte MSB = data[1];
  39. byte LSB = data[0];
  40.  
  41. float tempRead = ((MSB << 8) | LSB); //using two's compliment
  42. float TemperatureSum = tempRead / 16;
  43.  
  44. return TemperatureSum;
  45.  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement