Master Fast Read (no write of regsiter address first)


#define	MEMS_SENSOR_I2C_MASTER_IRQ_FLAG				0x0080

	i2c_comms_failed = 0;

	//----- SEND THE START CONDITION -----
	I2C4CON |= 0x0001;			//Set SEN
	while (I2C4CON & 0x0001)	//Wait for SEN to clear
		;


	//----- SEND THE I2C DEVICE ADDRESS WITH THE READ BIT SET -----
	IFS1 &= ~MEMS_SENSOR_I2C_MASTER_IRQ_FLAG;
	I2C4TRN = (MEMS_SENSOR_B_I2C_ADDRESS | 0x01);			//Bit 1 high for read
	while (!(IFS1 & MEMS_SENSOR_I2C_MASTER_IRQ_FLAG))
		;
	//Check ACK bit
	if (I2C4STAT & 80000)
		i2c_comms_failed = 1;			//Should be low for ACK

	//----- READ THE DATA BYTES -----
	for (count = 0; count < 6; count++)
	{
		//Read byte
		IFS1 &= ~MEMS_SENSOR_I2C_MASTER_IRQ_FLAG;
		I2C4CON |= 0x0008;			//Set RCEN
		while (!(IFS1 & MEMS_SENSOR_I2C_MASTER_IRQ_FLAG))
			;
		data[count] = I2C4RCV;

		//Acknowledge
		while (I2C4CON & 0x001f)	//The five Least Significant bits of I2CxCON must be ‘0’ (master logic inactive) before attempting to set the ACKEN bit
			;
		if (count < (6-1))
			I2C4CON &= ~0x0020;		//Set ACKDT (0=ACK, 1=NAK)
		else
			I2C4CON |= 0x0020;		//Set ACKDT (0=ACK, 1=NAK)
		I2C4CON |= 0x0010;			//Set ACKEN
		while (I2C4CON & 0x0010)
			;

		if (i2c_comms_failed)
			data[count] = 0;
	}

	//----- SEND STOP -----
	I2C4CON |= 0x0004;			//Set PEN
	while (I2C4CON & 0x0004)	//Wait for PEN to clear
		;

 

 

 

UK Electronic Designer
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.