I don't understand how does I2C program work I2C is a serial communication protocol. it is basically two-wire communication protocol.It uses only two wire for the communication. In which one wire is used for the data (SDA) and other wire is used for the clock (SCL).
for example- if microcontroller wants to send information to RTC or microcontroller wants to receive information from RTC than I2C protocol will use to transfer or receive data
Here Is program
I don't understand read and write function in program. how dose I2C program work ?
for example- if microcontroller wants to send information to RTC or microcontroller wants to receive information from RTC than I2C protocol will use to transfer or receive data
Here Is program
Code:
/* Ports Used for I2C Communication */
#define SDA P0_0
#define SCL P0_1
/* Initializing I2C Bus Communication*/
void I2CInit()
{
SDA = 1;
SCL = 1;
}
/* Start Condition for I2C Communication */
void I2CStart()
{
SDA = 0;
SCL = 0;
}
/* ReStart Condition for I2C Communication */
void I2CRestart()
{
SDA = 1;
SCL = 1;
SDA = 0;
SCL = 0;
}
/* Stop Condition For I2C Bus */
void I2CStop()
{
SCL = 0;
SDA = 0;
SCL = 1;
SDA = 1;
}
/* */
void I2CAck()
{
SDA = 0;
SCL = 1;
SCL = 0;
SDA = 1;
}
void I2CNak()
{
SDA = 1;
SCL = 1;
SCL = 0;
SDA = 1;
}
/* Sending Data to slave on I2C bus */
unsigned char I2CSend(unsigned char Data)
{
unsigned char i, ack_bit;
for (i = 0; i < 8; i++) {
if ((Data & 0x80) == 0)
SDA = 0;
else
SDA = 1;
SCL = 1;
SCL = 0;
Data<<=1;
}
SDA = 1;
SCL = 1;
ack_bit = SDA;
SCL = 0;
return ack_bit;
}
unsigned char I2CRead()
{
unsigned char i, Data=0;
for (i = 0; i < 8; i++) {
SCL = 1;
if(SDA)
Data |=1;
if(i<7)
Data<<=1;
SCL = 0;
}
return Data;
}