I2C Remappable Library
mikroC PRO for PIC provides a library which simplifies usage of the I²C Remappable Module. This module is available for the MCUs with remappable pins, please check appropriate datasheet.

- Before using this library, make sure that Peripheral Pin Select Library is checked in the Library Manager, and that appropriate pins were mapped.
- I²C routines require you to specify the module you want to use. To select the desired I²C, simply change the letter
x
in the prototype for a number from1
to2
. - Number of I²C modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
Library Routines
- I2Cx_Remappable_Init
- I2Cx_Remappable_Start
- I2Cx_Remappable_Repeated_Start
- I2Cx_Remappable_Is_Idle
- I2Cx_Remappable_Rd
- I2Cx_Remappable_Wr
- I2Cx_Remappable_Stop
- I2Cx_SetTimeoutCallback
- I2C_Set_Active
Generic Routines
- I2C_Remappable_Start
- I2C_Remappable_Repeated_Start
- I2C_Remappable_Is_Idle
- I2C_Remappable_Rd
- I2C_Remappable_Wr
- I2C_Remappable_Stop
I2Cx_Remappable_Init
Prototype |
void I2Cx_Remappable_Init(const unsigned long clock); // for PIC18F K42 and K83 familyvoid I2Cx_Remappable_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. ![]()
|
Requires |
Library requires MSSP module. |
Example |
I2C1_Remappable_Init(100000); // for PIC18F K42 and K83 family I2C1_Remappable_Init(); |
I2Cx_Remappable_Start
Prototype |
unsigned short I2Cx_Remappable_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. See I2Cx_Remappable_Init. |
Example |
I2C1_Remappable_Start(); |
I2Cx_Remappable_Repeated_Start
Prototype |
void I2Cx_Remappable_Repeated_Start(void); |
---|---|
Returns |
Nothing. |
Description |
Issues repeated START signal. ![]()
|
Requires |
I²C must be configured before using this function. See I2Cx_Remappable_Init. |
Example |
I2C1_Remappable_Repeated_Start(); |
I2Cx_Remappable_Is_Idle
Prototype |
unsigned short I2Cx_Remappable_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. See I2Cx_Remappable_Init. |
Example |
if (I2C1_Remappable_Is_Idle()) {...} |
I2Cx_Remappable_Rd
Prototype |
unsigned short I2Cx_Remappable_Rd(unsigned short ack); // for PIC18F K42 and K83 familyunsigned short I2Cx_Remappable_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
![]()
|
||||||
Requires |
I²C must be configured before using this function. See I2Cx_Remappable_Init. Also, START signal needs to be issued in order to use this function. See I2Cx_Remappable_Start (not valid for PIC18F K42 and K83 family). |
||||||
Example |
Read data and send not acknowledge signal: unsigned short take; ... take = I2C1_Remappable_Rd(0); |
I2Cx_Remappable_Wr
Prototype |
unsigned short I2Cx_Remappable_Wr(unsigned short data_); // for PIC18F K42 and K83 familyunsigned short I2Cx_Remappable_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
![]()
|
||||||
Requires |
I²C must be configured before using this function. See I2Cx_Remappable_Init. Also, START signal needs to be issued in order to use this function. See I2Cx_Remappable_Start (not valid for PIC18F K42 and K83 family). |
||||||
Example |
I2C1_Remappable_Wr(0xA3); |
I2Cx_Remappable_Stop
Prototype |
void I2Cx_Remappable_Stop(void); |
---|---|
Returns |
Nothing. |
Description |
Issues STOP signal. ![]()
|
Requires |
I²C must be configured before using this function. See I2Cx_Remappable_Init. |
Example |
I2C1_Remappable_Stop(); |
I2Cx_SetTimeoutCallback
Prototype |
void I2Cx_SetTimeoutCallback(unsigned long timeout, void (*i2c_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_Remappable_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_Remappable_Start, &I2C1_Remappable_Repeated_Start, &I2C1_Remappable_Rd, &I2C1_Remappable_Wr, &I2C1_Remappable_Stop, &I2C1_Remappable_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_Remappable_Start
Prototype |
unsigned short I2C_Remappable_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_Remappable_Init. |
Example |
I2C_Remappable_Start(); |
I2C_Remappable_Repeated_Start
Prototype |
void I2C_Remappable_Repeated_Start(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_Remappable_Init. |
Example |
I2C_Remappable_Repeated_Start(); |
I2C_Remappable_Is_Idle
Prototype |
unsigned short I2C_Remappable_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_Remappable_Init. |
Example |
if (I2C_Remappable_Is_Idle()) {...} |
I2C_Remappable_Rd
Prototype |
unsigned short I2C_Remappable_Rd(unsigned short ack); // for PIC18F K42 and K83 familyunsigned short I2C_Remappable_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_Remappable_Init. Also, START signal needs to be issued in order to use this function. See I2C_Remappable_Start (not valid for PIC18F K42 and K83 family). |
||||||
Example |
Read data and send not acknowledge signal: unsigned short take; ... I2C_Set_Active(&I2C1_Start, &I2C1_Restart, &I2C1_Rd, &I2C1_Wr, &I2C1_Stop, &I2C1_Is_Idle); // Sets the I2C1 module active // Read data and send the not_acknowledge signal take = I2C_Remappable_Rd(0); |
I2C_Remappable_Wr
Prototype |
unsigned short I2C_Remappable_Wr(unsigned short data_); // for PIC18F K42 and K83 familyunsigned short I2Cx_Remappable_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_Remappable_Init. Also, START signal needs to be issued in order to use this function. See I2C_Remappable_Start (not valid for PIC18F K42 and K83 family). |
||||||
Example |
char data_; unsigned int error; ... I2C_Set_Active(&I2C1_Start, &I2C1_Restart, &I2C1_Rd, &I2C1_Wr, &I2C1_Stop, &I2C1_Is_Idle); // Sets the I2C1 module active error = I2C_Wr(data_); error = I2C_Wr(0xA3); |
I2C_Remappable_Stop
Prototype |
void I2C_Remappable_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_Remappable_Init. |
Example |
I2C_Set_Active(&I2C1_Start, &I2C1_Restart, &I2C1_Rd, &I2C1_Wr, &I2C1_Stop, &I2C1_Is_Idle); // Sets the I2C1 module active // Issue STOP signal I2C_Remappable_Stop(); |
Notes |
None. |
What do you think about this topic ? Send us feedback!