Guest User

Untitled

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