Software I²C Library
The mikroPascal PRO for ARM provides routines for implementing Software I²C communication. These routines are hardware independent and can be used with any MCU. The Software I²C library enables you to use MCU as Master in I²C communication. Multi-master mode is not supported.
- This library implements time-based activities, so interrupts need to be disabled when using Software I²C.
- All Software I²C Library functions are blocking-call functions (they are waiting for I²C clock line to become logical one).
- The pins used for the Software I²C communication should be connected to the pull-up resistors. Turning off the LEDs connected to these pins may also be required.
- Every Software I²C library routine has its own counterpart in Hardware I²C library, except
I2C_Repeated_Start
.Soft_I2C_Start
is used instead ofI2C_Repeated_Start
. - Working clock frequency of the Software I²C is 20kHz.
External dependencies of Software I²C Library
Stellaris
The following variables must be defined in all projects using Software I²C Library: | Description : | Example : |
---|---|---|
var Soft_I2C_Scl : sbit; sfr; external; |
Soft I²C Clock line. | var Soft_I2C_Scl : sbit at GPIO_PORTD_DATA3_bit; |
var Soft_I2C_Sda : sbit; sfr; external; |
Soft I²C Data line. | var Soft_I2C_Sda : sbit at GPIO_PORTD_DATA4_bit; |
var Soft_I2C_Scl_Direction : sbit; sfr; external; |
Direction of the Soft I²C Clock pin. | var Soft_I2C_Scl_Direction : sbit at GPIO_PORTD_DIR3_bit; |
var Soft_I2C_Sda_Direction : sbit; sfr; external; |
Direction of the Soft I²C Data pin. | var Soft_I2C_Sda_Direction : sbit at GPIO_PORTD_DIR4_bit; |
MSP432
The following variables must be defined in all projects using Software I²C Library: | Description : | Example : |
---|---|---|
var Soft_I2C_Scl_in : sbit; sfr; external; |
Soft I²C Clock Output line. | var Soft_I2C_Scl_in : sbit at DIO_P6IN.B0; |
var Soft_I2C_Scl_out : sbit; sfr; external; |
Soft I²C Clock Input line. | var Soft_I2C_Scl_out : sbit at DIO_P6OUT.B0; |
var Soft_I2C_Sda_in : sbit; sfr; external; |
Soft I²C Data Input line. | var Soft_I2C_Sda_in : sbit at DIO_P6IN.B1; |
var Soft_I2C_Sda_out : sbit; sfr; external; |
Soft I²C Data line. | var Soft_I2C_Sda_out : sbit at DIO_P6IN.B1; |
var Soft_I2C_Scl_Direction : sbit; sfr; external; |
Direction of the Soft I²C Clock pin. | var Soft_I2C_Scl_Direction : sbit at DIO_P6DIR.B0; |
var Soft_I2C_Sda_Direction : sbit; sfr; external; |
Direction of the Soft I²C Data pin. | var Soft_I2C_Sda_Direction : sbit at DIO_P6DIR.B1; |
STM32
The following variables must be defined in all projects using Software I²C Library: | Description : | Example : |
---|---|---|
var Soft_I2C_Scl_Output : sbit; sfr; external; |
Soft I²C Clock Output line. | var Soft_I2C_Scl_Output : sbit at GPIOD_ODR.B3; |
var Soft_I2C_Scl_Input : sbit; sfr; external; |
Soft I²C Clock Input line. | var Soft_I2C_Scl_Input : sbit at GPIOD_ODR.B4; |
var Soft_I2C_Sda_Output : sbit; sfr; external; |
Soft I²C Data Output line. | var Soft_I2C_Sda_Output : sbit at GPIOD_IDR.B3; |
var Soft_I2C_Sda_Input : sbit; sfr; external; |
Soft I²C Data Input line. | var Soft_I2C_Sda_Input : sbit at GPIOD_IDR.B4; |
CEC1x02
The following variables must be defined in all projects using Software I²C Library: | Description : | Example : |
---|---|---|
var Soft_I2C_Scl_Output : sbit; sfr; external; |
Soft I²C Clock Output line. | var Soft_I2C_Scl_Output : sbit at GPIO_OUTPUT_PIN_015_bit; |
var Soft_I2C_Scl_Input : sbit; sfr; external; |
Soft I²C Clock Input line. | var Soft_I2C_Scl_Input : sbit at GPIO_INPUT_PIN_015_bit; |
var Soft_I2C_Sda_Output : sbit; sfr; external; |
Soft I²C Data Output line. | var Soft_I2C_Sda_Output : sbit at GPIO_OUTPUT_PIN_016_bit; |
var Soft_I2C_Sda_Input : sbit; sfr; external; |
Soft I²C Data Input line. | var Soft_I2C_Sda_Input : sbit at GPIO_INPUT_PIN_016_bit; |
Library Routines
Soft_I2C_Init
Prototype |
procedure Soft_I2C_Init(); |
---|---|
Description |
Configures the software I²C module. |
Parameters |
None. |
Returns |
Nothing. |
Requires |
External dependencies of the library from the top of the page must be defined before using this function. |
Example |
Stellaris// Software I2C connections var Soft_I2C_Scl : sbit at GPIO_PORTD_DATA3_bit; Soft_I2C_Sda : sbit at GPIO_PORTD_DATA4_bit; Soft_I2C_Scl_Direction : sbit at GPIO_PORTD_DIR3_bit; Soft_I2C_Sda_Direction : sbit at GPIO_PORTD_DIR4_bit; // End Software I2C connections ... Soft_I2C_Init(); MSP432// Software I2C connections var Soft_I2C_Scl_in : sbit at DIO_P6IN.B0; Soft_I2C_Scl_out : sbit at DIO_P6OUT.B0; Soft_I2C_Sda_in : sbit at DIO_P6IN.B1; Soft_I2C_Sda_out : sbit at DIO_P6OUT.B1; Soft_I2C_Scl_Direction : sbit at DIO_P6DIR.B0; Soft_I2C_Sda_Direction : sbit at DIO_P6DIR.B1; // End Software I2C connections ... Soft_I2C_Init(); STM32// Software I2C connections var Soft_I2C_Scl_Output : sbit at GPIOD_ODR.B3; Soft_I2C_Sda_Output : sbit at GPIOD_ODR.B4; Soft_I2C_Scl_Input : sbit at GPIOD_IDR.B3; Soft_I2C_Sda_Input : sbit at GPIOD_IDR.B4; // End Software I2C connections ... Soft_I2C_Init(); CEC1x02// Software I2C connections var Soft_I2C_Scl_Output : sbit at GPIO_OUTPUT_PIN_015_bit; Soft_I2C_Sda_Output : sbit at GPIO_INPUT_PIN_015_bit; Soft_I2C_Scl_Input : sbit at GPIO_OUTPUT_PIN_016_bit; Soft_I2C_Sda_Input : sbit at GPIO_INPUT_PIN_016_bit; // End Software I2C connections ... Soft_I2C_Init(); |
Notes |
None. |
Soft_I2C_Start
Prototype |
procedure Soft_I2C_Start(); |
---|---|
Description |
Determines if the I²C bus is free and issues START signal. |
Parameters |
None. |
Returns |
Nothing. |
Requires |
Software I²C must be configured before using this function. See Soft_I2C_Init routine. |
Example |
// Issue START signal Soft_I2C_Start(); |
Notes |
None. |
Soft_I2C_Read
Prototype |
function Soft_I2C_Read(ack : word) : byte; |
---|---|
Description |
Reads one byte from the slave. |
Parameters |
|
Returns |
One byte from the Slave. |
Requires |
Soft I²C must be configured before using this function. See Soft_I2C_Init routine. Also, START signal needs to be issued in order to use this function. See Soft_I2C_Start routine. |
Example |
var take : byte; ... // Read data and send the not_acknowledge signal take := Soft_I2C_Read(0); |
Notes |
None. |
Soft_I2C_Write
Prototype |
function Soft_I2C_Write(data_ : byte) : byte; |
---|---|
Description |
Sends data byte via the I²C bus. |
Parameters |
|
Returns |
|
Requires |
Soft I²C must be configured before using this function. See Soft_I2C_Init routine. Also, START signal needs to be issued in order to use this function. See Soft_I2C_Start routine. |
Example |
var data_, error : byte; ... error := Soft_I2C_Write(data_); error := Soft_I2C_Write($A3); |
Notes |
None. |
Soft_I2C_Stop
Prototype |
procedure Soft_I2C_Stop(); |
---|---|
Description |
Issues STOP signal. |
Parameters |
None. |
Returns |
Nothing. |
Requires |
Soft I²C must be configured before using this function. See Soft_I2C_Init routine. |
Example |
// Issue STOP signal Soft_I2C_Stop(); |
Notes |
None. |
Soft_I2C_Break
Prototype |
procedure Soft_I2C_Break(); |
---|---|
Description |
All Software I²C Library functions can block the program flow (see note at the top of this page). Calling this routine from interrupt will unblock the program execution. This mechanism is similar to WDT. |
Parameters |
None. |
Returns |
Nothing. |
Requires | Nothing. |
Example |
|
Notes |
Interrupts should be disabled before using Software I²C routines again (see note at the top of this page). |