/* PIC_i2c.c ------------------------------ 1) send the i2c address 2) send buffer address 3) send 6 data bytes for buffer (address 0/1/2/3/4/5) 4) read 8 bytes from PIC and print usage :- type with spaces but without the < > <16F690test1> <6 datas for buffer[ ]> */ #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 != 9) /* report error if we are not getting just 8 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]*/ DataForBuffer[3] = atoi(argv[5]); /* data for buffer[BufferAddress+2]*/ DataForBuffer[4] = atoi(argv[6]); /* data for buffer[BufferAddress+3]*/ DataForBuffer[5] = atoi(argv[7]); /* data for buffer[BufferAddress+4]*/ DataForBuffer[6] = atoi(argv[8]); /* data for buffer[BufferAddress+5]*/ //------------------------------------------------------------------------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]; /* second data byte to be sent - use to callibrate thermometer?*/ buf[3] = DataForBuffer[3]; /* third data byte to be sent - use to callibrate thermometer?*/ buf[4] = DataForBuffer[4]; /* fourth data byte to be sent - spare */ buf[5] = DataForBuffer[5]; /* fifth data byte to be sent - spare */ buf[6] = DataForBuffer[6]; /* sixth data byte to be sent - spare */ write(i2c,buf,7); /* 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("PicData 1 = %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("PicData 2 = %d \n", PicData[2]); i2c = close(i2c); //------------------------------------------------------------------------read next data - PicData3 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] + 2; /* 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[3] = buf[0]; printf("PicData 3 = %d \n", PicData[3]); i2c = close(i2c); //------------------------------------------------------------------------read next data - PicData4 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] + 3; /* first data byte to be sent */ write(i2c,buf,2); /* we send 2 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[4] = buf[0]; printf("PicData 4 = %d \n", PicData[4]); i2c = close(i2c); //------------------------------------------------------------------------read next data - PicData5 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] + 4; /* first data byte to be sent */ write(i2c,buf,2); /* we send 2 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[5] = buf[0]; printf("PicData 5 = %d \n", PicData[5]); i2c = close(i2c); //------------------------------------------------------------------------read next data - PicData6 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] + 5; /* first data byte to be sent */ write(i2c,buf,2); /* we send 2 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[6] = buf[0]; printf("PicData 6 = %d \n", PicData[6]); i2c = close(i2c); //------------------------------------------------------------------------read next data - PicData7 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] + 6; /* first data byte to be sent */ write(i2c,buf,2); /* we send 2 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[7] = buf[0]; printf("PicData 7 = %d \n", PicData[7]); i2c = close(i2c); return(0); //------------------------------------------------------------------------read next data - PicData8 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] + 7; /* first data byte to be sent */ write(i2c,buf,2); /* we send 2 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[8] = buf[0]; printf("PicData 8 = %d \n", PicData[8]); i2c = close(i2c); return(0); }