/* PIC_i2c_one_value.c Read value of A to D input ------------------------------ 1) send the i2c address 2) send StartPoint 3) send 2 data bytes for buffer (address 0/1) 4) read 2 bytes from PIC and print usage :- type with spaces but without the < > use x = 0, 2, 4 to read A to D lines 0,1,2 */ #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 buf[8]; /* 3 bytes - use to feed the i2c driver */ unsigned long address; /* i2c bus address */ int rc; int main(int argc, char** argv) { if (argc != 5) /* report error if we are not getting just 4 inputs after the program name */ { printf("Error. usage: %s i2c_chip_address BufferAddress data_for_buffer[address]-(6 data numbers)\n", argv[0]); } address = atoi(argv[1]); /* i2c decimal address is the first number after the program name */ BufferAddress = atoi(argv[2]); /* the array number for buffer */ DataForBuffer[1] = atoi(argv[3]); /* data for buffer[BufferAddress]*/ DataForBuffer[2] = atoi(argv[4]); /* data for buffer[BufferAddress+1]*/ //------------------------------------------------------------------------send all data then read PicData1 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] = BufferAddress; /* address in buffer to start recording received data */ buf[1] = DataForBuffer[1]; /* first data byte to be sent - used for moving up array of return data*/ buf[2] = DataForBuffer[2]; /* display command byte*/ write(i2c,buf,3); /* we send 7 bytes */ /* the next command forces the PIC to respond - it makes "(state == 0x80)" in the PIC */ read(i2c,buf,1); /* buf(0) now contains 1 data byte from PIC */ PicData[1] = buf[0]; //printf("%d \n", PicData[1]); i2c = close(i2c); //---------------------------------------------------------------------read next data - PicData2 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] = BufferAddress; /* address in buffer to start recording received data */ buf[1] = DataForBuffer[1] + 1; /* first data byte to be sent */ write(i2c,buf,2); /* we send 3 bytes */ /* the next command forces the PIC to respond - it makes "(state == 0x80)" in the PIC */ read(i2c,buf,1); /* buf(0) now contains 1 data byte from PIC */ PicData[2] = buf[0]; //printf("%d \n", PicData[2]); i2c = close(i2c); printf("%d \n", PicData[1] + 256*PicData[2]); }