# Read temperature via I2C from DS1621
# Customize the code according your device name
# Copyleft 2013 Chaosdorf
#include <stdio.h>
#include <stdint.h>
#include <fcntl.h>
#include <stdlib.h>
#include <linux/i2c-dev.h>
int main(void)
{
int file;
char filename[20];
int addr = 0x48; /* The I2C address */
int32_t temperature;
int32_t temp_int;
int32_t temp_counter;
int32_t temp_slope;
// Open a file giving access to i2c interface 1
sprintf(filename,"/dev/i2c-1");
if ((file = open(filename,O_RDWR)) < 0) {
printf("No access\n");
exit(1);
}
// Check that the chip is there
if (ioctl(file,I2C_SLAVE,addr) < 0) {
printf("No chip detected\n");
exit(1);
}
// Set up continuous measurement
if (i2c_smbus_write_byte_data(file, 0xac, 0x00) < 0) {
printf("Set continuos mesurement failed\n");
exit(1);
}
// Start measurements
if (i2c_smbus_write_byte(file, 0xee) < 0) {
printf("Start measurements failed\n");
exit(1);
}
// read TEMP_INT
if ((temp_int = i2c_smbus_read_byte_data(file, 0xaa)) < 0) {
printf("Read temp_int failed\n");
exit(1);
}
// read TEMP_COUNTER
if ((temp_counter = i2c_smbus_read_byte_data(file, 0xa8)) < 0) {
printf("Read temp_counter failed\n");
exit(1);
}
// read TEMP_SLOPE
if ((temp_slope = i2c_smbus_read_byte_data(file, 0xa9)) < 0) {
printf("Read temp_scope failed\n");
exit(1);
}
// calculate temperaure
temperature = temp_int * 100 - 25 + ((temp_slope - temp_counter) * 100 / temp_slope);
printf("%d", (int32_t)temperature);
close(file);
exit(0);
}