e-interiores Headline Animator

5.10.07

ds18x20 con Arduino (midiendo la temperatura)

Desde hace tiempo tengo conectado un arduino a mi mini-itx
Por ahora solo monitorizo la temperatura de la cpu, disco duro, fuente de alimentación y exterior. Como sensores uso ds18x20 de Maxim (Gracias maxim por esos excelentes samples)
Arduino busca los sensores que hay en su red filtra los que son del tipo sensor de temperatura y les pide su valor. Dado que cada uno de ellos tiene un id interno único, es posible identificarlos. Posterioremente se envía el id del sensor y la temperatura leida por el puerto serie.
En el pc hay un programa c# (mono) que lee el puerto serie, traduce el id a un nombre dado y añade la fecha y hora de la lectura.
El código fuente de arduino:



#include
/* OneWire en Arduino
Descargamos el código fuente de aquí */

/* DS18S20 Temperature chip i/o */

OneWire ds(10); // on pin 10

void setup(void) {
Serial.begin(57600);
}

void loop(void) {
byte i;
byte present = 0;
byte data[12];
byte addr[8];

if ( !ds.search(addr)) {
ds.reset_search();
return;
}

Serial.print("R");
for( i = 0; i < 8; i++) {
Serial.print("-");
if(addr[i]<16)
Serial.print(0);
Serial.print(addr[i], HEX);
}

if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.print("CRC is not valid!\n");
return;
}

if ( addr[0] != 0x10) {
Serial.print("Device is not a DS18S20 family device.\n");
return;
}

ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end

delay(1000); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.

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();
}
int HighByte=data[0];
int LowByte=data[1];

// Teniendo LowByte y HighByte
int TReading = (HighByte << 8) + LowByte;
int SignBit = TReading & 0x8000; // test most sig bit
if (SignBit) // negative
{
TReading = (TReading ^ 0xffff) + 1; // 2's comp
}
int Tc_100 = (6 * TReading) + TReading / 4; // multiply by (100 * 0.0625) or 6.25

int Whole = Tc_100 / 100; // separate off the whole and fractional portions
int Fract = Tc_100 % 100;

Serial.print(":");
Serial.print(HighByte/2);
Serial.print(".");
Serial.print(LowByte/2);
Serial.print(":");
Serial.print(TReading);

Serial.println();
}


Fuentes: Arduino playground, foros de arduino y phanderson

No hay comentarios: