/* PCF8591_addr_line_V-slug.c -------------------------- read the voltage on one A/D line (0-3) and set the output voltage on the D/A pin -------------------------------------------------------------------------------- 1) send the i2c address 2) send A/D line number (program adds 64 to enable D/A in control register) 3) send byte for D/A out 3) read the chosen A/D usage :- type with spaces but without the < >
(even if the D/A pin is unused type in any value 0-255) */ #include #include #include #include #include #include #define I2C_SLAVE 0x0703 /* Change slave address */ int i2c; int AD_line; /* line number 0-3 */ int V_out; /* value for the D/A out line */ unsigned char buf[1]; /* use to feed the i2c driver */ unsigned long address; /* i2c bus address */ int byte_in_0; /* the 8 bit byte that represents the voltage on the AD_line */ int rc; int main(int argc, char** argv) { if (argc != 4) { /* report error if we are not getting just 3 inputs after the program name */ fprintf(stderr, "Error. usage: %s i2c_chip_address A/D_line_(0-3) D/A_Vout(0-255)\n",argv[0]); exit(1); } address = atoi(argv[1]); /* address is the first number after the program name */ AD_line = atoi(argv[2]); /* A/D input line 0-3 */ AD_line = AD_line + 64; /* enable the D/A output pin */ V_out = atoi(argv[3]); /* A/D input line 0-3 */ i2c = open("/dev/i2c-0",O_RDWR); /* open the device dev/i2c-0 */ rc=ioctl(i2c,I2C_SLAVE,address); /* set the i2c chip address */ buf[0] = AD_line; /* A/D input line number is the first data byte sent */ buf[1] = V_out; /* D/A out value is the second data byte sent */ write(i2c,buf,2); /* we send 2 bytes */ read(i2c,buf,1); /* buf(0) contains the byte of data in the chip cache */ /* do it again */ usleep (1000); read(i2c,buf,1); /* buf(0) now contains the byte of data from the most recent analogue input*/ printf("%d \n", buf[0]); /* buf[0] goes 0 to 255 as voltage input goes from 0 to supply voltage*/ i2c = close(i2c); return(0); } /* Sunspot 070128 */