Advertisement
RuiViana

Media_Temperatura_18B20

Nov 17th, 2016
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. #include <LiquidCrystal.h>
  2. #include <OneWire.h>
  3. byte i = 0;
  4. byte data[9];
  5. byte addr[8];
  6. float temp;                             // Mudei para float
  7. float tempACC;                          // Acumulador de temperatura
  8. float tempD;                            // Parte decimal da temperatura
  9. boolean type;
  10.  
  11. OneWire  ds(2);  // pin 2
  12. LiquidCrystal lcd(11, 12, 4, 5, 3, 7);
  13. //-------------------------------
  14. void setup(void)
  15. {
  16.   lcd.begin(20, 4);
  17.   lcd.print("Temp de cada Sensor = ");
  18.   lcd.setCursor(0, 1);
  19.   lcd.print("S1,S2,S3 = ");
  20.   lcd.setCursor(0, 2);
  21.   lcd.print("CARLOSKWIEK.COM.BR");
  22.   lcd.setCursor(4, 3);
  23.   lcd.print("BLOG E MUITO +");
  24. }
  25. //-------------------------------
  26. void loop(void)
  27. {
  28.   if (!ds.search(addr))           //get the addresses of Temperature Sensors
  29.   {
  30.     ds.reset_search();            // Pare a procura de Sensor
  31.     temp = tempACC / 3;
  32.     lcd.setCursor(9, 1);
  33.     lcd.print(temp);
  34.     lcd.print(" ");
  35.     lcd.write(223);               // Simbolo graus
  36.     lcd.print("C    ");
  37.     tempACC = 0;                  // Zera o acumulado
  38.     return;
  39.   }
  40.   switch (addr[0])
  41.   {
  42.     case 0x10: type = 1; break;   //DS18S20
  43.     case 0x22: type = 0; break;   //DS1822
  44.     case 0x28: type = 0; break;   //DS18B20
  45.     default: break;
  46.   }
  47.   ds.reset();
  48.   ds.select(addr);
  49.   ds.write(0x44);
  50.   delay(750);
  51.   ds.reset();
  52.   ds.select(addr);
  53.   ds.write(0xBE);
  54.   for ( i = 0; i < 9; i++)          //Leitura
  55.   {
  56.     data[i] = ds.read();
  57.   }
  58.   if (!type)                        //DS18B20 ou DS1822
  59.   {
  60.     temp = (data[1] << 4) | (data[0] >> 4);
  61.     if ((data[1] >> 7) == 1)
  62.     {
  63.       data[1] = ~data[1];
  64.       data[0] = (~data[0]) + 1;
  65.       temp = temp * - 1;
  66.     }
  67.     tempD = tempD + ((data[0] & 0x0F) * 625);  // Calcula valor decimal
  68.     if (tempD > 625)
  69.     {
  70.       tempD = tempD / 1000;
  71.     }
  72.     else
  73.     {
  74.       tempD = tempD / 10000;
  75.     }
  76.     temp = temp + tempD;                      // Junta inteiro com decimal
  77.     tempACC = tempACC + temp;                 // Acumula temperatura
  78.   }
  79.  
  80.   else          //DS18S20
  81.   {
  82.     temp =  data[0] >> 1;
  83.     if ((data[1] >> 7) == 1)
  84.     {
  85.       data[0] = ~data[0];
  86.     }
  87.     temp =  temp * - 1;
  88.     //tempD = ((data[0] & 0x01) * 5);
  89.     //temp = temp + tempD;                      // Junta inteiro com decimal
  90.     tempACC = tempACC + temp;                 // Acumula temperatura
  91.   }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement