I²C Library
The I²C full master I²C module is available with a number of the ARM MCU models. The mikroBasic 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 |
sub 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 :
sub procedure I2Cx_Init_Advanced(dim bitrate as longword) ' for Stellaris MCUs with alternative PORT functions on GPIO pins :sub procedure I2Cx_Init_Advanced(dim bitrate as longword, dim module as ^const Module_Struct) ' for ST MCUs :sub procedure I2Cx_Init_Advanced(dim I2C_ClockSpeed as longword, dim module as ^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 |
sub 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 |
sub procedure I2Cx_Restart() |
|---|---|
| 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 |
sub 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 |
sub function I2Cx_Get_Status() as longword |
|---|---|
| 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 dim stat as longword stat = I2C1_Get_Status() |
| Notes |
|
I2Cx_Is_Idle
| Prototype |
sub function I2Cx_Is_Idle() as 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 |
sub procedure I2Cx_Master_Slave_Addr_Set(dim slave_addr as byte, dim dir as 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 |
sub function I2Cx_Master_Bus_Busy() as 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 ... end if |
| Notes |
|
I2Cx_Master_Busy
| Prototype |
sub function I2Cx_Master_Busy() as 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 ... end if |
| Notes |
|
I2Cx_Read
| Prototype |
' for Stellaris MCUs :
sub function I2Cx_Read(dim byref dat as byte, dim mode as byte) as byte ' for ST MCUs :sub procedure I2Cx_Read(dim slave_address as byte, dim ptrdata as ^byte, dim count as longword, dim END_mode as longword) |
||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 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 :
sub function I2Cx_Write(dim dat as byte, dim mode as byte) as byte ' for ST MCUs :sub function I2Cx_Write(dim slave_address as byte, dim ptrdata as ^byte, dim count as longword, dim END_mode as longword) as 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_PORTB for visual check. See the figure below how to interface the 24C02 to ARM.
Stellaris
program I2C_Simple
dim i, b as char
atest as word
sub procedure EEPROM_24C02_Init()
I2C0_Init()
end sub
'--------------- Writes data to 24C02 EEPROM - signle location
sub procedure EEPROM_24C02_WrSingle(dim wAddr as byte, dim wData as byte)
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 sub
'--------------- Reads data from 24C02 EEPROM - single location (random)
sub function EEPROM_24C02_RdSingle(dim rAddr as byte) as byte
dim
res as char
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 sub
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 to 0x7F
EEPROM_24C02_WrSingle(i,b)
Inc(b)
Delay_ms(5)
next i
for i = 0x00 to 0x7F
GPIO_PORTD_DATA = i
GPIO_PORTE_DATA = EEPROM_24C02_RdSingle(i)
Delay_ms(50)
next i
end.
STM32
program I2C_Simple
dim i as char
dim data_ as byte[256]
'--------------- Writes data to 24C02 EEPROM - signle location
sub procedure EEPROM_24C02_WrSingle(dim wAddr as byte, dim wData as byte)
data_[0] = wAddr
data_[1] = wData
I2C1_Start()
' issue I2C start signal
I2C1_Write(0x50,@data_,2,END_MODE_STOP)
end sub
'--------------- Reads data from 24C02 EEPROM - single location (random)
sub function EEPROM_24C02_RdSingle(dim rAddr as byte) as byte
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 sub
main:
GPIO_Digital_Output(@GPIOD_BASE, _GPIO_PINMASK_ALL)
I2C1_Init() ' performs I2C initialization
GPIOD_ODR = 0xFFFF
Delay_ms(1000)
for i = 0x00 to 0x7F
EEPROM_24C02_WrSingle(i,i)
Inc(GPIOD_ODR)
Delay_ms(5)
next i
Delay_ms(500)
GPIOD_ODR = 0xFFFF
Delay_ms(1000)
for i = 0x00 to 0x7F
GPIOD_ODR = EEPROM_24C02_RdSingle(i)
Delay_ms(50)
next i
end.
What do you think about this topic ? Send us feedback!




