ads1115 analog-to-digital 4-channel added
[i2c-telemetry.git] / pcf8591_1.c
1 /* 
2 PCF8591-all-lines_addr_V-slug.c
3 ---------------------
4 read the voltage on all A/D lines (0-3) and set the output voltage on the D/A pin
5 ---------------------------------------------------------------------------------
6
7 1) send the i2c address
8 3) send byte for D/A out
9 3) read all 4 A/D lines
10 usage :- type with spaces but without the < >
11 <PCF8591_address_readvolts> <address as decimal> <D/A value as decimal 0-255>
12 (even if the D/A pin is unused type in any value 0-255)
13 */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <sys/ioctl.h>
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <linux/i2c-dev.h>
21 #define I2C_SLAVE0x0703/* Change slave address*/
22 int i2c; 
23 int AD_line; /* line number 0-3 */
24 int V_out; /* value for the D/A out line */
25 unsigned char buf[1]; /* use to feed the i2c driver */
26 unsigned long address; /* i2c bus address */
27 int byte_in_0; /* the 8 bit byte that represents the voltage on the AD_line */
28 int AD_line_0; /* volts byte on line number 0 */
29 int AD_line_1; /* volts byte on line number 1 */
30 int AD_line_2; /* volts byte on line number 2 */
31 int AD_line_3; /* volts byte on line number 3 */
32 int rc;
33
34 int main(int argc, char** argv)
35 {
36 if (argc != 3) /* report error if we are not getting just 2 inputs after the program name */
37
38 {
39
40 printf("Error. usage: %s i2c_chip_address D/A_Vout(0-255)\n", argv[0]);
41
42 }
43
44 address = atoi(argv[1]); /* address is the first number after the program name */
45
46 V_out = atoi(argv[2]); /* A/D input line 0-3 */
47
48 i2c = open("/dev/i2c-0",O_RDWR); /* open the device dev/i2c-0 */
49 rc=ioctl(i2c,I2C_SLAVE,address); /* set the i2c chip address */
50
51 AD_line = 0 + 64; /* enable the D/A output pin 0*/
52 buf[0] = AD_line; /* A/D input line number is the first data byte sent */
53 buf[1] = V_out; /* D/A out value is the second data byte sent */
54 write(i2c,buf,2); /* we send 2 bytes <control register> <D/A value> */
55 read(i2c,buf,1); /* buf(0) contains the byte of data in the chip cache */
56 sleep (1);      /* do it again */
57 read(i2c,buf,1); /* buf(0) now contains the byte of data from the most recent analogue input*/
58 AD_line_0 = buf[0];
59
60 AD_line = 1 + 64; /* enable the D/A output pin 0*/
61 buf[0] = AD_line; /* A/D input line number is the first data byte sent */
62 buf[1] = V_out; /* D/A out value is the second data byte sent */
63 write(i2c,buf,2); /* we send 2 bytes <control register> <D/A value> */
64 read(i2c,buf,1); /* buf(0) contains the byte of data in the chip cache */
65 //sleep (1);    /* do it again */
66 // read(i2c,buf,1); /* buf(0) now contains the byte of data from the most recent analogue input*/
67 AD_line_1 = buf[0];
68
69 AD_line = 2 + 64; /* enable the D/A output pin 0*/
70 buf[0] = AD_line; /* A/D input line number is the first data byte sent */
71 buf[1] = V_out; /* D/A out value is the second data byte sent */
72 write(i2c,buf,2); /* we send 2 bytes <control register> <D/A value> */
73 read(i2c,buf,1); /* buf(0) contains the byte of data in the chip cache */
74 //sleep (1);    /* do it again */
75 // read(i2c,buf,1); /* buf(0) now contains the byte of data from the most recent analogue input*/
76 AD_line_2 = buf[0];
77
78 AD_line = 3 + 64; /* enable the D/A output pin 0*/
79 buf[0] = AD_line; /* A/D input line number is the first data byte sent */
80 buf[1] = V_out; /* D/A out value is the second data byte sent */
81 write(i2c,buf,2); /* we send 2 bytes <control register> <D/A value> */
82 read(i2c,buf,1); /* buf(0) contains the byte of data in the chip cache */
83 //sleep (1);    /* do it again */
84 // read(i2c,buf,1); /* buf(0) now contains the byte of data from the most recent analogue input*/
85 AD_line_3 = buf[0];
86
87 printf("A/D line 0 = %d \n", AD_line_0, " "); 
88 printf("A/D line 1 = %d \n", AD_line_1, " ");
89 printf("A/D line 2 = %d \n", AD_line_2, " ");
90 printf("A/D line 3 = %d \n", AD_line_3, " ");
91 /*AD_line_x goes 0 to 255 as voltage input goes from 0 to supply voltage*/
92
93 i2c = close(i2c);
94 return(0);
95 }