# Need help to understand I2C example code

#### aspirea5745

Joined Apr 17, 2019
99
Hi all,

I am trying to understand a software implementation of the I2C protocol. I have found example code from this website https://www.elprocus.com/i2c-bus-protocol-tutorial-interface-applications/

Programming : start condition

Code:
sbit SDA = P1^7; // initialize the SDA and SCL pins of the microcontroller//
sbit SCL = P1^6;

void delay(unsigned int);

void main ()
{
SDA=1; //processing the data//

SCL=1; //clock is high//
delay();

SDA=0; //sent the data//

delay();

SCL=0; //clock signal is low//
}

Void delay(int p)
{
unsignedint a,b;

For(a=0;a<255;a++); //delay function//

For(b=0;b<p;b++);
}
I don't understand this part of code and comments are not helping much more

Code:
{
SDA=1; //processing the data//

SCL=1; //clock is high//
delay();

SDA=0; //sent the data//

delay();

SCL=0; //clock signal is low//
}
I can understand we are setting the port bits SDA and SCL to either low or high
but I don't know following

When do we need to set and clear (SDA or SCL) and why delay needed ?

#### MrChips

Joined Oct 2, 2009
23,565
Before examining how software works, it is important to understand how the hardware works.

Take a look at how SDA and SCL are related.

Data SDA (sending or receiving) must be synchronized to the correct transition of the clock SCL.
There is always a setup time where SDA is stable before SCL is applied.
Similarly there is hold time before SDA is released.

You set and clear SDA according to the data that is required to be sent.
You set and clear SCL in order to send the correct clocking signals. This mechanism is also known as bit-banging.
The purpose of the delays is to ensure that the specified setup and hold times have been met.

#### aspirea5745

Joined Apr 17, 2019
99
Before examining how software works, it is important to understand how the hardware works.

Take a look at how SDA and SCL are related.
.
@MrChips Thank you for helping.

In a diagram, start is generated when SDA goes High-Low when SCL=High

I can write below code for above statement

Code:
void I2C_start(void)
{
if (SCL == 1)
{
SDA = 1; /* Set SDA */

SDA = 0; /* Clear SDA */
}
}

#### MrChips

Joined Oct 2, 2009
23,565
In any serial communications protocol that requires an external clock signal, there must be a master/slave relationship.
Only the master sends the clock. The slave does not control SCL.

In your situation, who is the master and who is the slave?

#### aspirea5745

Joined Apr 17, 2019
99
In your situation, who is the master and who is the slave?
Suppose Master is microcontroller (8051) and slave is RTC (DS1307)

#### MrChips

Joined Oct 2, 2009
23,565
Suppose Master is microcontroller (8051) and slave is RTC (DS1307)
Ok. Then you don't test for SCL.
Your 8051 MCU code will control SCL and SDA.

Note that you need to initialize both SCL and SDA according to the I2C protocol before initiating data transfer.

btw, I am an embedded systems engineer with over 40 years of professional experience. I do not write code until I understand what needs to be done. I understand the hw first. Then I design a program flowchart. Writing code is the last thing I want to do. At that stage, writing code is the easy part and it usually works correctly because it was written according to specifications, not the other way around.

#### aspirea5745

Joined Apr 17, 2019
99
Ok. Then you don't test for SCL.
Note that you need to initialize both SCL and SDA according to the I2C protocol before initiating data transfer..
I wanted to understand the source code because I don't have idea. I saw some diagram and I thought it's good idea to see the some source code

An IDLE condition is defined as both SCL and SDA high

What should be the sequence of SDA/SCL for initialization

What should be the sequence of SDA/SCL to generate start condition

Last edited:

#### MrChips

Joined Oct 2, 2009
23,565
I wanted to understand the source code because I don't have idea. I saw some diagram and I thought it's good idea to see the some source code

An IDLE condition is defined as both SCL and SDA high

What should be the sequence of SDA/SCL for initialization

What should be the sequence of SDA/SCL to generate start condition
I suggest that you ignore the source code for now.
All the information you seek is in the datasheet of the DS1307.

#### aspirea5745

Joined Apr 17, 2019
99
I am an embedded systems engineer with over 40 years of professional experience. I do not write code until I understand what needs to be done. I understand the hw first. Then I design a program flowchart. Writing code is the last thing I want to do. At that stage, writing code is the easy part and it usually works correctly because it was written according to specifications, not the other way around.
@MrChips This is my basic flow chart to write data to Ds1307 and read data from ds1307

#### MrChips

Joined Oct 2, 2009
23,565
According to the DS1307 datasheet:

Idle condition is SDA = HIGH, SCL = HIGH

START condition is SDA = LOW while SCL = HIGH.

#### aspirea5745

Joined Apr 17, 2019
99
@MrChips you said previously you make flow chart before writing code

I followed your opinion and I have tried my attempt to make flow chart. It would nice to talk about flow chart and later we can come at coding part's

#### MrChips

Joined Oct 2, 2009
23,565
I would not treat the START condition as a separate operation. I would treat it is the start of the byte to be transmitted.

The first test would be to send the <Slave Address> and see if the slave acknowledges.
Do you have an oscilloscope?

#### aspirea5745

Joined Apr 17, 2019
99
The first test would be to send the <Slave Address> and see if the slave acknowledges.
Do you have an oscilloscope?
I don't have oscilloscope even I don't have DS1307

I was assuming for only example
Suppose Master is microcontroller (8051) and slave is RTC (DS1307)
I can understand how difficult to check code without hardware. I am just trying to understand rough idea to implement code for I2C communication

#### BobaMosfet

Joined Jul 1, 2009
1,780
I don't have oscilloscope even I don't have DS1307

I was assuming for only example

I can understand how difficult to check code without hardware. I am just trying to understand rough idea to implement code for I2C communication
Don't try to understand the code first. Go learn how the i2c protcol works- THAT IS MUCH EASIER and lots of documentation exists. Once you understand the protocol, you can then understand how the code works because you know what you're looking for. Trying to reverse engineer someone's logic to understand something is last-resort, when you have no other option.

#### aspirea5745

Joined Apr 17, 2019
99
Don't try to understand the code first. Go learn how the i2c protcol works-
That's what I am doing now I don't have ds1307 yet but I will get it very soon

I do not wanted to waste time so I started to understand I2C protocols and I followed given advice and made flow chart

It would be much appreciated if someone help me to convert flow chart into code

#### BobaMosfet

Joined Jul 1, 2009
1,780
That's what I am doing now I don't have ds1307 yet but I will get it very soon

I do not wanted to waste time so I started to understand I2C protocols and I followed given advice and made flow chart

It would be much appreciated if someone help me to convert flow chart into code
Since you're new to it, write down a flow-chart for non-multi-master i2c. The protcol is easy and well documented. All you need to do is write down is steps for 3 functions, based on the protocol and which MCU you're using.
1. initialize
3. write

That's 3 functions, it's that easy. You'll need to make some defines for flagbits and so forth, but... seriously, you can do this. Here is a PDF you can download that you can review:

http://www.ti.com/lit/an/slva704/slva704.pdf
If you want the document to tell you everything, look on the web for 'UM10204' - that is the i2c specification from NXP (who bought Philips Semiconductor who made i2c). It will give you everything you need for understanding all the bit-patterns you need for ACK/NACK and messaging if you have to do it all yourself.

Last edited:

#### atferrari

Joined Jan 6, 2004
4,331
If you want the document to tell you everything, look on the web for 'UM10204' - that is the i2c specification from NXP (who bought Philips Semiconductor who made i2c). It will give you everything you need for understanding all the bit-patterns you need for ACK/NACK and messaging if you have to do it all yourself.
+1 to that. It is what I did when I got interested in I2C. That would give you a head start.

#### aspirea5745

Joined Apr 17, 2019
99
Since you're new to it, write down a flow-chart for non-multi-master i2c. The protcol is easy and well documented. All you need to do is write down is steps for 3 functions, based on the protocol and which MCU you're using.
@BobaMosfet I have made my flow chart post #9

let's talk regarding the flow chart. Is it logically correct to read the data for I2C communication ?

#### BobaMosfet

Joined Jul 1, 2009
1,780
@BobaMosfet I have made my flow chart post #9

let's talk regarding the flow chart. Is it logically correct to read the data for I2C communication ?
No, the flowchart is not logically correct. Read the links I've provided, they explain it all.

#### aspirea5745

Joined Apr 17, 2019
99