I²C Library
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 :
- I²C routines require you to specify the module you want to use. To select the desired I²C, simply change the letter
xin the prototype for a number from1to2. - Number of I²C modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
Library Routines
- I2Cx_Init
- I2Cx_Start
- I2Cx_Repeated_Start
- I2Cx_Is_Idle
- I2Cx_Rd
- I2Cx_Wr
- I2Cx_Stop
- I2Cx_SetTimeoutCallback
- I2C_Set_Active
Generic Routines
I2Cx_Init
| Prototype |
void I2Cx_Init(const unsigned long clock); // for PIC18F K42 and K83 familyvoid I2Cx_Init(); |
|---|---|
| Returns |
Nothing. |
| Description |
Initializes I²C with desired You don’t need to configure ports manually for using the module; library will take care of the initialization. Note :
|
| Requires |
Library requires MSSP module. |
| Example |
I2C1_Init(100000);// for PIC18F K42 and K83 family I2C1_Init(); |
I2Cx_Start
| Prototype |
unsigned short I2Cx_Start(void); |
|---|---|
| Returns |
If there is no error, function returns 0. |
| Description |
Determines if I²C bus is free and issues START signal. Note :
|
| Requires |
I²C must be configured before using this function. See I2Cx_Init. |
| Example |
I2C1_Start(); |
I2Cx_Repeated_Start
| Prototype |
void I2Cx_Repeated_Start(void); |
|---|---|
| Returns |
Nothing. |
| Description |
Issues repeated START signal. Note :
|
| Requires |
I²C must be configured before using this function. See I2Cx_Init. |
| Example |
I2C1_Repeated_Start(); |
I2Cx_Is_Idle
| Prototype |
unsigned short I2Cx_Is_Idle(void); |
|---|---|
| Returns |
Returns 1 if I²C bus is free, otherwise returns 0. |
| Description |
Tests if I²C bus is free. Note :
|
| Requires |
I²C must be configured before using this function. See I2Cx_Init. |
| Example |
if (I2C1_Is_Idle()) {...}
|
I2Cx_Rd
| Prototype |
unsigned short I2Cx_Rd(unsigned short ack); // for PIC18F K42 and K83 familyunsigned short I2Cx_Rd(char slave_address, char *ptrdata, unsigned short count, unsigned short END_mode); |
||||||
|---|---|---|---|---|---|---|---|
| Returns |
Returns one byte from the slave. |
||||||
| Description |
PIC12F/16F/18F
PIC18F K42 and K83 family
Note :
|
||||||
| Requires |
I²C must be configured before using this function. See I2Cx_Init. Also, START signal needs to be issued in order to use this function. See I2Cx_Start (not valid for PIC18F K42 and K83 family). |
||||||
| Example |
Read data and send not acknowledge signal: unsigned short take; ... take = I2C1_Rd(0); |
I2Cx_Wr
| Prototype |
unsigned short I2Cx_Wr(unsigned short data_); // for PIC18F K42 and K83 familyunsigned short I2Cx_Wr(char slave_address, char *ptrdata, unsigned short count, unsigned short END_mode); |
||||||
|---|---|---|---|---|---|---|---|
| Returns |
Returns 0 if there were no errors. |
||||||
| Description |
PIC12F/16F/18F
PIC18F K42 and K83 family
Note :
|
||||||
| Requires |
I²C must be configured before using this function. See I2Cx_Init. Also, START signal needs to be issued in order to use this function. See I2Cx_Start (not valid for PIC18F K42 and K83 family). |
||||||
| Example |
I2C1_Write(0xA3); |
I2Cx_Stop
| Prototype |
void I2Cx_Stop(void); |
|---|---|
| Returns |
Nothing. |
| Description |
Issues STOP signal. Note :
|
| Requires |
I²C must be configured before using this function. See I2Cx_Init. |
| Example |
I2C1_Stop(); |
I2Cx_SetTimeoutCallback
| Prototype |
void I2Cx_SetTimeoutCallback(unsigned long timeout, void (*i2c1_timeout)(char)); |
|---|---|
| Returns |
Nothing. |
| Description |
This function checks for a I²C timeout condition in a user defined time period, after which it jumps to a callback function if this condition is met.
|
| Requires |
|
| Example |
// define callback function
void I2C1_TimeoutCallback(char errorCode) {
if (errorCode == _I2C_TIMEOUT_RD) {
// do something if timeout is caused during read
}
if (errorCode == _I2C_TIMEOUT_WR) {
// do something if timeout is caused during write
}
if (errorCode == _I2C_TIMEOUT_START) {
// do something if timeout is caused during start
}
if (errorCode == _I2C_TIMEOUT_REPEATED_START) {
// do something if timeout is caused during repeated start
}
}
// initialize I2C module
I2C1_Init(100000);
// set timeout period and callback function
I2C1_SetTimeoutCallback(1000, I2C1_TimeoutCallback);
|
I2C_Set_Active
| Prototype |
void I2C_Set_Active(unsigned short (*start_ptr)(), void (*restart_ptr)(), unsigned short (*read_ptr)(unsigned short), unsigned short(*write_ptr)(unsigned short), void (*stop_ptr)(), unsigned short(*is_idle_ptr)()); // for PIC18F K42 and K83 familyvoid I2C_Set_Active(unsigned short (*read_ptr)(char, char *, unsigned short, unsigned short), unsigned short(*write_ptr)(char, char *, unsigned short, unsigned short), unsigned short(*is_idle_ptr)()); |
|---|---|
| Description |
Sets the active I²C module which will be used by the I²C routines. |
| Parameters |
|
| Returns |
Nothing. |
| Requires |
|
| Example |
I2C_Set_Active(&I2C1_Start, &I2C1_Repeated_Start, &I2C1_Wr, &I2C1_Wr, &I2C1_Stop, &I2C1_Is_Idle); // Sets the I2C1 module active |
| Notes |
Number of I²C modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library. |
I2C_Start
| Prototype |
unsigned short I2C_Start(void); |
|---|---|
| Returns |
If there is no error, function returns 0. |
| Description |
Determines if I²C bus is free and issues START signal. This is a generic routine which uses the active I²C module previously activated by the I2C_Set_Active routine. |
| Requires |
I²C must be configured before using this function. See I2Cx_Init. |
| Example |
I2C_Start(); |
I2C_Restart
| Prototype |
void I2C_Restart(void); |
|---|---|
| Returns |
Nothing. |
| Description |
Issues repeated START signal. This is a generic routine which uses the active I²C module previously activated by the I2C_Set_Active routine. |
| Requires |
I²C must be configured before using this function. See I2Cx_Init. |
| Example |
I2C_Restart(); |
I2C_Is_Idle
| Prototype |
unsigned short I2C_Is_Idle(void); |
|---|---|
| Returns |
Returns 1 if I²C bus is free, otherwise returns 0. |
| Description |
Tests if I²C bus is free. This is a generic routine which uses the active I²C module previously activated by the I2C_Set_Active routine. |
| Requires |
I²C must be configured before using this function. See I2Cx_Init. |
| Example |
if (I2C_Is_Idle()) {...}
|
I2C_Rd
| Prototype |
unsigned short I2C_Rd(unsigned short ack); // for PIC18F K42 and K83 familyunsigned short I2C_Rd(char slave_address, char *ptrdata, unsigned short count, unsigned short END_mode); |
||||||
|---|---|---|---|---|---|---|---|
| Returns |
Returns one byte from the slave. |
||||||
| Description |
PIC12F/16F/18F
PIC18F K42 and K83 family
This is a generic routine which uses the active I²C module previously activated by the I2C_Set_Active routine. |
||||||
| Requires |
I²C must be configured before using this function. See I2Cx_Init. Also, START signal needs to be issued in order to use this function. See I2C_Start (not valid for PIC18F K42 and K83 family). |
||||||
| Example |
Read data and send not acknowledge signal: unsigned char take; ... I2C_Set_Active(&I2C1_Start, &I2C1_Restart, &I2C1_Read, &I2C1_Write, &I2C1_Stop, &I2C1_Is_Idle); // Sets the I2C1 module active // Read data and send the not_acknowledge signal take = I2C_Read(_I2C_NACK); |
I2C_Wr
| Prototype |
unsigned short I2C_Wr(unsigned short data_); // for PIC18F K42 and K83 familyunsigned short I2C_Wr(char slave_address, char *ptrdata, unsigned short count, unsigned short END_mode); |
||||||
|---|---|---|---|---|---|---|---|
| Returns |
Returns 0 if there were no errors. |
||||||
| Description |
PIC12F/16F/18F
PIC18F K42 and K83 family
This is a generic routine which uses the active I²C module previously activated by the I2C_Set_Active routine. |
||||||
| Requires |
I²C must be configured before using this function. See I2Cx_Init. Also, START signal needs to be issued in order to use this function. See I2C_Start (not valid for PIC18F K42 and K83 family). |
||||||
| Example |
unsigned char data_; unsigned error ... I2C_Set_Active(&I2C1_Start, &I2C1_Restart, &I2C1_Read, &I2C1_Write, &I2C1_Stop, &I2C1_Is_Idle); // Sets the I2C1 module active error = I2C_Write(data_); error = I2C_Write(0xA3); |
I2C_Stop
| Prototype |
void I2C_Stop(void); |
|---|---|
| Returns |
Nothing. |
| Description |
Issues STOP signal. This is a generic routine which uses the active I²C module previously activated by the I2C_Set_Active routine. |
| Requires |
I²C must be configured before using this function. See I2Cx_Init. |
| Example |
I2C_Set_Active(&I2C1_Start, &I2C1_Restart, &I2C1_Read, &I2C1_Write, &I2C1_Stop, &I2C1_Is_Idle); // Sets the I2C1 module active // Issue STOP signal I2C_Stop(); |
| Notes |
None. |
Library 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).
void main(){
ANSEL = 0; // Configure AN pins as digital I/O
ANSELH = 0;
PORTB = 0;
TRISB = 0; // Configure PORTB as output
I2C1_Init(100000); // initialize I2C communication
I2C1_Start(); // issue I2C start signal
I2C1_Wr(0xA2); // send byte via I2C (device address + W)
I2C1_Wr(2); // send byte (address of EEPROM location)
I2C1_Wr(0xAA); // send data (data to be written)
I2C1_Stop(); // issue I2C stop signal
Delay_100ms();
I2C1_Start(); // issue I2C start signal
I2C1_Wr(0xA2); // send byte via I2C (device address + W)
I2C1_Wr(2); // send byte (data address)
I2C1_Repeated_Start(); // issue I2C signal repeated start
I2C1_Wr(0xA3); // send byte (device address + R)
PORTB = I2C1_Rd(0u); // Read the data (NO acknowledge)
I2C1_Stop(); // issue I2C stop signal
}
HW Connection

Interfacing 24c02 to PIC via I²C
What do you think about this topic ? Send us feedback!




