Advertisement
RuiViana

1_Wire_Single

Sep 28th, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. #include <OneWire.h>
  2.  
  3. OneWire ds(10); // on pin 10
  4. // ---------------------- Setup --------------------
  5. void setup(void)
  6. {
  7. Serial.begin(9600); // Usa monitor serial
  8. }
  9. // ---------------------- loop --------------------
  10. void loop(void)
  11. {
  12. byte i; // gp use
  13. byte present = 0; // Presence pulse
  14. byte type_s; // Tipo de sensor 18b20 18s20 etc
  15. byte data[12]; // Matriz para dados do sensor
  16. byte addr[8]; // Matriz para ROM do sensor
  17. float celsius; // Variavel para temperatura
  18.  
  19. if ( !ds.search(addr)) // Faça se não tiver addr
  20. {
  21. ds.reset_search(); // Reset 1-wire bus
  22. delay(250); // delay de 250ms
  23. return; // retorna
  24. }
  25.  
  26. if (OneWire::crc8(addr, 7) != addr[7]) // Se o endereço crc8 for diferente do byte 7
  27. {
  28. Serial.println("CRC is not valid!"); // Falha de CRC
  29. return; // Retorna
  30. }
  31. // the first ROM byte indicates which chip
  32. switch (addr[0]) // 10 18s20
  33. {
  34. case 0x10:
  35. type_s = 1; // tipo 1
  36. break;
  37. case 0x28: // 18s20
  38. type_s = 0; // tipo 0
  39. break;
  40. // case 0x22: // 1822
  41. // type_s = 0; // tipo 0
  42. // break;
  43. default:
  44. Serial.println("Device is not a DS18x20 family device."); // Se for diferente dests tipo informa
  45. return;
  46. }
  47.  
  48. ds.reset(); // Reset 1-wire bus
  49. ds.select(addr); // Seleciona o device com o endereço
  50. ds.write(0x44,1); // start conversion, with parasite power on at the end
  51.  
  52. delay(1000); // maybe 750ms is enough, maybe not
  53.  
  54. present = ds.reset(); // Qdo houver presence pulse
  55. ds.select(addr); // Seleciona o device com o endereço
  56. ds.write(0xBE); // Read Scratchpad
  57.  
  58. for ( i = 0; i < 9; i++) { // we need 9 bytes
  59. data[i] = ds.read();
  60.  
  61. }
  62. // convert the data to actual temperature
  63.  
  64. unsigned int raw = (data[1] << 8) | data[0]; // raw data1 or com data0 2 bytes
  65.  
  66. if (type_s) // Faça se for tipo1
  67. {
  68. raw = raw << 3; // 9 bit resolution default
  69.  
  70. if (data[7] == 0x10) // faça se for 18s20
  71. {
  72. // count remain gives full 12 bit resolution
  73. raw = (raw & 0xFFF0) + 12 - data[6];
  74. Serial.println(raw);
  75. }
  76. }
  77. else
  78. {
  79. byte cfg = (data[4] & 0x60);
  80. if (cfg == 0x00) raw = raw << 3; // 9 bit resolution, 93.75 ms
  81. else if (cfg == 0x20) raw = raw << 2; // 10 bit res, 187.5 ms
  82. else if (cfg == 0x40) raw = raw << 1; // 11 bit res, 375 ms
  83. // default is 12 bit resolution, 750 ms conversion time
  84. }
  85. float tempC = (float)raw / 16.0;
  86.  
  87. Serial.print(tempC);
  88. Serial.print(" C: ");
  89.  
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement