Inter IC Communication or I2C is a two wire serial communication bus used to connect a variety of external peripheral with a microcontroller. Most common are EEPROMs, RTC, Port Expanders etc. Most leading MCUs comes with at least one dedicated I2C host adaptor built in. But some times we need more than one I2C interface or we need I2C lines on some other i/o pins than those allotted to the hardware I2C, in that case we need to go with a solution called SoftI2C. In SoftI2C the I2C signals are handled in software, the advantage is that any two i/o line of the MCU can be used for communication. The drawback is that it need more CPU cycles are wasted in generating the signal thus less time slice is available to the application.

In this article we will present our open source, flexible and easy to use SoftI2C library. This library can be used to connect any I2C slave device with an AVR (and latter PIC MCU) MCU. Some limitations of the library are :-

  • No Multimaster support.
  • No clock stretching support.

But you can connect multiple number of I2C Slaves on the same bus.

i2c connection

I2C Master/Slave Connection

The Soft I2C Library for AVR

The soft I2C library for AVR comes in two files.

The configuration section lets you choose the I/O lines used for SDA and SCL. You can edit the i2csoft.h file’s I/O Configuration area to do that. The library can be compiled for almost any AVR device like ATmega8,ATmega168,ATmega328 etc.

API Reference.

SoftI2CInit()

Description:
Initializes the Soft I2C Engine.
Must be called before using any other lib functions.

Arguments:
NONE

Returns:
Nothing

 
SoftI2CStart()

Description:
Generates a START(S) condition on the bus.
NOTE: Can also be used for generating repeat start(Sr)
condition too.

Arguments:
NONE

Returns:
Nothing

 
SoftI2CStop()

Description:
Generates a STOP(P) condition on the bus.
NOTE: Can also be used for generating repeat start
condition too.

Arguments:
NONE

Returns:
Nothing

 
SoftI2CWriteByte()

Description:
Sends a Byte to the slave.

Arguments:
8 bit date to send to the slave.

Returns:
non zero if slave acknowledge the data receipt.
zero other wise.

 
SoftI2CReadByte()

Description:
Reads a byte from slave.

Arguments:
1 if you want to acknowledge the receipt to slave.
0 if you don’t want to acknowledge the receipt to slave.

Returns:
The 8 bit data read from the slave.

In next tutorial I will show you how to interface a I2C EEPROM, Temperature Sensor, RTC, Port Expander etc using our Soft I2C library.

Download

Help Us!

We try to publish beginner friendly tutorials for latest subjects in embedded
system as fast as we can. If you like these tutorials and they have helped you
solve problems, please help us in return. You can donate any amount
as you like securely using a Credit or Debit Card or Paypal
.





We would be very thankful for your kind help.

By

Avinash Gupta

Facebook,
Follow on Twitter.

www.AvinashGupta.com

me@avinashgupta.com