/* PIC_i2c_one_ADnumber_only_a.c Read value of A to D input ------------------------------ 1) send the i2c address 2) send x value - this determines which byte-pairs are sent from the PIC 3) send 6 bytes for buf[0-6] usage :- type with spaces but without the < > <5 bytes> use x = 0, 2, 4 to read A to D lines 0,1,2 number comes as just 0-1023 ./PIC_i2c_one_ADnumber_only_a 80 2 0 0 0 0 0 read line 1 (use 0,2,4) */ #include #include #include #include #include #include #define I2C_SLAVE 0x0703 /* Change slave address */ int i2c; int BufferAddress; /* which buffer[] to hold start of data */ int DataForBuffer[8]; /* values for the PIC buffer[BufferAddress] */ int PicData[8]; /* byte sent by PIC*/ unsigned char tx_buf[8]; /* bytes to send */ unsigned char rx_buf[8]; /* received bytes */ unsigned long address; /* i2c bus address */ int rc; int main(int argc, char** argv) { if (argc != 8) /* report error if we are not getting just 7 inputs after the program name */ { printf("Error. usage: %s i2c_chip_address BufferAddress (6 data numbers)\n", argv[0]); } address = atoi(argv[1]); /* i2c decimal address is the first number after the program name */ tx_buf[0] = atoi(argv[2]); tx_buf[1] = atoi(argv[3]); tx_buf[2] = atoi(argv[4]); tx_buf[3] = atoi(argv[5]); tx_buf[4] = atoi(argv[6]); tx_buf[5] = atoi(argv[7]); //-----------------------------------------------------------------get lo byte i2c = open("/dev/i2c-0",O_RDWR); /* open the device dev/i2c-0 */ rc=ioctl(i2c,I2C_SLAVE,address); /* set the i2c chip address */ write(i2c,tx_buf,6); /* we send 7 bytes
*/ read(i2c,rx_buf,1); /* rx_buf(0) now contains 2 data byte from PIC */ PicData[1] = rx_buf[0]; // printf("lo byte %d \n", PicData[1]); //-----------------------------------------------------------------get hi byte tx_buf[0] = tx_buf[0] + 1; /* increment x to get hi byte*/ write(i2c,tx_buf,6); /* we send data again with x incremented */ read(i2c,rx_buf,1); /* rx_buf(0) now contains 1 data byte from PIC */ PicData[2] = rx_buf[0]; // printf("hi byte %d \n", PicData[2]); i2c = close(i2c); printf("%d \n", PicData[1] + 256*PicData[2]); } // NB x on the display show value sent + 1