Initial fork + minor fixes for BMP085 (mBar output) + shell scripts for ADS1115 and...
[i2c-telemetry.git] / pcf8574_2.c
1 /* 
2 PCF8574_addr_read_slug.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 Home Page - http://www.sunspot.co.uk/Projects/SWEEX/slug/i2c/slug_i2c.html
14
15 */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <sys/ioctl.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <linux/i2c-dev.h>
23 #define I2C_SLAVE0x0703/* Change slave address*/
24
25 int i2c;
26 char filename[20];
27 unsigned long address;
28 int rc;
29 unsigned char data[1];
30 int byte; /* the 8 bit byte that represents the data present on the 8 wire port */
31
32 int main(int argc, char ** argv) 
33 {
34 if (argc != 2) { /* error if we are not getting just 1 input after the program name */
35 fprintf(stderr, "usage: %s <address> <databyte>\n",argv[0]);
36 exit(1);
37 }
38
39 /* address is the first number after the program name */
40 address = atoi(argv[1]);
41
42 i2c = open("/dev/i2c-0",O_RDWR); /* open the device dev/i2c-0 */
43 rc=ioctl(i2c,I2C_SLAVE,address); /* set the i2c chip address */
44
45 data[0] = 255; 
46 write(i2c,data,1); /* we send 255 to make all lines go off, ready for input data */
47
48 read(i2c,data,1); /* now data(0) contains the byte of data on the 8 port lines*/
49
50 byte = data[0];
51
52 printf("BYTE = %d \n", byte); 
53 /* BYTE value is 0-256)*/
54 i2c = close(i2c);
55 return(0);
56 }