Advertisement
RuiViana

Media_Temperatura_So__18B20

Nov 17th, 2016
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 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.   ds.reset();
  41.   ds.select(addr);
  42.   ds.write(0x44);
  43.   delay(750);
  44.   ds.reset();
  45.   ds.select(addr);
  46.   ds.write(0xBE);
  47.   for ( i = 0; i < 9; i++)          //Leitura
  48.   {
  49.     data[i] = ds.read();
  50.   }
  51.   temp = (data[1] << 4) | (data[0] >> 4);
  52.   if ((data[1] >> 7) == 1)
  53.   {
  54.     data[1] = ~data[1];
  55.     data[0] = (~data[0]) + 1;
  56.     temp = temp * - 1;
  57.   }
  58.   tempD = tempD + ((data[0] & 0x0F) * 625);  // Calcula valor decimal
  59.   if (tempD > 625)
  60.   {
  61.     tempD = tempD / 1000;
  62.   }
  63.   else
  64.   {
  65.     tempD = tempD / 10000;
  66.   }
  67.   temp = temp + tempD;                      // Junta inteiro com decimal
  68.   tempACC = tempACC + temp;                 // Acumula temperatura
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement