Initial fork + minor fixes for BMP085 (mBar output) + shell scripts for ADS1115 and...
[i2c-telemetry.git] / pcf8574_4.c
1 /*
2     PCF8574_addr_read.c
3     -------------------
4 read the data on the 8 line port of an i2c bus PCF8574
5 ------------------------------------------------------
6
7 1) set the i2c address of the PCF8574
8 2) send data 255 to turn off all 8 lines - make them inputs
9 3) read the data on the 8 port lines as a byte
10   usage :- type (without the < >)
11   <PCF8574_address_read_4> <space> <i2c_bus_address_of_PCF8574_as_decimal>
12 */
13
14 #include <stdio.h>
15 #include <fcntl.h>
16 #include <unistd.h>
17 #include <sys/ioctl.h>
18 #include <linux/i2c.h>
19
20   int i2c;
21   int buf[1];
22   int address; /* i2c bus address of the PCF8574 */
23   int byte;    /* the 8 bit byte that represents the data present on the 8 wire port */
24
25 int main(int argc, char** argv)
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
32   address = atoi(argv[1]); /* address is the first number after the program name */
33
34   i2c = open("/dev/i2c/0",O_RDWR); /* open the i2c-bus number 0 */
35
36   ioctl(i2c,I2C_SLAVE, address); /* set the address of the chip we will talk to */
37
38   buf[0] = 255; 
39   write(i2c,buf,1); /* we send 255 to make all lines go off, ready for input data */
40
41   read(i2c,buf,1); /* now buf(0) contains the byte of data on the 8 port lines*/
42
43   byte = buf[0];
44
45   printf("BYTE = %d \n", byte); 
46 /* BYTE value is 0-256)*/
47
48   i2c = close(i2c);
49 }
50