I2C Address

Set as right shifted byte address (excluding RW at bit 0).
So 0b01101000 is set as (0x68 >> 1), not 0x68.

We always specify I2C addresses as the normal byte address, so we would use (0x68 >> 1) when setting the address to be used.

Example usage (without interrupts)

#include "mcc_generated_files/i2c_host/mssp1.h"

//WRITE BYTES
	uint8_t TxData[2] = {1,2}
	I2C1_Write(MCP3425_I2C_DEVICE_ADDRESS, &TxData[0], 2);
	while (I2C1_IsBusy())
		I2C1_Tasks();
	Result = I2C1_ErrorGet();		//0= no error (success), >0 = error

//READ BYTES
	uint8_t RxData[3];
	I2C1_Read(MCP3425_I2C_DEVICE_ADDRESS, &RxData[0], 3);
	while (I2C1_IsBusy())
		I2C1_Tasks();
	Result = I2C1_ErrorGet();		//0= no error (success), >0 = error

//WRITE READ BYTES
	uint8_t TxData[2] = {1,2}
	uint8_t RxData[3];
	I2C1_WriteRead(MCP3425_I2C_DEVICE_ADDRESS, &TxData[0], 2, &RxData[0], 3);
	while (I2C1_IsBusy())
		I2C1_Tasks();
	Result = I2C1_ErrorGet();		//0= no error (success), >0 = error

How we use in our libraries

In our .h file
bool I2C1_Write_Blocking (uint16_t Address, uint8_t *TxData, size_t TxLength);
bool I2C1_Read_Blocking (uint16_t Address, uint8_t *RxData, size_t RxLength);
bool I2C1_WriteRead_Blocking (uint16_t Address, uint8_t *TxData, size_t TxLength, uint8_t *RxData, size_t RxLength);

//WRITE BYTES
#define IO_EXPANDER_I2C_WRITE(Address, TxData, TxLength)							I2C1_Write_Blocking((Address >> 1), TxData, TxLength)

//READ BYTES
#define IO_EXPANDER_I2C_READ(Address, RxData, RxLength)								I2C1_Read_Blocking((Address >> 1), RxData, RxLength)

//WRITE READ BYTES
#define IO_EXPANDER_I2C_WRITE_READ(Address, TxData, TxLength, RxData, RxLength)		I2C1_WriteRead_Blocking((Address >> 1), TxData, TxLength, RxData, RxLength)
In our .c file
#include "mcc_generated_files/i2c_host/mssp1.h"

//We have to create functions for access because the MCC I2C code is non blocking whereas we want it blocking until complete

//WRITE BYTES
bool I2C1_Write_Blocking (uint16_t Address, uint8_t *TxData, size_t TxLength)
{
	Nop();
	Nop();
	Nop();
	if (I2C1_Write(Address, TxData, TxLength) == 0)
		return(0);
	while (I2C1_IsBusy())
		I2C1_Tasks();
	if (I2C1_ErrorGet() == 0)		//0= no error (success), >0 = error
		return(1);
	else
		return(0);
}

//READ BYTES
bool I2C1_Read_Blocking (uint16_t Address, uint8_t *RxData, size_t RxLength)
{
	if (I2C1_Read(Address, RxData, RxLength) == 0)
		return(0);
	while (I2C1_IsBusy())
		I2C1_Tasks();
	if (I2C1_ErrorGet() == 0)		//0= no error (success), >0 = error
		return(1);
	else
		return(0);
}

//WRITE READ BYTES
bool I2C1_WriteRead_Blocking (uint16_t Address, uint8_t *TxData, size_t TxLength, uint8_t *RxData, size_t RxLength)
{
	if (I2C1_WriteRead(Address, TxData, TxLength, RxData, RxLength) == 0)
		return(0);
	while (I2C1_IsBusy())
		I2C1_Tasks();
	if (I2C1_ErrorGet() == 0)		//0= no error (success), >0 = error
		return(1);
	else
		return(0);
}
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 *