Summary of Connect I²C with PIC
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
Important :
- Some MCUs have multiple I²C modules. In order to use the desired I²C library routine, simply change the number
1in 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(const unsigned 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 |
Code listing
|
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 |
Code listing (2)
|
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 |
Code listing (3)
|
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 |
Code listing (4)
|
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:
C source (5)
|
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 |
Code listing (6)
|
I2C1_Stop
| Prototype | void I2C1_Stop(void); |
|---|---|
| Returns | Nothing. |
| Description | Issues STOP signal. |
| Requires | I²C must be configured before using this function. |
| Example |
Code listing (7)
|
Code Example
For more detail: Connect I²C with 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.
Related projects
CAN FD Node based on a PIC18 Microcontroller
The introduction sets the stage for an embedded systems project that involves developing a Embedded C CAN FD project using the MCP2517FD...
Communication & InterfacingLAN9662 Gigabit Switch: Optimized for Industrial Ethernet
Introduction It should come as little surprise therefore that one element of what is now more commonly referred to in some circles...
Communication & InterfacingPIC Microcontroller: Fundamentals & Applications for Students
Introduction Microcontrollers are the key components in the embedded systems market. A variety of embedded systems was formed and developed after the...
Communication & Interfacing10BASE-T1S Ethernet: Simplified OEM Design for Auto Devices
Introduction Cars these days are surrounded by technology. They come loaded with all sorts of electronic gadgets and sensors. This project wants...
Communication & InterfacingDesigning a Microcontroller Framework for PC-Based Control of Electrical Appliances
ABSTRACT The preference for personal computers as a platform for designing and implementing control algorithms is growing due to the ease of...
Communication & InterfacingEfficient Multichannel Data Acquisition with USB Connectivity using PIC Microcontroller
1. INTRODUCTION With the rapid advancements in embedded technology, there is a growing demand for a data acquisition system that combines fast...
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.