Connect I²C with PIC

The I²C full master MSSP module is available with a number of PIC MCU models. mikroC PRO for PIC provides library which supports the master I²C mode.

  Important :

  • Some MCUs have multiple I²C modules. In order to use the desired I²C library routine, simply change the number 1 in the prototype with the appropriate module number, i.e. I2C2_Init(100000);Connect I²C with PIC

Library Routines

  • I2C1_Init
  • I2C1_Start
  • I2C1_Repeated_Start
  • I2C1_Is_Idle
  • I2C1_Rd
  • I2C1_Wr
  • I2C1_Stop

I2C1_Init

Prototypevoid I2C1_Init(const unsigned long clock);
ReturnsNothing.
DescriptionInitializes I²C with desired clock (refer to device data sheet for correct values in respect with Fosc). Needs to be called before using other functions of I²C Library.
You don’t need to configure ports manually for using the module; library will take care of the initialization.
RequiresLibrary requires MSSP module.
  Note : Calculation of the I²C clock value is carried out by the compiler, as it would produce a relatively large code if performed on the library level. Therefore, compiler needs to know the value of the parameter in the compile time. That is why this parameter needs to be a constant, and not a variable.
Example
I2C1_Init(100000);

I2C1_Start

Prototypeunsigned short I2C1_Start(void);
ReturnsIf there is no error, function returns 0.
DescriptionDetermines if I²C bus is free and issues START signal.
RequiresI²C must be configured before using this function.
Example
I2C1_Start();

I2C1_Repeated_Start

Prototypevoid I2C1_Repeated_Start(void);
ReturnsNothing.
DescriptionIssues repeated START signal.
RequiresI²C must be configured before using this function.
Example
I2C1_Repeated_Start();

I2C1_Is_Idle

Prototypeunsigned short I2C1_Is_Idle(void);
ReturnsReturns 1 if I²C bus is free, otherwise returns 0.
DescriptionTests if I²C bus is free.
RequiresI²C must be configured before using this function.
Example
if (I2C1_Is_Idle()) {...}

I2C1_Rd

Prototypeunsigned short I2C1_Rd(unsigned short ack);
ReturnsReturns one byte from the slave.
DescriptionReads one byte from the slave, and sends not acknowledge signal if parameter ack is 0, otherwise it sends acknowledge.
RequiresI²C must be configured before using this function.
Also, START signal needs to be issued in order to use this function.
ExampleRead data and send not acknowledge signal:
unsigned short take;
...
take = I2C1_Rd(0);

I2C1_Wr

Prototypeunsigned short I2C1_Wr(unsigned short data_);
ReturnsReturns 0 if there were no errors.
DescriptionSends data byte (parameter data) via I²C bus.
RequiresI²C must be configured before using this function.
Also, START signal needs to be issued in order to use this function.
Example
I2C1_Write(0xA3);

I2C1_Stop

Prototypevoid I2C1_Stop(void);
ReturnsNothing.
DescriptionIssues STOP signal.
RequiresI²C must be configured before using this function.
Example
I2C1_Stop();

Connect I²C with PIC schematichCode Example

This code demonstrates use of I²C library. PIC MCU is connected (SCL, SDA pins) to 24c02 EEPROM. Program sends data to EEPROM (data is written at address 2). Then, we read data via I²C from EEPROM and send its value to PORTB, to check if the cycle was successful (see the figure below how to interface 24c02 to PIC).

For more detail: Connect I²C with PIC


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.