//-----------------------------------------------
	//----- SETUP SYNCHRONOUS SERIAL PORT (SSP) -----
	//-----------------------------------------------
	//----- SETUP SSP AS I2C -----
	//Used for: LED Driver
	//I2C requirements:- 100kHz clock high time 4.0uS, clock low time 4.7uS, 400kHz clock high time 0.6uS, clock low time 1.3uS
	//The PIC18 does not fully conform to the 400kHz I2C spec (which applies to all rates >100kHz) but may be used with care
	//where higher rates are required.
	//Lower 7 bits of SSPADD are the baud rate.  ((Tcycle / 2) * SSPADD * 2) = time per bit.  1 / time per bit = frequency.
	//400kHz @ 40MHz OSC = SSPADD 25
	//400kHz @ 16MHz OSC = SSPADD 9
	//100kHz @ 40MHz OSC = SSPADD 100
	//100kHz @ 32MHz OSC = SSPADD 80
	//100kHz @ 16MHz OSC = SSPADD 39
	//100kHz @ 4MHz OSC = SSPADD 10
	SSPADD = 39;					//setup i2c clk speed
	OpenI2C(MASTER, SLEW_OFF);		//Slew rate control should be off for 100KHz, on for 400KHz
#define	LED_DRIVER_I2C_START_IN_PROGRESS_BIT		SSP1CON2bits.SEN
#define	LED_DRIVER_I2C_RESTART_IN_PROGRESS_BIT	SSP1CON2bits.RSEN
#define	LED_DRIVER_I2C_WRITE_BYTE(a)				WriteI2C1(a)
#define	LED_DRIVER_I2C_TX_IN_PROGRESS_BIT			SSP1STATbits.R_W
#define	LED_DRIVER_I2C_ACK_NOT_RECEIVED_BIT		SSP1CON2bits.ACKSTAT
#define	LED_DRIVER_I2C_READ_BYTE(a)				a=ReadI2C1()			//<<<< UPDATE THIS FUNCTION IF USING DIFFERENT I2C PORT!!
#define	LED_DRIVER_I2C_ACK_IN_PROGRESS_BIT		SSP1CON2bits.ACKEN
#define	LED_DRIVER_I2C_STOP_IN_PROGRESS_BIT		SSP1CON2bits.PEN

//StopI2C1 - Generate bus stop condition
#define LED_DRIVER_I2C_STOP_I2C StopI2C1()

//StartI2C1 - Generate bus start condition
#define LED_DRIVER_I2C_START_I2C StartI2C1()

//RestartI2C1 - Generate bus restart condition
#define LED_DRIVER_I2C_RESTART_I2C RestartI2C1()

//NotAckI2C1 - Generate bus Not ACK condition
#define LED_DRIVER_I2C_NOT_ACK NotAckI2C1()

//AckI2C1 - Generate bus ACK condition
#define LED_DRIVER_I2C_ACK AckI2C1()

//Idle I2C1 -  Test if I2C1 module is idle
#define LED_DRIVER_I2C_IDLE_I2C IdleI2C1()
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.