sample.pde 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include <OneWire.h>
  2. /* DS18S20 Temperature chip i/o */
  3. OneWire ds(10); // on pin 10
  4. void setup(void) {
  5. Serial.begin(9600);
  6. }
  7. void loop(void) {
  8. byte i;
  9. byte present = 0;
  10. byte data[12];
  11. byte addr[8];
  12. if ( !ds.search(addr)) {
  13. Serial.print("No more addresses.\n");
  14. ds.reset_search();
  15. delay(250);
  16. return;
  17. }
  18. Serial.print("R=");
  19. for( i = 0; i < 8; i++) {
  20. Serial.print(addr[i], HEX);
  21. Serial.print(" ");
  22. }
  23. if ( OneWire::crc8( addr, 7) != addr[7]) {
  24. Serial.print("CRC is not valid!\n");
  25. return;
  26. }
  27. if ( addr[0] != 0x10) {
  28. Serial.print("Device is not a DS18S20 family device.\n");
  29. return;
  30. }
  31. // The DallasTemperature library can do all this work for you!
  32. ds.reset();
  33. ds.select(addr);
  34. ds.write(0x44,1); // start conversion, with parasite power on at the end
  35. delay(1000); // maybe 750ms is enough, maybe not
  36. // we might do a ds.depower() here, but the reset will take care of it.
  37. present = ds.reset();
  38. ds.select(addr);
  39. ds.write(0xBE); // Read Scratchpad
  40. Serial.print("P=");
  41. Serial.print(present,HEX);
  42. Serial.print(" ");
  43. for ( i = 0; i < 9; i++) { // we need 9 bytes
  44. data[i] = ds.read();
  45. Serial.print(data[i], HEX);
  46. Serial.print(" ");
  47. }
  48. Serial.print(" CRC=");
  49. Serial.print( OneWire::crc8( data, 8), HEX);
  50. Serial.println();
  51. }