// 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 LM75A.c -lwiringPi // pi@raspberrypi:~ $ ./LM75A // 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: int main(int argc, char** argv) { int fd; // Standard Linux filehandle for I2C device int data; // Data as read from wiringPiI2CReadReg16() int16_t raw16; // Word from I2C device double temperature; setbuf(stdout, NULL); // NetBeans hack: disable buffering on stdout printf("Reading 2 bytes from I2C address 0x%02X, register 0x%02hX\n", LM75A_ADDR, LM75A_TEMP); fd = wiringPiI2CSetup(LM75A_ADDR); // Initialises the I2C system // if (fd < 0) Error... data = wiringPiI2CReadReg16(fd, LM75A_TEMP); // Get 2 bytes from LM75A // if (data < 0) Error... // Simulation for T < 0°C (untested) // See Table 10.Temp register value in data sheet // printf ("SIMULATED! "); // data = 0xE0FF; // FFE0 / 1111 1111 111x / −0.125 °C // data = 0x0019; // 1900 / 0001 1001 000x / +25 °C // data = 0x0000; // 0000 / 0000 0000 000x / 0 °C // data = 0x00E7; // E700 / 1110 0111 000x / -25 °C // data = 0x20C9; // C920 / 1100 1001 001x / −54.875 °C // data = 0x20C9; // C920 / 1100 1001 000x / −54.875 °C raw16 = ((data << 8) & 0xFF00) | ((data >> 8) & 0xE0); // Adjust MSB and LSB printf("Raw data: 0x%02hhX 0x%02hhX\n", (uint8_t) ((raw16 & 0xFF00) >> 8), (uint8_t) (raw16 & 0xFF)); temperature = (double) raw16 / 256; // Convert worr fron LM75A to degree Celsius printf("Temperature: %4.3f degree Celsius\n", temperature); return (EXIT_SUCCESS); }