I²C Library
The I²C full master I²C module is available with a number of the ARM MCU models. The mikroC PRO for ARM provides a library which supports the master I²C mode.
Important :
- I²C library routines require you to specify the module you want to use. To select the desired I²C module, simply change the letter x in the routine prototype for a number from 0 to 5.
- Number of I²C modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
- Library routine prototypes may vary from family to family, so please pay attention to the desired routine section.
Library Routines
- I2Cx_Init
- I2Cx_Init_Advanced
- I2Cx_Enable
- I2Cx_Disable
- I2Cx_Start
- I2Cx_Get_Status
- I2Cx_Is_Idle
- I2Cx_Master_Slave_Addr_Set
- I2Cx_Master_Bus_Busy
- I2Cx_Master_Busy
- I2Cx_Read
- I2Cx_Write
I2Cx_Init
| Prototype |
void I2Cx_Init(); |
||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Description |
This function configures and initializes the desired I²C module with the standard 100kbps bitrate. |
||||||||||||||||||||||
| Parameters |
None. |
||||||||||||||||||||||
| Returns |
Nothing. |
||||||||||||||||||||||
| Requires |
MCU with the I²C module. |
||||||||||||||||||||||
| Example |
// Initialize the I2C1 module with the standard bitrate of 100000 I2C1_Init(); |
||||||||||||||||||||||
| Notes |
Stellaris
STM32
The peripheral input clock frequency must be at least :
|
I2Cx_Init_Advanced
| Prototype |
// for Stellaris MCUs :
void I2Cx_Init_Advanced(unsigned long bitrate); // for Stellaris MCUs with alternative PORT functions on GPIO pins :void I2Cx_Init_Advanced(unsigned long bitrate, const Module_Struct *module); // for ST MCUs :void I2Cx_Init_Advanced(unsigned long : I2C_ClockSpeed, const Module_Struct *module); |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Description |
This function configures and initializes the desired I²C module using custom bitrate setting. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Parameters |
Stellaris
STM32
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Returns |
Nothing. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Requires |
MCU with the I²C module. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Example |
Stellaris// Initialize the I2C1 module with in fast mode I2C1_Init_Advanced(_I2C_BITRATE_FAST_MODE); // Initialize the I2C1 module with in fast mode on GPIO_PORTJ I2C1_Init_Advanced(_I2C_BITRATE_FAST_MODE, &_GPIO_MODULE_I2C1_J01); STM32// Initialize the I2C1 module with 100Kbps speed on PORTB I2C1_Init_Advanced(100000, &_GPIO_MODULE_I2C1_PB67); |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Notes |
Stellaris
STM32The peripheral input clock frequency must be at least :
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
I2Cx_Enable
| Prototype |
void I2Cx_Enable(); |
|---|---|
| Description |
This routine enables I²C Master block. This routine is valid only for Stellaris devices. |
| Parameters |
None. |
| Returns |
Nothing. |
| Requires |
MCU with at least one I²C module. Used I²C module must be initialized before using this function. See I2Cx_Init and I2Cx_Init_Advanced routines. |
| Example |
// Enable I2C1 Master block I2C1_Enable(); |
| Notes |
|
I2Cx_Disable
| Prototype |
void I2Cx_Disable(); |
|---|---|
| Description |
This routine disables I²C Master block. This routine is valid only for Stellaris devices. |
| Parameters |
None. |
| Returns |
Nothing. |
| Requires |
MCU with at least one I²C module. Used I²C module must be initialized before using this function. See I2Cx_Init and I2Cx_Init_Advanced routines. |
| Example |
// Disable I2C1 Master block I2C1_Disable(); |
| Notes |
|
I2Cx_Start
| Prototype |
void I2Cx_Start(); |
|---|---|
| Description |
Determines if I²C bus is free and issues START signal. This routine is valid only for ST devices. |
| Parameters |
None. |
| Returns |
If there is no error, routine returns 0. |
| Requires |
MCU with at least one I²C module. Used I²C module must be initialized before using this function. See I2Cx_Init and I2Cx_Init_Advanced routines. |
| Example |
// Start I2C1 module I2C1_Start(); |
| Notes |
|
I2Cx_Get_Status
| Prototype |
function I2Cx_Get_Status() : dword; |
|---|---|
| Description |
This routine returns the values of both I²C status registers, SR1 and SR2, combined in one 32-bit value. This routine is valid only for ST devices. |
| Parameters |
None. |
| Returns |
Values of both I²C status registers. |
| Requires |
MCU with at least one I²C module. Used I²C module must be initialized before using this function. See I2Cx_Init and I2Cx_Init_Advanced routines. |
| Example |
// Get Status for I2C1 module unsigned long stat; stat = I2C1_Get_Status(); |
| Notes |
|
I2Cx_Is_Idle
| Prototype |
unsigned I2Cx_Is_Idle(); |
|---|---|
| Description |
Tests if I²C bus is free. This routine is valid only for ST devices. |
| Parameters |
None. |
| Returns |
Returns 1 if I²C bus is free, otherwise returns 0. |
| Requires |
MCU with at least one I²C module. Used I²C module must be initialized before using this function. See I2Cx_Init and I2Cx_Init_Advanced routines. |
| Example |
if (I2C1_Is_Idle() {
...
|
| Notes |
|
I2Cx_Master_Slave_Addr_Set
| Prototype |
void I2Cx_Master_Slave_Addr_Set(char slave_addr, char dir); |
||||||
|---|---|---|---|---|---|---|---|
| Description |
Sets the address that the I²C Master will place on the bus, i.e. Slave address. This routine is valid only for Stellaris devices. |
||||||
| Parameters |
|
||||||
| Returns |
Nothing. |
||||||
| Requires |
MCU with at least one I²C module. Used I²C module must be initialized before using this function. See I2Cx_Init and I2Cx_Init_Advanced routines. |
||||||
| Example |
// Set address which will be used by Master to transmit data to I2C1_Master_Slave_Addr_Set(0x0A, _I2C_DIR_MASTER_TRANSMIT); |
||||||
| Notes |
|
I2Cx_Master_Bus_Busy
| Prototype |
char I2Cx_Master_Bus_Busy(); |
|---|---|
| Description |
Indicates whether or not the I²C bus is busy. This routine is valid only for Stellaris devices. |
| Parameters |
None. |
| Returns |
|
| Requires |
MCU with at least one I²C module. Used I²C module must be initialized before using this function. See I2Cx_Init and I2Cx_Init_Advanced routines. |
| Example |
// If I2C bus is free :
if (!I2C1_Master_Bus_Busy) {
...
}
|
| Notes |
|
I2Cx_Master_Busy
| Prototype |
char I2Cx_Master_Busy(); |
|---|---|
| Description |
Indicates whether or not the I²C Master is busy. This routine is valid only for Stellaris devices. |
| Parameters |
None. |
| Returns |
|
| Requires |
MCU with at least one I²C module. Used I²C module must be initialized before using this function. See I2Cx_Init and I2Cx_Init_Advanced routines. |
| Example |
// If I2C Master is free :
if (!I2C1_Master__Busy) {
...
}
|
| Notes |
|
I2Cx_Read
| Prototype |
// for Stellaris MCUs :
char I2Cx_Read(char *dat, char mode); // for ST MCUs :void I2Cx_Read(char slave_address, char : ^ptrdata, unsigned long count, unsigned long END_mode); |
||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Description |
Receives a byte from the I²C bus. |
||||||||||||||||
| Parameters |
Stellaris
STM32
|
||||||||||||||||
| Returns |
|
||||||||||||||||
| Requires |
|
||||||||||||||||
| Example |
StellarisI2C0_Master_Slave_Addr_Set(0x50, _I2C_DIR_MASTER_RECEIVE); // set slave address, and read operation I2C0_Read(res, _I2C_MASTER_MODE_SINGLE_RECEIVE); // issue I2C start, and read byte. STM32// Example for ST : I2C1_Read(0x50, data_, num_bytes, END_MODE_STOP); // receive num_bytes, store it to data_, and issue a stop condition . |
||||||||||||||||
| Notes |
|
I2Cx_Write
| Prototype |
// for Stellaris MCUs :
unsigned I2Cx_Write(char dat, char mode); // for ST MCUs :unsigned I2Cx_Write(unsigned char slave_address, unsigned char *buf, unsigned long count, unsigned long END_mode) |
||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Description |
Sends data byte via the I²C bus. |
||||||||||||||||
| Parameters |
Stellaris
STM32
|
||||||||||||||||
| Returns |
|
||||||||||||||||
| Requires |
MCU with at least one I²C module. Used I²C module must be initialized before using this function. See I2Cx_Init and I2Cx_Init_Advanced routines. |
||||||||||||||||
| Example |
StellarisI2C0_Master_Slave_Addr_Set(0x50, _I2C_DIR_MASTER_TRANSMIT); // set slave address, and write operation I2C0_Write(rAddr, _I2C_MASTER_MODE_SINGLE_SEND); // issue I2C start, and send byte (address of EEPROM location) STM32I2C1_Write(0x50, data_, num_bytes, END_MODE_STOP); // write num_bytes, stored in data_, and issue a stop condition. |
||||||||||||||||
| Notes |
|
Library Example
This code demonstrates working with the I²C library. Program sends data to EEPROM (data is written at the address 2). After that, program reads data from the same EEPROM address and displays it on GPIO_PORTE for visual check. See the figure below how to interface the 24C02 to ARM.
Stellaris
char data_[256];
//--------------- Writes data to 24C02 EEPROM - signle location
void EEPROM_24C02_WrSingle(unsigned short wAddr, unsigned short wData) {
data_[0] = wAddr;
data_[1] = wData;
I2C1_Start();
// issue I2C start signal
I2C1_Write(0x50,data_,2,END_MODE_STOP);
}//~
//--------------- Reads data from 24C02 EEPROM - single location (random)
unsigned short EEPROM_24C02_RdSingle(unsigned short rAddr) {
data_[0] = rAddr;
I2C1_Start(); // issue I2C start signal
I2C1_Write(0x50,data_,1,END_MODE_RESTART);
I2C1_Read(0x50,data_,1,END_MODE_STOP);
return data_[0];
}
void main(){
unsigned short i;
GPIO_Digital_Output(&GPIOD_BASE, _GPIO_PINMASK_ALL);
I2C1_Init(); // performs I2C initialization
GPIOD_ODR = 0xFFFF;
Delay_ms(1000);
for(i = 0; i<0x80; i++) {
EEPROM_24C02_WrSingle(i,i);
GPIOD_ODR++;
delay_ms(5);
}
Delay_ms(500);
GPIOD_ODR = 0xFFFF;
Delay_ms(1000);
for(i = 0x00; i < 0x80; i++){
GPIOD_ODR = EEPROM_24C02_RdSingle(i);
delay_ms(100);
}
}
STM32
void EEPROM_24C02_Init() {
I2C0_Init();
}
//--------------- Writes data to 24C02 EEPROM - signle location
void EEPROM_24C02_WrSingle(unsigned short wAddr, unsigned short wData) {
I2C0_Master_Slave_Addr_Set(0x50, _I2C_DIR_MASTER_TRANSMIT); // set slave address, and write operation
I2C0_Write(wAddr, _I2C_MASTER_MODE_BURST_SEND_START); // issue I2C start, and send byte (address of EEPROM location)
I2C0_Write(wData, _I2C_MASTER_MODE_BURST_SEND_FINISH); // send data (data to be written), and finish transmit
}
//--------------- Reads data from 24C02 EEPROM - single location (random)
unsigned short EEPROM_24C02_RdSingle(unsigned short rAddr) {
unsigned short reslt;
I2C0_Master_Slave_Addr_Set(0x50, _I2C_DIR_MASTER_TRANSMIT); // set slave address, and write operation
I2C0_Write(rAddr, _I2C_MASTER_MODE_SINGLE_SEND); // issue I2C start, and send byte (address of EEPROM location)
I2C0_Master_Slave_Addr_Set(0x50, _I2C_DIR_MASTER_RECEIVE); // set slave address, and read operation
I2C0_Read(&reslt, _I2C_MASTER_MODE_SINGLE_RECEIVE); // issue I2C start, and read byte.
return reslt;
}
unsigned short i;
char b;
void main(){
GPIO_Digital_Output(&GPIO_PORTD_DATA_BITS, _GPIO_PINMASK_ALL); // Configure GPIO_PORTD as output
GPIO_PORTD_DATA = 0; // Set GPIO_PORTD value to zero
GPIO_Digital_Output(&GPIO_PORTE_DATA_BITS, _GPIO_PINMASK_ALL); // Configure GPIO_PORTE as output
GPIO_PORTE_DATA = 0; // Set GPIO_PORTE value to zero
EEPROM_24C02_Init(); // performs I2C initialization
b = 0x00;
for(i = 0x00; i<0x80; i++) {
EEPROM_24C02_WrSingle(i,b);
b++;
delay_ms(5);
}
for(i = 0x00; i < 0x80; i++){
GPIO_PORTD_DATA = i;
GPIO_PORTE_DATA = EEPROM_24C02_RdSingle(i);
delay_ms(50);
}
}
What do you think about this topic ? Send us feedback!




