Software I²C Library

The mikroC 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.

  Important :

External dependencies of Software I²C Library

Stellaris

The following variables must be defined in all projects using Software I²C Library: Description : Example :
extern sbit Soft_I2C_Scl; Soft I²C Clock line. sbit Soft_I2C_Scl at GPIO_PORTD_DATA3_bit;
extern sbit Soft_I2C_Sda; Soft I²C Data line. sbit Soft_I2C_Sda at GPIO_PORTD_DATA4_bit;
extern sbit Soft_I2C_Scl_Direction; Direction of the Soft I²C Clock pin. sbit Soft_I2C_Scl_Direction at GPIO_PORTD_DIR3_bit;
extern sbit Soft_I2C_Sda_Direction; Direction of the Soft I²C Data pin. sbit Soft_I2C_Sda_Direction at GPIO_PORTD_DIR4_bit;

MSP432

The following variables must be defined in all projects using Software I²C Library: Description : Example :
extern sbit Soft_I2C_Scl_in; Soft I²C Clock Input line. sbit Soft_I2C_Scl_in at DIO_P6IN.B0;
extern sbit Soft_I2C_Scl_out; Soft I²C Clock Output line. sbit Soft_I2C_Scl_out at DIO_P6OUT.B0;
extern sbit Soft_I2C_Sda_in; Soft I²C Data Input line. sbit Soft_I2C_Sda_in at DIO_P6IN.B1;
extern sbit Soft_I2C_Sda_out; Soft I²C Data Output line. sbit Soft_I2C_Sda_out at DIO_P6OUT.B1;
extern sbit Soft_I2C_Scl_Direction; Direction of the Soft I²C Clock pin. sbit Soft_I2C_Scl_Direction at DIO_P6DIR.B0;
extern sbit Soft_I2C_Sda_Direction; Direction of the Soft I²C Data pin. sbit Soft_I2C_Sda_Direction at DIO_P6DIR.B1;

STM32

The following variables must be defined in all projects using Software I²C Library: Description : Example :
extern sbit Soft_I2C_Scl_Output; Soft I²C Clock Output line. sbit Soft_I2C_Scl_Output at GPIOD_ODR.B3;
extern sbit Soft_I2C_Scl_Input; Soft I²C Clock Input line. sbit Soft_I2C_Scl_Input at GPIOD_IDR.B3;
extern sbit Soft_I2C_Sda_Output; Soft I²C Data Output line. sbit Soft_I2C_Sda_Output at GPIOD_ODR.B4;
extern sbit Soft_I2C_Sda_Input; Soft I²C Data Input line. sbit Soft_I2C_Sda_Input at GPIOD_IDR.B3;

CEC1x02

The following variables must be defined in all projects using Software I²C Library: Description : Example :
extern sbit Soft_I2C_Scl_Output; Soft I²C Clock Output line. sbit Soft_I2C_Scl_Output at GPIO_OUTPUT_PIN_015_bit;
extern sbit Soft_I2C_Scl_Input; Soft I²C Clock Input line. sbit Soft_I2C_Scl_Input at GPIO_INPUT_PIN_015_bit;
extern sbit Soft_I2C_Sda_Output; Soft I²C Data Output line. sbit Soft_I2C_Sda_Output at GPIO_OUTPUT_PIN_016_bit;
extern sbit Soft_I2C_Sda_Input; Soft I²C Data Input line. sbit Soft_I2C_Sda_Input at GPIO_INPUT_PIN_016_bit;

Library Routines

Soft_I2C_Init

Prototype

void 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
sbit Soft_I2C_Scl           at GPIO_PORTD_DATA3_bit;
sbit Soft_I2C_Sda           at GPIO_PORTD_DATA4_bit;
sbit Soft_I2C_Scl_Direction at GPIO_PORTD_DIR3_bit;
sbit Soft_I2C_Sda_Direction at GPIO_PORTD_DIR4_bit;
// End Software I2C connections
...
Soft_I2C_Init();

MSP432

// Software I2C connections
sbit Soft_I2C_Scl_in        at DIO_P6IN.B0;
sbit Soft_I2C_Scl_out       at DIO_P6OUT.B0;
sbit Soft_I2C_Sda_in        at DIO_P6IN.B1;
sbit Soft_I2C_Sda_out       at DIO_P6OUT.B1;
sbit Soft_I2C_Scl_Direction at DIO_P6DIR.B0;
sbit Soft_I2C_Sda_Direction at DIO_P6DIR.B1;
// End Software I2C connections
...
Soft_I2C_Init();

STM32

// Software I2C connections
sbit Soft_I2C_Scl_Output    at GPIOD_ODR.B3;
sbit Soft_I2C_Scl_Input     at GPIOD_IDR.B3;
sbit Soft_I2C_Sda_Output    at GPIOD_ODR.B4;
sbit Soft_I2C_Sda_Input     at GPIOD_IDR.B4;
// End Software I2C connections
...
Soft_I2C_Init();

CEC1x02

// Software I2C connections
sbit Soft_I2C_Scl_Output    at GPIO_OUTPUT_PIN_015_bit;
sbit Soft_I2C_Scl_Input     at GPIO_INPUT_PIN_015_bit;
sbit Soft_I2C_Sda_Output    at GPIO_OUTPUT_PIN_016_bit;
sbit Soft_I2C_Sda_Input     at GPIO_INPUT_PIN_016_bit;
// End Software I2C connections
...
Soft_I2C_Init();
Notes

None.

Soft_I2C_Start

Prototype

void 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

unsigned short Soft_I2C_Read(unsigned int ack);

Description

Reads one byte from the slave.

Parameters
  • ack: acknowledge signal parameter. If the ack==0 not acknowledge signal will be sent after reading, otherwise the acknowledge signal will be sent.
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
unsigned short take;
...
// Read data and send the not_acknowledge signal
take = Soft_I2C_Read(0);
Notes

None.

Soft_I2C_Write

Prototype

unsigned short Soft_I2C_Write(unsigned short data_);

Description

Sends data byte via the I²C bus.

Parameters
  • data_: data to be sent
Returns
  • 0 if there were no errors.
  • 1 if write collision was detected on the I²C bus.
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
unsigned short data_, error;
...
error = Soft_I2C_Write(data_);
error = Soft_I2C_Write(0xA3);
Notes

None.

Soft_I2C_Stop

Prototype

void 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

void 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).

Copyright (c) 2002-2019 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code