// Rapsberry Pi: I2C con wiringPi - Versione 1.3 - Marzo 2018 // Copyright (c) 2013-2018, Vincenzo Villa // Creative Commons | Attribuzione - Condividi allo stesso modo 4.0 Internazionale (CC BY-SA 4.0) // Creative Commons | Attribution-Share Alike 4.0 Unported // https://www.vincenzov.net/tutorial/RaspberryPi/i2c_wpi.htm // pi@raspberrypi:~ $ gcc -Wall -o LM75A_thermostat LM75A_thermostat.c -lwiringPi // pi@raspberrypi:~ $ ./LM75A_thermostat // Write to LM75A TOS and THYST registers "thermostat mode" // See LM75A data sheet for details #include #include #include #include #define LM75A_ADDR 0x4F // LM75A I2C address #define LM75A_CONF 1 // Configuration register #define LM75A_TEMP 0 // Temperature register #define LM75A_TOS 3 // Overtemperature shutdown threshold registe #define LM75A_THYST 2 // Hysteresis register #define HYSTERESIS 22.5 // Required hyseresis °C // example: if T < 22.5, LED switch OFF #define THRESOLD 25 // Required temperature shutdown threshold °C // example: if T > 25, LED switch ON int main(int argc, char** argv) { int fd; // Standard Linux filehandle for I2C device int data; // Data as read from wiringPiI2CReadReg16() or as required from wiringPiI2CWriteReg16()) int16_t raw16; // Word from I2C device double temperature, tos, thyst; setbuf(stdout, NULL); // NetBeans hack: disable buffering on stdout fd = wiringPiI2CSetup(LM75A_ADDR); // Initialises the I2C system // if (fd < 0) Error... data = wiringPiI2CReadReg16(fd, LM75A_TEMP); // Get 2 bytes from LM75A: actual temperature // if (data < 0) Error... raw16 = ((data << 8) & 0xFF00) | ((data >> 8) & 0xE0); // Adjust MSB and LSB temperature = (double) raw16 / 256; // Convert word from LM75A to degree Celsius printf("Temperature: %4.3f degree Celsius\n", temperature); // Write and read-back Overtemperature shutdown threshold raw16 = THRESOLD * 256; data = ((raw16 << 8) & 0x8000) | ((raw16 >> 8) & 0xFF); // Adjust MSB and LSB data = wiringPiI2CWriteReg16(fd, LM75A_TOS, data); // Write to LM75A address // if (data < 0) Error... data = wiringPiI2CReadReg16(fd, LM75A_TOS); // Get 2 bytes from LM75A (Overtemperature shutdown threshold)) raw16 = ((data << 8) & 0xFF00) | ((data >> 8) & 0x80); // Adjust MSB and LSB tos = (double) raw16 / 256; // Convert word from LM75A to degree Celsius // Write and read-back Hysteresis raw16 = HYSTERESIS * 256; data = ((raw16 << 8) & 0x8000) | ((raw16 >> 8) & 0xFF); // Adjust MSB and LSB data = wiringPiI2CWriteReg16(fd, LM75A_THYST, data); // Write to LM75A address (Hysteresis) // if (data < 0) Error... data = wiringPiI2CReadReg16(fd, LM75A_THYST); // Get 2 bytes from LM75A (Hysteresis) // if (data < 0) Error... raw16 = ((data << 8) & 0xFF00) | ((data >> 8) & 0x80); // Adjust MSB and LSB thyst = (double) raw16 / 256; // Convert word from LM75A to degree Celsius printf("The LED becomes active when temperature exceeds %3.1f deg C, and leaves the active state when the temperature drops below %3.1f deg C\n\n", tos, thyst); return (EXIT_SUCCESS); }