RuiViana

PaiDeSanto_18B20.ino

Jul 5th, 2021 (edited)
1,146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.38 KB | None | 0 0
  1.  
  2. /*
  3. Este identifica o tipo de sensor
  4. 26/06/2015
  5. Funciona muito Bem
  6.  */
  7.  
  8.   #include <OneWire.h>    // Biblioteca de 1Wire
  9.   OneWire  ds(4);        // Seleciona a porta do 1WirwBus on pin 4
  10.   int contador = 0;
  11.  //----------------------------------------------------------------------  
  12.   void setup()
  13.   {
  14.     Serial.begin(9600);   // Inicialisa a serial
  15.     delay(1000);          //Aguarda 1 seg antes de acessar as informações do sensor
  16.   }
  17.  //----------------------------------------------------------------------
  18.   void loop()
  19.   {  
  20.     byte i;              // Variavel de uso geral
  21.     byte present = 0;    // Variavel para present bit do 1Wire
  22.     byte type_s;         // Variavel para selecionar o tipo de Sensor
  23.     byte data[12];       // Matriz para guardar os dados lidos do sensor
  24.     byte addr[8];        // Matriz para guardar endereço lidos do sensor
  25.     float celsius, fahrenheit;   // Variavel para guardar o valor das temparutras
  26.    
  27.     if ( !ds.search(addr))       // Search for the next device.
  28.     {
  29.       Serial.println("No more addresses.");
  30.       contador = 0;
  31.       Serial.println();
  32.       ds.reset_search();         // Begin a new search.
  33.       delay(100);
  34.       return;
  35.     }
  36.     contador++;
  37.     Serial.print(contador);
  38.     Serial.print("  ROM =");
  39.     for( i = 0; i < 8; i++)     // inicia a impresão do valor da ROM
  40.     {
  41.       Serial.write(' ');
  42.       Serial.print(addr[i], HEX);
  43.     }
  44.  
  45.     if (OneWire::crc8(addr, 7) != addr[7])     // Verifica o CRC
  46.     {
  47.       Serial.println("CRC is not valid!");
  48.       return;
  49.     }
  50.     Serial.print(" ");
  51.    
  52.     // the first ROM byte indicates which chip
  53.     switch (addr[0])             // Le o byte 0 da ROM
  54.     {
  55.       case 0x10:                // Se for 0x10
  56.         Serial.println("  Chip = DS18S20");  // or old DS1820
  57.         type_s = 1;             // Set tipo = 1
  58.         break;
  59.       case 0x28:                // Se for 0x28
  60.         Serial.println("  Chip = DS18B20");
  61.         type_s = 0;             // Set tipo = 0
  62.         break;
  63.       case 0x22:                // Se for 0x22
  64.         Serial.println("  Chip = DS1822");
  65.         type_s = 0;             // Set tipo = 0
  66.         break;
  67.       default:
  68.         Serial.println("Device is not a DS18x20 family device.");
  69.         return;
  70.     }
  71.    
  72.     ds.reset();                // Reset the 1-wire bus.
  73.     ds.select(addr);           // Select a device based on its address.
  74.     ds.write(0x44,1);          // start conversion, with parasite power on at the end
  75.    
  76.     delay(1000);               // maybe 750ms is enough, maybe not
  77.     // we might do a ds.depower() here, but the reset will take care of it.
  78.    
  79.     present = ds.reset();      // Reset the 1-wire bus. e agurada o Present Pulse
  80.     ds.select(addr);           // Select a device based on its address.    
  81.     ds.write(0xBE);            // Read Scratchpad
  82.    
  83.     Serial.print("  Data = ");
  84.     Serial.print(present,HEX);
  85.     Serial.print(" ");
  86.     for ( i = 0; i < 9; i++)   // Imprime os 8 bytes de dados
  87.     {           // we need 9 bytes
  88.       data[i] = ds.read();     // Read a byte.
  89.       Serial.print(data[i], HEX);
  90.       Serial.print(" ");
  91.     }
  92.     Serial.print(" CRC=");
  93.     Serial.print(OneWire::crc8(data, 8), HEX);  // Imprime o CRC8
  94.     Serial.println();
  95.    
  96.     // convert the data to actual temperature
  97.    
  98.     unsigned int raw = (data[1] << 8) | data[0]; // Função raw  Shift Data[1] 8 bits
  99.                                                  //   e faz or com data[0]
  100.     if (type_s)
  101.     {
  102.       raw = raw << 3; // 9 bit resolution default
  103.       if (data[7] == 0x10)
  104.       {
  105.         // count remain gives full 12 bit resolution
  106.         raw = (raw & 0xFFF0) + 12 - data[6];
  107.       }
  108.     }
  109.     else
  110.     {
  111.       byte cfg = (data[4] & 0x60);
  112.       if (cfg == 0x00) raw = raw << 3;  // 9 bit resolution, 93.75 ms
  113.       else if (cfg == 0x20) raw = raw << 2; // 10 bit res, 187.5 ms
  114.       else if (cfg == 0x40) raw = raw << 1; // 11 bit res, 375 ms
  115.       // default is 12 bit resolution, 750 ms conversion time
  116.     }
  117.     celsius = (float)raw / 16.0;
  118.     fahrenheit = celsius * 1.8 + 32.0;
  119.     Serial.print("  Temperature = ");
  120.     Serial.print(celsius);
  121.     Serial.print(" Celsius, ");
  122.     Serial.print(fahrenheit);
  123.     Serial.println(" Fahrenheit");
  124.  
  125.     //Não diminuir o valor abaixo. O ideal é a leitura a cada 2 segundos
  126.     delay(1000);
  127.   }
Add Comment
Please, Sign In to add comment