Need help to understand I2C example code

Thread Starter

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
30,707
Before examining how software works, it is important to understand how the hardware works.

Take a look at how SDA and SCL are related.

upload_2019-10-20_8-14-58.png


upload_2019-10-20_8-16-4.png


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.
 

Thread Starter

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 */
     }
}
Have I missed your point?
 

MrChips

Joined Oct 2, 2009
30,707
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?
 

MrChips

Joined Oct 2, 2009
30,707
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.
Your code will also read back SDA from the DS1307.

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.
 

Thread Starter

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
30,707
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.
 

Thread Starter

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

1571659103653.png
 

Thread Starter

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
30,707
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?
 

Thread Starter

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
2,110
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.
 

Thread Starter

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
2,110
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
  2. read
  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,764
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.
 

Thread Starter

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 ?
 

Thread Starter

aspirea5745

Joined Apr 17, 2019
99
Read the links I've provided, they explain it all.
I have read it I do not understand point 3.1 Writing to a Slave On The I 2C Bus & figure 8

Master will send a start condition on the bus with the slave's address, as well as the last bit (the R/W bit) set to 0, which signifies a write
Slave address and R/w = 0
After the slave sends the acknowledge bit
What happen when we send wrong address, definitely slave device will not respond

What is condition to check ACK/NACK
 
Top