The article details the mikroC PRO for PIC library routines for the I²C full master MSSP module. It explains initialization, start/stop signals, data reading/writing, and bus status checking. A code example demonstrates interfacing a PIC MCU with a 24c02 EEPROM using SCL and SDA pins to write and read data successfully.
Parts used in the I²C Master Project:
PIC MCU
24c02 EEPROM
SCL pin
SDA pin
PORTB
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);
Library Routines
I2C1_Init
I2C1_Start
I2C1_Repeated_Start
I2C1_Is_Idle
I2C1_Rd
I2C1_Wr
I2C1_Stop
I2C1_Init
Prototype
void I2C1_Init(constunsigned long clock);
Returns
Nothing.
Description
Initializes 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.
Requires
Library 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
Prototype
unsigned short I2C1_Start(void);
Returns
If there is no error, function returns 0.
Description
Determines if I²C bus is free and issues START signal.
Requires
I²C must be configured before using this function.
Example
I2C1_Start();
I2C1_Repeated_Start
Prototype
void I2C1_Repeated_Start(void);
Returns
Nothing.
Description
Issues repeated START signal.
Requires
I²C must be configured before using this function.
Example
I2C1_Repeated_Start();
I2C1_Is_Idle
Prototype
unsigned short I2C1_Is_Idle(void);
Returns
Returns 1 if I²C bus is free, otherwise returns 0.
Description
Tests if I²C bus is free.
Requires
I²C must be configured before using this function.
Example
if (I2C1_Is_Idle()) {...}
I2C1_Rd
Prototype
unsigned short I2C1_Rd(unsigned short ack);
Returns
Returns one byte from the slave.
Description
Reads one byte from the slave, and sends not acknowledge signal if parameter ack is 0, otherwise it sends acknowledge.
Requires
I²C must be configured before using this function.
Also, START signal needs to be issued in order to use this function.
Example
Read data and send not acknowledge signal:
unsigned short take;
...
take = I2C1_Rd(0);
I2C1_Wr
Prototype
unsigned short I2C1_Wr(unsigned short data_);
Returns
Returns 0 if there were no errors.
Description
Sends data byte (parameter data) via I²C bus.
Requires
I²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
Prototype
void I2C1_Stop(void);
Returns
Nothing.
Description
Issues STOP signal.
Requires
I²C must be configured before using this function.
Example
I2C1_Stop();
Code 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).
Quick Solutions to Questions related to I²C Master Project:
How do I initialize the I²C module? Call the I2C1_Init function with the desired clock value before using other functions.
Can I use multiple I²C modules on one MCU? Yes, simply change the number in the routine prototype from 1 to the appropriate module number.
Does the I2C1_Start function return an error code? It returns 0 if there is no error when determining if the bus is free and issuing the START signal.
What happens if the ack parameter is 0 in I2C1_Rd? The function sends a not acknowledge signal after reading one byte from the slave.
Is manual port configuration required for this library? No, the library takes care of the initialization so you do not need to configure ports manually.
Why must the clock parameter be a constant? The compiler calculates the I²C clock value at compile time to avoid producing large code.
When should I use I2C1_Repeated_Start? Use it to issue a repeated START signal after configuring the I²C module.
How can I check if the I²C bus is free? Call the I2C1_Is_Idle function which returns 1 if the bus is free and 0 otherwise.
What is required before sending data with I2C1_Wr? You must configure the I²C and issue a START signal before using this function.
What does the provided code example demonstrate? It shows writing data to address 2 of a 24c02 EEPROM and reading it back to PORTB.
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.
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. ACCEPTCheck Privacy Policy
Manage consent
Privacy Overview
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.