Bonjour,
J' aurai besoin d' aide pour mon projet thermomètre digital avec une sonde de température DS18B20.
Je voudrai simplement afficher la température avec un seul chiffre après la virgule, j' ai pas trouve lol
Merci d'avance pour votre aide.
Voici le code :
//-----------------------------temperature--------------------
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius;
if ( !ds.search(addr))
//search for next object on Onewire bus. For the first found, continue loop, if none found, restart from 0
{
ds.reset_search();
delay(250);
return;
}
if (OneWire::crc8(addr, 7) != addr[7]) //Check if there is no errors on transmission
{
Serial.println("CRC invalide...");
return;
}
if(addr[0] == 0x10) //Check if it's a DS18b20 on the bus or not
{
type_s = 1;
} else type_s=0;
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
//delay(1000);
present = ds.reset();
ds.select(addr);
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];
if (type_s)
{
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10)
{
// count remain gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 - data[6];
}
}
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
}
celsius = (float)raw / 16.0;
lcd.setCursor(10, 0);
lcd.print(celsius); // affichage temperature
lcd.print((char)223); // affichage caractere degres
}