I2C( read as I square C) bus first introduced by Philips. Because of its simplicity and flexibility the I2C bus has become one of the most microcontroller bus system used for interfacing various IC. The I2C bus use only 2 bidirectional data lines for communicating with the microcontroller and the I2C protocol specification can support up to 128(27-1) devices attached to the bus.
The I2C protocol use master and slave method, the master which is usually the microcontroller which the slave can be any I2C devices such as serial EEPROM, I/O Expender or even another microcontroller. As the name I2C use only 2 pin to control up to 127 devices.

The 2 controlling pin are –
SDA: Serial Data pin.
SCL: Serial Clock (Synchronize clock)
Each slave devices have their own individual 7 bits of address length

Bit0 represent R/W, For Read bit0= 1 and Write bit0 = 0.
Let’s look at the bits available in AVR family-


Let’s make the user defined function of start, transmission and stop condition.
uint8_t I2C_Start(void) -> TWI start or repeat start
uint8_t I2C_Start(void)
{
TWCR=(1<<TWEN)|(1<<TWINT)|(1<<TWSTA); //--- TWI Interrupt disable, TWI enable, TWI Start
while(!(TWCR&(1<<TWINT))); //--- Wait until start condition is executed
return(TWSR&0xF8);
}
void I2C_Stop(void) -> TWI stop condition
void I2C_Stop(void)
{
TWCR=(1<<TWSTO)|(1<<TWINT)|(1<<TWEN);//--- TWI Interrupt disable, TWI enable, TWI Stop
while(TWCR&(1<<TWSTO)); //--- Wait until stop condition execution
}
uint8_t I2C_Write(char data) -> TWI write condition
uint8_t I2C_Write(char data)
{
TWDR=data;
TWCR=(1<<TWEN)|(1<<TWINT); //--- TWI Interrupt disable, TWI enable
while(!(TWCR&(1<<TWINT))); //--- Wait until start condition is executed
return(TWSR&0xF8);
}
Read function can be done with Acknowledge or without Acknowledge.
char I2C_Read_Ack(void) -> Read with Acknowledge
char I2C_Read_Ack(void)
{
TWCR=(1<<TWEN)|(1<<TWINT)|(1<<TWEA); //--- TWI Interrupt disable, TWI enable, TWI Accknowledge
while(!(TWCR&(1<<TWINT))); //--- Wait until transmit is complete
return TWDR; //--- Return received data
}
char I2C_Read_Nack(void) -> Read without Acknowledge
char I2C_Read_Nack(void)
{
TWCR=(1<<TWEN)|(1<<TWINT); //--- TWI Interrupt disable, TWI enable
while(!(TWCR&(1<<TWINT))); //--- Wait until transmit is complete
return TWDR; //--- Return received data
}
Those functions are used according to datasheet provided by the IC. We will Interface I2C in the next article.








Visit Today : 54
Total Visit : 28469