#include <stdio.h> //Introduce the standard input and output library for input and output data #include <windows.h> //Introduce Windows API header files for operating Windows systems #include <winioctl.h> //Introduce Windows I/O control function header file #define I2C_ADDRESS 0x48 //Define the I2C address as 0x48, which is the address of the LM75 sensor #define I2C_REG_READ 0x00 //Define the I2C read operation command as 0x00 #define I2C_REG_WRITE 0x01 //Define the I2C write operation command as 0x01 int main() // define main function { HANDLE hI2C; // Define a handle variable hI2C for operating the I2C bus char data[2]; //Define a character array data to store the read temperature data int temperature; // Define an integer variable temperature to store the calculated temperature value //Open the I2C bus hI2C = CreateFile("I2C0", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); // Open the I2C bus, the parameters are bus name, access mode, sharing mode, etc. if (hI2C == INVALID_HANDLE_VALUE) // If the opening fails { printf("Error: Unable to open I2C bus\ "); // Output error message return 1; //End program } //Write I2C address and operation command if (!WriteFile(hI2C, & amp;I2C_ADDRESS, sizeof(I2C_ADDRESS), NULL, NULL)) // Write address to I2C bus { printf("Error: Failed to write I2C address\ "); // Output error message CloseHandle(hI2C); // Close the I2C bus return 1; //End program } if (!WriteFile(hI2C, & amp;I2C_REG_READ, sizeof(I2C_REG_READ), NULL, NULL)) // Write operation command to I2C bus { printf("Error: Failed to write I2C operation command\ "); // Output error message CloseHandle(hI2C); // Close the I2C bus return 1; //End program } //Read temperature data if (!ReadFile(hI2C, data, sizeof(data), NULL, NULL)) // Read data from the I2C bus { printf("Error: Failed to read temperature data\ "); // Output error message CloseHandle(hI2C); // Close the I2C bus return 1; //End program } // Calculate the temperature (assuming the LM75 data format is 2'b10, that is, a 10-bit binary number, the highest bit is the sign bit) temperature = ((data[0] & amp; 0x7F) << 8) | data[1]; // Calculate the temperature value if (data[0] & amp; 0x80) // If the highest bit is 1, it means negative temperature { temperature = -temperature; // Negate to get the actual temperature value } printf("Temperature: %d°C\ ", temperature); // Output the calculated temperature value // Close the I2C bus CloseHandle(hI2C); // Close the I2C bus return 0; //The program runs successfully }
#include <stdio.h> #include <windows.h> int main() { HANDLE hI2C; //I2C bus handle BYTE I2C_ADDRESS = 0x48; // I2C device address BYTE I2C_REG_READ = 0x00; //Read the temperature register address BYTE data[2]; //Storage the read temperature data int temperature; //Storage the calculated temperature value //Open the I2C bus hI2C = CreateFile("\.\I2C0", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if (hI2C == INVALID_HANDLE_VALUE) { printf("Error: Unable to open I2C bus\ "); return 1; } //Write I2C address and operation command if (!WriteFile(hI2C, & amp;I2C_ADDRESS, sizeof(I2C_ADDRESS), NULL, NULL)) { printf("Error: Failed to write I2C address\ "); CloseHandle(hI2C); return 1; } if (!WriteFile(hI2C, & amp;I2C_REG_READ, sizeof(I2C_REG_READ), NULL, NULL)) { printf("Error: Failed to write I2C operation command\ "); CloseHandle(hI2C); return 1; } //Read temperature data if (!ReadFile(hI2C, data, sizeof(data), NULL, NULL)) { printf("Error: Failed to read temperature data\ "); CloseHandle(hI2C); return 1; } // Calculate temperature temperature = ((data[0] << 8) | data[1]) >> 5; if (temperature & amp; 0x0400) // If the highest bit is 1, it means negative temperature { temperature |= 0xF800; // Extended sign bit } printf("Temperature: %d°C\ ", temperature); // Close the I2C bus CloseHandle(hI2C); return 0; }
#include <stdio.h> //Introduce the standard input and output library #include <windows.h> //Introducing the Windows system library, which contains some functions and data types of the operating system // Function declaration, used to read temperature sensor data int readTemperature(HANDLE hI2C, BYTE deviceAddress, BYTE registerAddress); int main() // program entry function { HANDLE hI2C; // Define I2C bus handle BYTE I2C_ADDRESS = 0x48; // Define the I2C device address of the temperature sensor BYTE I2C_REG_READ = 0x00; // Define the register address for reading temperature int temperature; // Define the variable that stores the calculated temperature value //Open the I2C bus and return an I2C bus handle hI2C = CreateFile("\.\I2C0", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if (hI2C == INVALID_HANDLE_VALUE) // If the I2C bus handle is invalid { printf("Unable to open I2C bus\ "); // Output error message return 1; // return 1 } //Call the readTemperature function to read the temperature sensor data temperature = readTemperature(hI2C, I2C_ADDRESS, I2C_REG_READ); if (temperature == -1) //If reading the temperature fails { printf("Unable to read temperature\ "); // Output error message CloseHandle(hI2C); // Close the I2C bus handle return 1; // return 1 } // Output the calculated temperature value printf("Temperature: %d°C\ ", temperature); //Close the I2C bus handle CloseHandle(hI2C); return 0; // The program ends normally and returns 0 } //Define the readTemperature function for reading temperature sensor data int readTemperature(HANDLE hI2C, BYTE deviceAddress, BYTE registerAddress) { BYTE data[2]; //Define an array of length 2 to store the read temperature data int temperature; // Define the variable that stores the calculated temperature value //Write the device address to the I2C bus, return -1 if the writing fails if (!WriteFile(hI2C, & amp;deviceAddress, sizeof(deviceAddress), NULL, NULL)) { return -1; } //Write the register address to the I2C bus, if the writing fails, return -1 if (!WriteFile(hI2C, & amp;registerAddress, sizeof(registerAddress), NULL, NULL)) { return -1; } //Read temperature data from the I2C bus, return -1 if the reading fails if (!ReadFile(hI2C, data, sizeof(data), NULL, NULL)) { return -1; } // Perform bit operations on the read temperature data to calculate the temperature value temperature = ((data[0] << 8) | data[1]) >> 5; // If the highest bit is 1, it means negative temperature if (temperature & 0x0400) { temperature |= 0xF800; // Extended sign bit } return temperature; // Return the calculated temperature value }
The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. C Skill Tree Home Page Overview 194006 people are learning the system