void LDWordReadI2C(unsigned char SlaveAddress, unsigned char reg, unsigned char *data1, unsigned char *data2){ StartI2C2(); //Send the Start Bit IdleI2C2(); //Wait to complete MasterWriteI2C2(SlaveAddress); //transmit write command IdleI2C2(); //Wait to complete MasterWriteI2C2(reg); IdleI2C2(); StopI2C2(); IdleI2C2(); StartI2C2(); //Send the Start Bit IdleI2C2(); //Wait to complete MasterWriteI2C2(SlaveAddress|0x01); //transmit read command IdleI2C2(); //Wait to complete unsigned char data[2]; MastergetsI2C2(2, data, 30); // "MCHP I2C" *data1=data[0]; *data2=data[1]; // if (status!=0) // while(1); StopI2C2(); //Send the Stop condition IdleI2C2(); //Wait to complete }
char I2C2_READ_String(U8 DevAdd, U8 RegAdd, U8 *RegValue, U8 len) { int DataWait = len*100; char ComRes,DevAddr; DevAddr = (DevAdd<<1)|M_I2C_Write; StartI2C2(); // Send the start bit IdleI2C2(); // Wait to complete while(I2C2STATbits.ACKSTAT); //wait for acknowledgement ComRes=MasterWriteI2C2(DevAddr); // Adress with write mode IdleI2C2(); // Wait to complete while(I2C2STATbits.ACKSTAT); //wait for acknowledgement ComRes=MasterWriteI2C2(RegAdd); // Write the chip ID register loaction for read IdleI2C2(); // Send the start bit while(I2C2STATbits.ACKSTAT); //wait for acknowledgement RestartI2C2(); // Send the start bit IdleI2C2(); // Wait to complete while(I2C2STATbits.ACKSTAT); //wait for acknowledgement DevAddr = (DevAdd<<1)|M_I2C_Read; MasterWriteI2C2(DevAddr); // Adress with write mode IdleI2C2(); // Wait to complete while(I2C2STATbits.ACKSTAT); //wait for acknowledgement MastergetsI2C2(len, RegValue, DataWait); // Read the Register value IdleI2C2(); // Wait to complete NotAckI2C2(); // Not Acknowledge I2C IdleI2C2(); // Wait to complete StopI2C2(); // Stop I2C communication IdleI2C2(); // Wait to complete; return ComRes; }
/***************************************************************************** * Function Name : gyroReadString * Description : It reads predetermined data string length from the I2C bus. * Parameters : length is the string length to read * data is the storage for received gyro data * data_wait is the timeout value * Return Value : Number of bytes read before timeout. *****************************************************************************/ static inline unsigned int gyroReadString(unsigned length, unsigned char * data, unsigned int data_wait) { return MastergetsI2C2(length, data, data_wait); }
unsigned int i2cReadString(unsigned char channel, unsigned length, unsigned char * data, unsigned int data_wait) { if (channel == 1) { return MastergetsI2C1(length, data, data_wait); } else { return MastergetsI2C2(length, data, data_wait); } }