ads1115 analog-to-digital 4-channel added
[i2c-telemetry.git] / lm75.c
1   /*
2     LM75_address.c
3     --------------
4       1) set the i2c address of the LM75
5         * R/W pulse at end of address byte not included in address 1-0-0-1-A2-A1-A0
6         * example - all lines to 0V - 1001000 = decimal 72
7       2) calculate the temperature
8       3) print it
9         usage :- type (without the < >)
10                 <LM75_address><space><address as decimal>
11 */
12
13 #include <stdio.h>
14 #include <stdint.h>
15 #include <fcntl.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <linux/i2c-dev.h>
19 #include <linux/i2c.h>
20 #include <sys/ioctl.h>
21 #include "smbus.h"
22
23 int main(int argc, char** argv)
24 {
25   int address;      /* i2c bus address */
26
27  if (argc != 2)   /* report error if we are not getting just 1 input after the program name */
28  {
29   printf("Error. usage: %s i2c_chip_address\n", argv[0]);
30  }
31   address = atoi(argv[1]); /* address is the first number after the program name */
32
33
34           int f = open("/dev/i2c-0",O_RDWR);
35           char buf[2];
36           if (f>0) {
37               ioctl(f,I2C_SLAVE,address);
38               if (-1 == read(f,buf,2)) {
39                   printf("read error.\n");
40               } else { /* calculate the temperature*/
41                   int temp = buf[0]*256+buf[1];
42                   temp >>=7;
43                   if (temp>512)
44                       temp &= -1;
45                   printf("Temperature (C)\t\t%g\n", (float)temp/2.0);
46               }
47           }
48           return close(f);
49       }