I²C Library
The I²C full master I²C module is available with a number of the ARM MCU models. The mikroPascal 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 |
procedure 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 :
procedure I2Cx_Init_Advanced(bitrate : dword); // for Stellaris MCUs with alternative PORT functions on GPIO pins :procedure I2Cx_Init_Advanced(bitrate : dword; module : ^const Module_Struct); // for ST MCUs :procedure I2Cx_Init_Advanced(I2C_ClockSpeed : dword; module : ^const Module_Struct); |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 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 in fast mode I2C1_Init_Advanced(_I2C_BITRATE_FAST_MODE); // Initialize the I2C1 module 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 |
procedure 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 |
procedure 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 |
procedure 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 var stat : dword; stat := I2C1_Get_Status(); |
| Notes |
|
I2Cx_Is_Idle
| Prototype |
function I2Cx_Is_Idle() : word; |
|---|---|
| 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() then ... |
| Notes |
|
I2Cx_Master_Slave_Addr_Set
| Prototype |
procedure I2Cx_Master_Slave_Addr_Set(slave_addr : byte; dir : byte); |
||||||
|---|---|---|---|---|---|---|---|
| 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 |
function I2Cx_Master_Bus_Busy() : byte; |
|---|---|
| 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 = 0) then ... |
| Notes |
|
I2Cx_Master_Busy
| Prototype |
function I2Cx_Master_Busy() : byte; |
|---|---|
| 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 = 0) then ... |
| Notes |
|
I2Cx_Read
| Prototype |
// for Stellaris MCUs :
function I2Cx_Read(var dat : byte; mode : byte) : byte; // for ST MCUs :procedure I2Cx_Read(slave_address : byte; ptrdata : ^byte; count : dword; END_mode: dword); |
||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 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 :
function I2Cx_Write(dat : byte; mode : byte) : byte; // for ST MCUs :function I2Cx_Write(slave_address : byte; ptrdata : ^byte; count : dword; END_mode: dword) : word; |
||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 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
program I2C_Simple;
var i, b : char;
procedure EEPROM_24C02_Init();
begin
I2C0_Init();
end;
//--------------- Writes data to 24C02 EEPROM - signle location
procedure EEPROM_24C02_WrSingle(wAddr : byte; wData : byte);
begin
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
end;
//--------------- Reads data from 24C02 EEPROM - single location (random)
function EEPROM_24C02_RdSingle(rAddr : byte) : byte;
var
res : byte;
begin
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(res, _I2C_MASTER_MODE_SINGLE_RECEIVE); // issue I2C start, and read byte.
result := res;
end;
begin
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 to 0x7F do
begin
EEPROM_24C02_WrSingle(i,b);
Inc(b);
Delay_ms(5); //max vrednost za upis u eeprom
end;
for i := 0x00 to 0x7F do
begin
GPIO_PORTD_DATA := i;
GPIO_PORTE_DATA := EEPROM_24C02_RdSingle(i);
Delay_ms(50);
end;
end.
STM32
program I2C_Simple;
var i: char;
var data_ : array[256] of byte;
//--------------- Writes data to 24C02 EEPROM - signle location
procedure EEPROM_24C02_WrSingle(wAddr : byte; wData : byte);
begin
data_[0] := wAddr;
data_[1] := wData;
I2C1_Start();
// issue I2C start signal
I2C1_Write(0x50,@data_,2,END_MODE_STOP);
end;
//--------------- Reads data from 24C02 EEPROM - single location (random)
function EEPROM_24C02_RdSingle(rAddr : byte) : byte;
begin
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);
result := data_[0];
end;
begin
GPIO_Digital_Output(@GPIOD_BASE, _GPIO_PINMASK_ALL);
I2C1_Init(); // performs I2C initialization
GPIOD_ODR := 0xFFFF;
Delay_ms(1000);
for i := 0x00 to 0x7F do
begin
EEPROM_24C02_WrSingle(i,i);
Inc(GPIOD_ODR);
Delay_ms(5); //max vrednost za upis u eeprom
end;
Delay_ms(500);
GPIOD_ODR := 0xFFFF;
Delay_ms(1000);
for i := 0x00 to 0x7F do
begin
GPIOD_ODR := EEPROM_24C02_RdSingle(i);
Delay_ms(50);
end;
end.
What do you think about this topic ? Send us feedback!




