Download the file
  1. #include "temperature.h"
  2. #include <OneWire.h>
  3.  
  4. Temperature::Temperature():
  5. ds(6), initialized(false)
  6. {
  7. uint8_t retries = 5;
  8. while (retries && !ds.search(addr)) {
  9. ds.reset_search();
  10. delay(250);
  11. retries--;
  12. }
  13. if (retries &&
  14. OneWire::crc8( addr, 7) == addr[7] &&
  15. addr[0] == 0x10) {
  16. initialized = true;
  17. }
  18. }
  19.  
  20. void Temperature::start_reading() {
  21. if (!initialized) return;
  22. ds.reset();
  23. ds.select(addr);
  24. ds.write(0x44,0); // 0: no parasitic power, 1: with parasitic power
  25. ready_time = millis() + 1000;
  26. }
  27.  
  28. unsigned short Temperature::get_value() {
  29. if (!initialized) return 0xFF00; // out of range of the DS18S20 => error
  30. while((int32_t) (millis() - ready_time) < 0)
  31. delay(5);
  32. ds.reset();
  33. ds.select(addr);
  34. ds.write(0xBE);
  35. //Serial.print("buf: ");
  36. for (int i = 0; i < 9; i++) {
  37. data[i] = ds.read();
  38. //Serial.print(data[i], HEX); Serial.print(" ");
  39. }
  40. //Serial.println();
  41. unsigned short val = (data[1] << 8) | data[0];
  42. return val;
  43. }
  44.  
  45. // vim:ft=arduino
  46.