how does I2C program work?

Discussion in 'Embedded Systems and Microcontrollers' started by Parth786, Jul 22, 2017.

  1. Parth786

    Thread Starter Active Member

    Jun 19, 2017
    642
    45
    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 (Text):
    1.        
    2. /* Ports Used for I2C Communication */
    3.         #define SDA P0_0
    4.         #define SCL P0_1
    5.        
    6.         /* Initializing I2C Bus Communication*/
    7.                 void I2CInit()
    8.         {
    9.             SDA = 1;
    10.             SCL = 1;
    11.         }
    12.      
    13.                 /* Start Condition for I2C Communication */
    14.         void I2CStart()
    15.         {
    16.             SDA = 0;
    17.             SCL = 0;
    18.         }
    19.      
    20.                 /* ReStart Condition for I2C Communication */
    21.         void I2CRestart()
    22.         {
    23.             SDA = 1;
    24.             SCL = 1;
    25.             SDA = 0;
    26.             SCL = 0;
    27.         }
    28.         /* Stop Condition For I2C Bus */
    29.         void I2CStop()
    30.         {
    31.             SCL = 0;
    32.             SDA = 0;
    33.             SCL = 1;
    34.             SDA = 1;
    35.         }
    36.         /* */
    37.         void I2CAck()
    38.         {
    39.             SDA = 0;
    40.             SCL = 1;
    41.             SCL = 0;
    42.             SDA = 1;
    43.         }
    44.        
    45.         void I2CNak()
    46.         {
    47.             SDA = 1;
    48.             SCL = 1;
    49.             SCL = 0;
    50.             SDA = 1;
    51.         }
    52.          /* Sending Data to slave on I2C bus */
    53.         unsigned char I2CSend(unsigned char Data)
    54.         {
    55.              unsigned char i, ack_bit;
    56.              for (i = 0; i < 8; i++) {
    57.                 if ((Data & 0x80) == 0)
    58.                     SDA = 0;
    59.                 else
    60.                     SDA = 1;
    61.                 SCL = 1;
    62.                  SCL = 0;
    63.                 Data<<=1;
    64.              }
    65.              SDA = 1;
    66.              SCL = 1;
    67.              ack_bit = SDA;
    68.              SCL = 0;
    69.              return ack_bit;
    70.         }
    71.        
    72.         unsigned char I2CRead()
    73.         {
    74.             unsigned char i, Data=0;
    75.             for (i = 0; i < 8; i++) {
    76.                 SCL = 1;
    77.                 if(SDA)
    78.                     Data |=1;
    79.                 if(i<7)
    80.                     Data<<=1;
    81.                 SCL = 0;
    82.             }
    83.             return Data;
    84.         }
    I don't understand read and write function in program. how dose I2C program work ?
     
  2. ericgibbs

    Moderator

    Jan 29, 2010
    5,492
    1,023
    hi parth,
    Read thru this I2C PDF.
    E
     
    • i2c.pdf
      File size:
      658.2 KB
      Views:
      19
    Parth786 and JohnInTX like this.
  3. Parth786

    Thread Starter Active Member

    Jun 19, 2017
    642
    45
    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.
     
  4. ericgibbs

    Moderator

    Jan 29, 2010
    5,492
    1,023
  5. Parth786

    Thread Starter Active Member

    Jun 19, 2017
    642
    45
    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 ?
     
  6. MaxHeadRoom

    Expert

    Jul 18, 2013
    15,718
    4,582
    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.
     
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,943
    1,816
    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."
     
  8. Parth786

    Thread Starter Active Member

    Jun 19, 2017
    642
    45
    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
     
  9. MaxHeadRoom

    Expert

    Jul 18, 2013
    15,718
    4,582
    It would have been helpful if the 8051 had been mentioned up front!:(
    Max.
     
    simozz and JohnInTX like this.
  10. Parth786

    Thread Starter Active Member

    Jun 19, 2017
    642
    45
    sorry for not mentioning 8051.
    can you help to understand below function
    Code (Text):
    1. /* Sending Data to slave on I2C bus */
    2.         unsigned char I2CSend(unsigned char Data)
    3.         {
    4.              unsigned char i, ack_bit;
    5.              for (i = 0; i < 8; i++) {
    6.                 if ((Data & 0x80) == 0)
    7.                     SDA = 0;
    8.                 else
    9.                     SDA = 1;
    10.                 SCL = 1;
    11.                  SCL = 0;
    12.                 Data<<=1;
    13.              }
    14.              SDA = 1;
    15.              SCL = 1;
    16.              ack_bit = SDA;
    17.              SCL = 0;
    18.              return ack_bit;
    19.         }
    I don't understand what is basic work when master send data to slave device ?
     
  11. JohnInTX

    Moderator

    Jun 26, 2012
    3,323
    1,667
    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: Jul 23, 2017
    Parth786 likes this.
  12. Parth786

    Thread Starter Active Member

    Jun 19, 2017
    642
    45
    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 (Text):
    1. start condition
    2. write device address
    3. write register address
    4. send re-start
    5. read device
    6. send nack
    7. send stop
    If I want to read from ds1307 than I have to do this
    Code (Text):
    1.  start condition
    2. write device address
    3. write register address
    4. send re-start
    5. read device
    6. send nack
    7. send stop
    now I am trying to write code for ds1307.
     
  13. ericgibbs

    Moderator

    Jan 29, 2010
    5,492
    1,023
  14. Parth786

    Thread Starter Active Member

    Jun 19, 2017
    642
    45
    really good information, I studied code I have question. there is a sample code for reading DS1307
    Code (Text):
    1. I2CStart();        /* start condition */
    2. I2CSend(0xD0);    /* Slave address + Write */
    3. I2CSend(0x00);    /* Starting address of RTC */
    4. I2CRestart();    /* Repeated start condition */
    5. I2CSend(0xD1);    /* Slave address + Read */
    6. /* Read 8 registers from RTC */
    7. for (i = 0; i < 8; i++) {
    8.     a[i] = I2CRead();
    9.  
    10.     /* check for last byte */
    11.     if(i == 7)
    12.         I2CNak();    /* NAK if last byte */
    13.     else
    14.         I2CAck();    /* ACK for all read bytes */
    15. }
    16. /* Reading finished send stop condition */
    17. 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?.
     
  15. ericgibbs

    Moderator

    Jan 29, 2010
    5,492
    1,023
    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
     
    Parth786 likes this.
  16. Parth786

    Thread Starter Active Member

    Jun 19, 2017
    642
    45
    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: Jul 24, 2017
  17. ericgibbs

    Moderator

    Jan 29, 2010
    5,492
    1,023
    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
     
  18. Parth786

    Thread Starter Active Member

    Jun 19, 2017
    642
    45
    Which language do you use for programming ?
    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
     
  19. jayanthd

    Active Member

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