Site icon Ercan Koçlar

I2C Communication Protocol and MicroC Library

I2C_kapak

I2C_kapak

This post is also available in: Türkçe




1- What is I2C ?

Positive sides  Negative Sides
It is flexible. You can develop as many slaves and master / slave parts as you like within the system.  Address conflicts can occur because part addresses are defined when they are generated.
It makes selection based on address. So you do not need an extra CS (chip select) pin.  Speeds are limited compared to other parallel communication systems.
The connection is simple. Even if you use more than one part, only 2 lines are connected  In some cases, putting too many pull-up resistors can lead to stress on the PCB’s surface area.
There are error detection systems, ACK and NACK. This tells you whether the process is correct or not. (to be described later)
It works with all parts with this protocol regardless of speed.

2- Objective of the Protocol I2C

3- Creating the Required Structure for I2C Protocol

Schematic working system of I2C protocol

4- How Does the I2C Protocol Work?

4A- I2C “START” Contact Start Mark

Sending the I2C protocol START mark

4B- I2C Address and Transaction Mark

In the I2C Protocol, ADDRESS is 7. 8. Bit indicates whether to write (0) or read (1).

4C – I2C “DATA”  Data transfer

In the I2C Protocol, the Data is sent in 8 bits. The next 9th bit contains the ACK / NACK data.

4D- I2C “STOP” Communication Stop Sign

Sending the I2C Protocol STOP mark

4E- I2C Check Mark (ACK – NACK)

A whole communication in the I2C Protocol
Image of I2C Protocol on oscilloscope

I2C Protocol Result


I2C MikroC Library

1- I2C Functions

 I2C_ILETISIM_BASLAT Function

Function : void I2C_ILETISIM_BASLAT()

Purpose : Initiates I2C communication. It is also used in place of restart.

Parameter : No

Usage :

I2C_ILETISIM_BASLAT();// starts I2C communication

Feedback : It does not return any value since it is a void type.

 

– I2C_ILETISIM_DURDUR Function

Function : void I2C_ILETISIM_DURDUR()

Purpose : ends communication

Parameter : no

Usage :

void I2C_ILETISIM_DURDUR()//Ends communication in any case

Feedback : It does not return any value since it is a void type.

 

– I2C_VERI_YAZ Function

Function : unsigned char I2C_VERI_YAZ(unsigned char veri)

Purpose : It sends 8 bits of data to the connected part.

Parameter :

Usage :

I2C_VERI_YAZ(0b01010101)//The register addresses of the slave device in data writing and communication are sent with this function.

Feedback :

 

– I2C_VERI_OKU Function

Function : unsigned char I2C_VERI_OKU(unsigned char sonlandir)

Purpose : It reads 8 bits of data from the connected part.

Parameter :

Usage:

I2C_VERI_OKU(1);// Once reading is done and reading is terminated

Feedback:

 

– I2C_ADRES_GONDER Function

Function : unsigned char I2C_ADRES_GONDER(unsigned char adres, unsigned char islem)

Purpose : It provides the address of the part to be contacted and provides the connection. It also determines whether the operation to be performed is writing or reading.

Parameter:

Usage:

I2C_ADRES_GONDER(0b01101000,0);// A 7-bit address was sent and write was specified.
I2C_ADRES_GONDER(0b01101000,1);// A 7-bit address was sent and write was specified.

Feedback :

 

2- Application of Functions

 

2A- Making Pin Identification

//I2C Pin Tanımlaması
 sbit I2C_SDA at RA5_bit;
 sbit I2C_SCL at RE0_bit;

 sbit I2C_SDA_VERI at LATA5_bit;//27.01.2019 güncellemesi ile eklendi
 
 sbit I2C_SDA_Direction at TRISA5_bit;
 sbit I2C_SCL_Direction at TRISE0_bit;

 

2B- Determination of communication speed

#define I2C_ILETISIM_HIZI Delay_us(10);// communication speed adjustment

 

2C- I2C Case Study

void  main()
{
    unsigned char veri[8];//the array created for writing the read data
    unsigned char txt[5];//created for printing the readout value on the GLCD and conversion operations.
    unsigned i =0;//Approval check was added to the condition.
    ADCON1=13;//PIC in A/D port setting
    CMCON=7;//the comparators were turned off.

    
    SAP1024_INIT (240,128,6);// added to see the results on GLCD.


    I2C_ILETISIM_BASLAT();//commnunication started
    do{i=I2C_ADRES_GONDER(0b01101000,0);}while(i==0);//The address is sent and it is checked whether the confirmation has come.
    I2C_VERI_YAZ(0b00000000);//register seçiliyor//ds1307 register is chosen
    I2C_VERI_YAZ(0b10000000);//writing is done
    I2C_VERI_YAZ(0b00000000);//writing is done
    I2C_VERI_YAZ(0b00000000);//writing is done
    I2C_VERI_YAZ(0b00000000);//writing is done
    I2C_VERI_YAZ(0b00000000);//writing is done
    I2C_VERI_YAZ(0b00000000);//writing is done
    I2C_VERI_YAZ(0b00000000);//writing is done
    I2C_ILETISIM_DURDUR();//communication is stopped
    
    //communication is restarted and ds1307 is activated.
    I2C_ILETISIM_BASLAT();
    I2C_ADRES_GONDER(0b01101000,0);
    I2C_VERI_YAZ(0b00000000);//register is being chosen
    I2C_VERI_YAZ(0b00000000);
    I2C_ILETISIM_DURDUR();


  // In this section, the continuous seconds data from DS1307 is read raw and printed on GLCD.
  while(1)
  
  {
    I2C_ILETISIM_BASLAT();
    I2C_ADRES_GONDER(0b01101000,0);
    I2C_VERI_YAZ(0b00000000);//register is being chosen
    I2C_ILETISIM_DURDUR();
    
    I2C_ILETISIM_BASLAT();
    I2C_ADRES_GONDER(0b01101000,1);
    veri[0]=I2C_VERI_OKU(1);
    I2C_ILETISIM_DURDUR();
    
    ShortToStr(veri[0],txt);
    SAP1024_YAZI_YAZMA(1,1,txt);
    delay_ms(1000);
  }

}

 

Result


Library Files


Documents Taken


 

Exit mobile version