Está en la página 1de 3

#include <stdint.

h>
#include <TFTv2.h>
#include <SPI.h>
#include <i2cmaster.h>

int direccionSensor1 = 0x50<<1; // Direccin del sensor 1


int direccionSensor2 = 0x55<<1; // Direccin del sensor 2
int direccionSensor3 = 0x5A<<1; // Direccin del sensor 2

float celcius1 = 0; // Guardara temperatura 1 en Celcius


float celcius2 = 0; // Guardara temperatura 2 en Celcius
float celcius3 = 0; // Guardara temperatura 2 en Celcius

void setup()
{
TFT_BL_ON; // Ilumina el TFT Shield
Tft.TFTinit(); // Inicia el TFT Shield*/

Serial.begin(9600); // Comunicacin serial en 9600bps.


i2c_init(); // Inicializar i2c bus.
PORTC = (1 << PORTC4) | (1 << PORTC5); // Pullups.
}

void loop()
{
celcius1 = temperaturaCelcius(direccionSensor1); //Lee la temperatura para cada
sensor y
celcius2 = temperaturaCelcius(direccionSensor2); //la guarda en la respectiva
variable
celcius3 = temperaturaCelcius(direccionSensor3); //

/*fahrenheit1 = (celcius1*1.8) + 32; // Sidesean convertir en Fahrenheit


fahrenheit2 = (celcius2*1.8) + 32;*/

Tft.drawString("Temp1",0,0,4,CYAN);
Tft.fillRectangle(0,40,200,70,BLACK);
Tft.drawFloat(celcius1,0,0,40,6,CYAN);

Tft.drawString("Temp2",0,115,4,CYAN);
Tft.fillRectangle(0,155,200,70,BLACK);
Tft.drawFloat(celcius2,0,0,155,6,CYAN);

Tft.drawString("Temp3",0,230,4,CYAN);
Tft.fillRectangle(0,270,200,70,BLACK);
Tft.drawFloat(celcius3,0,0,270,6,CYAN);

Serial.print(celcius1);
Serial.print(",");
Serial.print(celcius1);
Serial.print(",");
Serial.print(celcius2);
Serial.print(",");
Serial.print(celcius3);
Serial.println();

delay(5000); // Espera tres segundo para tomar otro par


de lecturas
}
//Funcin con comentarios originales de http://wiki.wiring.co
float temperaturaCelcius(int address) {
int dev = address;
int data_low = 0;
int data_high = 0;
int pec = 0;

// Write
i2c_start_wait(dev+I2C_WRITE);
i2c_write(0x07);

// Read
i2c_rep_start(dev+I2C_READ);
data_low = i2c_readAck(); // Read 1 byte and then send ack.
data_high = i2c_readAck(); // Read 1 byte and then send ack.
pec = i2c_readNak();
i2c_stop();

// This converts high and low bytes together and processes temperature,
// MSB is a error bit and is ignored for temps.
double tempFactor = 0.02; // 0.02 degrees per LSB (measurement
// resolution of the MLX90614).
double tempData = 0x0000; // Zero out the data
int frac; // Data past the decimal point

// This masks off the error bit of the high byte, then moves it left
// 8 bits and adds the low byte.
tempData = (double)(((data_high & 0x007F) << 8) + data_low);
tempData = (tempData * tempFactor)-0.01;
float celcius = tempData - 273.15;

// Returns temperature un Celcius.


return celcius;
}

/* Function to convert a float (or double) type to a string array


* The value will be printed to 2 decimal places
*/
char* dec2str(float value)
{
char str[24]; // Increase if the "units" string is lengthened

int ptr = 0; // Array member pointer

if (value < -0.005) { str[ptr] = '-'; ptr++; } // Check sign and append

// else {str[ptr]='+'; ptr++;} // Optionally add + sign at beginning


// str[ptr]='$'; ptr++; // Optionally add or $ etc for monetary values

value = abs(value); // Remove the sign


value += 0.005; // Add to round number up
value -= 0.0000000005; // Fudge factor, otherwise x.0049999999 is rounded
up (value of 0 not affected)

ltoa(value, str + ptr, 10);


strcat(str, ".");

int dec = 100 * (value - (unsigned long)value);


if (dec < 10) strcat(str, "0");
ltoa(dec, &str[strlen(str)], 10);

// Could add units string passed as a parameter to function


// strcat(str," mV"); // Optionally add "units" to end of string

return str;
}

También podría gustarte