Advertisement
skycrane

Untitled

Jun 4th, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. #include <OneWire.h>
  2.  
  3. // DS18S20 Temperature chip i/o
  4. OneWire ds(10); // on pin 10
  5.  
  6. void setup(void) {
  7. // initialize inputs/outputs
  8. // start serial port
  9. Serial.begin(9600);
  10. }
  11.  
  12. void loop(void) {
  13.  
  14. int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract;
  15.  
  16. byte i;
  17. byte present = 0;
  18. byte data[12];
  19. byte addr[8];
  20.  
  21. if ( !ds.search(addr)) {
  22. Serial.print("No more addresses.\n");
  23. ds.reset_search();
  24. return;
  25. }
  26.  
  27. Serial.print("R=");
  28. for( i = 0; i < 8; i++) {
  29. Serial.print(addr[i], HEX);
  30. Serial.print(" ");
  31. }
  32.  
  33. if ( OneWire::crc8( addr, 7) != addr[7]) {
  34. Serial.print("CRC is not valid!\n");
  35. return;
  36. }
  37.  
  38. if ( addr[0] == 0x10) {
  39. Serial.print("Device is a DS18S20 family device.\n");
  40. }
  41. else if ( addr[0] == 0x28) {
  42. Serial.print("Device is a DS18B20 family device.\n");
  43. }
  44. else {
  45. Serial.print("Device family is not recognized: 0x");
  46. Serial.println(addr[0],HEX);
  47. return;
  48. }
  49.  
  50. ds.reset();
  51. ds.select(addr);
  52. ds.write(0x44,1); // start conversion, with parasite power on at the end
  53.  
  54. delay(4000); // maybe 750ms is enough, maybe not
  55. // we might do a ds.depower() here, but the reset will take care of it.
  56.  
  57. present = ds.reset();
  58. ds.select(addr);
  59. ds.write(0xBE); // Read Scratchpad
  60.  
  61. Serial.print("P=");
  62. Serial.print(present,HEX);
  63. Serial.print(" ");
  64. for ( i = 0; i < 9; i++) { // we need 9 bytes
  65. data[i] = ds.read();
  66. Serial.print(data[i], HEX);
  67. Serial.print(" ");
  68. }
  69. Serial.print(" CRC=");
  70. Serial.print( OneWire::crc8( data, 8), HEX);
  71. Serial.println();
  72.  
  73. LowByte = data[0];
  74. HighByte = data[1];
  75. TReading = (HighByte << 8) + LowByte;
  76. SignBit = TReading & 0x8000; // test most sig bit
  77. if (SignBit) // negative
  78. {
  79. TReading = (TReading ^ 0xffff) + 1; // 2's comp
  80. }
  81. Tc_100 = (6 * TReading) + TReading / 4; // multiply by (100 * 0.0625) or 6.25
  82.  
  83. Whole = Tc_100 / 100; // separate off the whole and fractional portions
  84. Fract = Tc_100 % 100;
  85.  
  86.  
  87. if (SignBit) // If its negative
  88. {
  89. Serial.print("-");
  90. }
  91. Serial.print(Whole);
  92. Serial.print(".");
  93. if (Fract < 10)
  94. {
  95. Serial.print("0");
  96. }
  97. Serial.print(Fract);
  98.  
  99. Serial.print("\n");
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement