예제 (TCN75)
I2C 인터페이스인 TCN75 칩의 온도를 가져오기 위한 응용이다.
... #define I2C_ADDR_TCN75 0x4f // 하드웨어 설계에 따라 달라진다. ... void tcn75_init(void) { int fd; fd = open("/dev/i2c-0", O_RDWR); // mknod /dev/i2c-0 c 89 0 if(fd < 0) { fprintf(stderr, "can't open /dev/i2c-0\n"); exit(2); } ioctl(fd, I2C_SLAVE, I2C_ADDR_TCN75); unsigned char cmd, read_data[2]; int read_value; cmd = 0x00; // TEMP write(fd, &cmd, 1); // Pointer Byte close(fd); } float tcn75_get_temperature(void) { int fd; fd = open("/dev/i2c-0", O_RDWR); if(fd < 0) { fprintf(stderr, "can't open /dev/i2c-0\n"); exit(2); } ioctl(fd, I2C_SLAVE, I2C_ADDR_TCN75); unsigned char read_data[2]; int read_value; float temperature; read(fd, read_data, 2); read_value = (read_data[0]<<8) | read_data[1]; read_value >>= 7; if(read_value > 256) read_value = read_value - 512; temperature = read_value / 2.0; close(fd); return temperature; } int main(int argc, char** argv) { tcn75_init(); while(1) { printf("temp = %f\n", tcn75_get_temperature()); sleep(1); } return 0; }