Configure I2C in MCC

Once you Generate the MCC code it will have dealt with initialising the I2C port ready for you to use it.

In your code
#include <definitions.h>			//Include all the MCC generated files so their fucntions can be called




//IN YOUR INITIALSE CODE
	//----- SETUP I2C 4 -----
	//Used for: 
	//Setup I2C using MCC:
	//	Setup I2C:
	//		Operating Mode: Master
	//		Disable Slew Rate Control: No (disable for Standard Speed mode (100 kHz) and 1 MHz mode, enable for High Speed mode (400 kHz))
	//		SMBus Input Levels: No
	//		Stop in Idle Mode bit: Yes (Yes=Discontinue module operation when the device enters Idle mode)
	//		I2C Band Rate (hz): 400000
	//		SDA Hold Time: Min 100nS (Delay shouldn't normally be needed)
	//		Include Force Write I2C Function: No
	//	Setup interrupts:
	//		Enable I2C4 Bus Collision Event Interrupt = Will already be set to Yes
	//		Enable I2C4 SlaveEvent Interrupt = Will already be set to Yes
	//		Enable I2C4 Master Event Interrupt: Will already be set to Yes
	//		Priority (set for all): 3	//<<<Set as required for your application
	//		Subpriority (set for all): 0	//<<<Set as required for your application
	
	//I2C4_CallbackRegister(I2C4_MyEventHandler,(uintptr_t)NULL);		//Register my callback function to be called when the I2C operation completes (optional)


//HOW TO PERFORM A READ OPERATION
/*
#define	MY_I2C_ADDRESS		0xa0
	uint8_t RxData[1];
	if (I2C4_Read((MY_I2C_ADDRESS >> 1), &RxData[0], sizeof(RxData)))		//Address needs passing in bits6:0
	{
		//READ STARTED		
		while (I2C4_IsBusy())
			;
		
		//READ COMPLETED
		if (I2C4_ErrorGet() == I2C_ERROR_NONE)
		{
			
		}
	}
*/


//HOW TO PERFORM A WRITE OPERATION
/*
#define	MY_I2C_ADDRESS		0xa0
	uint8_t TxData[1] = {0x00};
	if (I2C4_Write((MY_I2C_ADDRESS >> 1), &TxData[0], sizeof(TxData)))		//Address needs passing in bits6:0
	{
		//WRITE STARTED
		while (I2C4_IsBusy())
			;
		
		//WRITE COMPLETED
		if (I2C4_ErrorGet() == I2C_ERROR_NONE)
		{
			
		}
	}
*/


//HOW TO PERFORM A WRITE READ OPERATION
/*
#define	MY_I2C_ADDRESS		0xa0
	uint8_t RxData[1];
	uint8_t TxData[1] = {0x00};
	if (I2C4_WriteRead((MY_I2C_ADDRESS >> 1), &TxData[0], sizeof(TxData), &RxData[0], sizeof(RxData)))		//Address needs passing in bits6:0
	{
		//WRITE READ STARTED
		while (I2C4_IsBusy())
			;
		
		//WRITE READ COMPLETED
		if (I2C4_ErrorGet() == I2C_ERROR_NONE)
		{
			
		}
	}
*/


//*************************************************
//*************************************************
//********** I2C4 OPERATION COMPLETE IRQ **********
//*************************************************
//*************************************************
/*
void I2C4_MyEventHandler (uintptr_t context)
{
	if (I2C4_ErrorGet() != I2C_ERROR_NONE)
	{
		//AN ERROR OCCURED
	}
	else
	{
		//LAST OPERATION COMPLETED (Stop bit)
		
	}
}
*/
Other UART functions

MCC will have created a few useful functions you can call – see the file it generated in: /config/default/peripheral/i2c/

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.

Comments

Your email address will not be published. Required fields are marked *