how does I2C program work?

Thread Starter

Parth786

Joined Jun 19, 2017
642
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
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;
        }
I don't understand read and write function in program. how dose I2C program work ?
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
hi parth,
Read thru this I2C PDF.
E
I have looked this PDF. There is very good information but I am not familiar with PIC controller. I am looking help for two functions read function and write function. There are tow function in program that I posted in first post.
If I want to write to the RTC so what logic should be inside function and If I want to read from the RTC so what logic should be inside function. I am trying understand that two function in programming.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
hi Parth,
Sorry, I do not use the 'C' language for programming, but I have this short tutorial for I2C, hope it helps.

http://www.robot-electronics.co.uk/i2c-tutorial

E
hey Don't say sorry. I feel bad you tried to help me its lot for me.
I have visited the link you attached, also I am looking some information actually I was trying to understand what is logic with programming. which language do you use for programming ?
 

MaxHeadRoom

Joined Jul 18, 2013
30,658
There is also the tutorial #6 by Nigel Goodwin that has the assembly language program version and the C version can be found easily by Google.
Max.
 

ErnieM

Joined Apr 24, 2011
8,415
I2C is a very good protocol, my preferred choice, but there are some devils in it's details. The devil arises since each slave device may present it's needs slightly differently.

Please link to what slave device you need to talk to. Then we can pull out the details for the necessary steps.

The routines are not PIC specific as they "bit bang," or directly control the SCL and SDA pins. Most PICs have special hardware to do this grunt work for you. As this code does not use that special hardware it should work on just about any slow processor. "Slow" because there is noting to slow things down to the maximum speed a slave could accept when the processor is "fast."
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Please link to what slave device you need to talk to. Then we can pull out the details for the necessary steps.
yet I don't have device but I will use ds1307 real time clock with 8051. that's reason I want to understand the I2C protocols.
8051 will send data to ds1307 or receive data from ds1307 via I2C protocols
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
It would have been helpful if the 8051 had been mentioned up front!:(
Max.
sorry for not mentioning 8051.
can you help to understand below function
Code:
/* 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;
        }
I don't understand what is basic work when master send data to slave device ?
 

JohnInTX

Joined Jun 26, 2012
4,787
In the PDF in post #2, page 19 shows a basic I2C write and 20 shows a write to set an address then a read to read data. It doesn't matter if it's a PIC or 8051, the code you show bit bangs the I2C and the rules are the same. I2C is a protocol. That means you have to obey the rules. All transactions start with the Start condition i.e. SDA goes low when SDA is high. They end with a stoP condition. These are described in the PDF. After start, you send the command byte that includes the read/write bit, etc.

Your I2CSend routine is used to send one byte from master to slave and return the ACK or NAK BUT it won't work until you satisfy the rest of the protocol. It would only be the block marked DATA on page 20. As you can see there is more to it than just that one routine.

TheDS1307 datasheet shows the complete I2C transactions needed to communicate with it. You need to read that in detail and flow out how you are going to do the I2C and what DS1307 registers you need to read/write to use it.

If you just want to crash together code that you found in the internet and try to get it working without really understanding it, well.. good luck with all of that. But if you really want to know what I2C is all about so that you can write your own code you should read this from the guys that invented it:
https://www.nxp.com/docs/en/user-guide/UM10204.pdf
 
Last edited:

Thread Starter

Parth786

Joined Jun 19, 2017
642
If you just want to crash together code that you found in the internet and try to get it working without really understanding it, well.. good luck with all of that. But if you really want to know what I2C is all about so that you can write your own code you should read this from the guys that invented it:
https://www.nxp.com/docs/en/user-guide/UM10204.pdf
Thanks. I want to write my own code as per my thinking I2C protocols work in following manner
If i want to write to ds1307 than I have to do this
* Master Transmit/Slave Receive
Code:
start condition
write device address
write register address
send re-start
read device
send nack
send stop
If I want to read from ds1307 than I have to do this
Code:
 start condition
write device address
write register address
send re-start
read device
send nack
send stop
now I am trying to write code for ds1307.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
hi P,
This link is for 8051 to I2C for DS1307, it should be enough to give you an idea of how to proceed,
E
http://www.8051projects.net/wiki/DS1307_I2C_RTC_Interfacing_Tutorial
really good information, I studied code I have question. there is a sample code for reading DS1307
Code:
I2CStart();        /* start condition */
I2CSend(0xD0);    /* Slave address + Write */
I2CSend(0x00);    /* Starting address of RTC */
I2CRestart();    /* Repeated start condition */
I2CSend(0xD1);    /* Slave address + Read */
/* Read 8 registers from RTC */
for (i = 0; i < 8; i++) { 
    a[i] = I2CRead();

    /* check for last byte */
    if(i == 7)
        I2CNak();    /* NAK if last byte */
    else
        I2CAck();    /* ACK for all read bytes */
}
/* Reading finished send stop condition */
I2CStop();
generally I understand what's the loop and array in C. I don't understand why there is loop and array. i mean whats the need of array and loop in that program?.
 

ericgibbs

Joined Jan 29, 2010
21,439
hi,
Usually an array and loop is used to store the register information being Read from the DS1307 into your program, so that after Reading and storing you can process the array data within your program.
By processing the data you normally convert it into a form that can be read by the user.
E
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
hi,
Usually an array and loop is used to store the register information being Read from the DS1307 into your program, so that after Reading and storing you can process the array data within your program.
By processing the data you normally convert it into a form that can be read by the user.
E
thanks. Now how to write complex program. i mean if Ds1307 RTC and LCD connected to 8051. my code is not ready. I have divide my program into small modules
module 1 :
define and set pins for LCD and DS1307 RTC
module 2:
write c code for DS1307 ( make function for ds1307 like as read, write )
Module 3
write c code for LCD (Data , command initialize.. etc)
module 4:
main function ( call the functions RTC and LCD functions when needed)
what do you think. Is it right way to complete big program?
 
Last edited:

ericgibbs

Joined Jan 29, 2010
21,439
hi,
Your method looks OK.
Reference the DS1307, the data being read into the array is in 'packed BCD' , this means your routines must unpack the BCD bytes and convert them into ASCII, which is required by the LCD.
Same packing is required when Writing to the DS1307.
Remember to include a routine that allows you to set the Time and Date into the DS1307, be sure to have the back up battery on the DS1307 module.

As I do not use 'C' I cannot give a programming example.

E
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
hi,
As I do not use 'C' I cannot give a programming example.
E
Which language do you use for programming ?
hi,
Your method looks OK.
E
ok may be someone will point out me. actually I want to make my work more easy.
below question raise in my mind
How many function should be in module 2?
How many function should be in module 3 ?
I think In module 3,need following functions
LCDinitialize ( );
LCD_Delay ( );
LCD_Data ( );
LCD_Command ( );
LCD_string ( );
I have doubt in module 2 I want to divide module in to functions
 

jayanthd

Joined Jul 4, 2015
945
Still need help with the I2C understanding and project. I can help with PIC, AVR, PIC24, dsPIC, PIC32, STM32, Tiva, Stellaris, MSP430 and Arduino.
 
Top