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