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