Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <OneWire.h>
- OneWire ds(10); // on pin 10
- // ---------------------- Setup --------------------
- void setup(void)
- {
- Serial.begin(9600); // Usa monitor serial
- }
- // ---------------------- loop --------------------
- void loop(void)
- {
- byte i; // gp use
- byte present = 0; // Presence pulse
- byte type_s; // Tipo de sensor 18b20 18s20 etc
- byte data[12]; // Matriz para dados do sensor
- byte addr[8]; // Matriz para ROM do sensor
- float celsius; // Variavel para temperatura
- if ( !ds.search(addr)) // Faça se não tiver addr
- {
- ds.reset_search(); // Reset 1-wire bus
- delay(250); // delay de 250ms
- return; // retorna
- }
- if (OneWire::crc8(addr, 7) != addr[7]) // Se o endereço crc8 for diferente do byte 7
- {
- Serial.println("CRC is not valid!"); // Falha de CRC
- return; // Retorna
- }
- // the first ROM byte indicates which chip
- switch (addr[0]) // 10 18s20
- {
- case 0x10:
- type_s = 1; // tipo 1
- break;
- case 0x28: // 18s20
- type_s = 0; // tipo 0
- break;
- // case 0x22: // 1822
- // type_s = 0; // tipo 0
- // break;
- default:
- Serial.println("Device is not a DS18x20 family device."); // Se for diferente dests tipo informa
- return;
- }
- ds.reset(); // Reset 1-wire bus
- ds.select(addr); // Seleciona o device com o endereço
- ds.write(0x44,1); // start conversion, with parasite power on at the end
- delay(1000); // maybe 750ms is enough, maybe not
- present = ds.reset(); // Qdo houver presence pulse
- ds.select(addr); // Seleciona o device com o endereço
- ds.write(0xBE); // Read Scratchpad
- for ( i = 0; i < 9; i++) { // we need 9 bytes
- data[i] = ds.read();
- }
- // convert the data to actual temperature
- unsigned int raw = (data[1] << 8) | data[0]; // raw data1 or com data0 2 bytes
- if (type_s) // Faça se for tipo1
- {
- raw = raw << 3; // 9 bit resolution default
- if (data[7] == 0x10) // faça se for 18s20
- {
- // count remain gives full 12 bit resolution
- raw = (raw & 0xFFF0) + 12 - data[6];
- Serial.println(raw);
- }
- }
- else
- {
- byte cfg = (data[4] & 0x60);
- if (cfg == 0x00) raw = raw << 3; // 9 bit resolution, 93.75 ms
- else if (cfg == 0x20) raw = raw << 2; // 10 bit res, 187.5 ms
- else if (cfg == 0x40) raw = raw << 1; // 11 bit res, 375 ms
- // default is 12 bit resolution, 750 ms conversion time
- }
- float tempC = (float)raw / 16.0;
- Serial.print(tempC);
- Serial.print(" C: ");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement