Sunday, October 22, 2023

I2C Communication Using PIC Controller and MikroC for PIC

 


I2C Protocol Communication

I2C Protocol:

I2C (Inter-Integrated Circuit) protocol was invented by Philips Semiconductors in 1980s for on board communication between microcontroller and other peripheral chips. It’s a short distance serial communication that required only two lines (like UART Tx and Rx) for bidirectional data. It’s for low speed peripherals including EEPROMs, digital sensors, I2C LCD and temperature sensors.

Communication lines:

I2C protocol uses 2 lines for communication one is SDA (Serial Data) line and SCL (Serial Clock) line. Any master and any slave can be connected through these two signal lines.

SDA: data transfer on this line from master to slave or salve to master and direction of the data controls through Control signal.

SCL: is clock signal line which is use for synchronization for data transfer.

Connection Diagram:

I2C Protocol Communication using PIC Controller

          Pull up resistors with +Vcc connected because SDA and SCL line are open drain drives.

·        I2C Communication start and stop by master device.


I2C Protocol Communication using PIC Controller


Addressing: After the start condition its necessary to specify the slave address. Because the there are several slave peripherals are connected on the same line. Each slave have a unique address.


I2C Protocol Communication using PIC Controller

After starting the I2C first byte is send to the slave is called control byte because first 7 bits make slave address in which first 4 are fixed and last 3 bits (A2, A1, A0) are programmable and last LSB bit is define to read or write the selected slave.

·        LSB = 0 indicates the Master will write the data

·        LSB = 1 indicates the Master will read the data

When the receiver gets its address, it has to generate the Acknowledge signal.

·        Acknowledge = 0 volt on SDA line (SDA line LOW)

·        No Acknowledge = 5 volt on SDA line

 

Data Transfer Speed:

Mode

Speed

Standard mode

100 Kbps

Fast mode

400 Kbps

High Speed

Up to 3.4 Mbps

 

In short story:

1.      Master initiate (start) I2C

2.      Master send the slave address with Read or write condition

3.      If slave acknowledged

4.      Master will read or write to selected slave 

Circuit Diagram: 

Circuit diagram and Simulation is made in Proteus.

I2C Protocol Communication using PIC Controller

Programming in MikroC:

// *** I2C Protocol Communication *** //
// *** By: Elektronics Garage     *** //
// *** Author: Fayyaz Hussain     *** //

unsigned int Rd_data; // store the Read data in this variable
unsigned int address_N; // address modification variable if needed
void EEPROM_Wr(unsigned int address, unsigned int my_data);// EEPROM Write function
unsigned EEPROM_Rd1(unsigned int address); // EEPROM Read function

void main(){
   unsigned int i,j,a=1;     // variable for loop count
  CMCON = 0x07;           // To turn off comparators
  ADCON1 = 0x06;          // To turn off analog to digital converters
  I2C1_Init(100000);         // satndard speed
  TRISB = 0;                 // Configure PORTB as output
  PORTB = 0;

  while (1){
  for(i=0,a=1;i<8;i++)
                  {EEPROM_Wr(i,a);
                  Delay_ms(100);
                  a = a<<1;
                  } 
                  Delay_ms(200);
  for (j=0;j<8;j++)
                  {Rd_data = EEPROM_Rd1(j);
                  PORTB = Rd_data;
                  Delay_ms(1000);
                  }
  }
  }
  
  void EEPROM_Wr(unsigned int address, unsigned int Wr_data)
  {
           // initialize I2C communication
  I2C1_Start();              // issue I2C start signal
  I2C1_Wr(0xA0);             // send byte via I2C  (device address + W)
  address_N = address>>8;    // saving higher order address to address_N
  I2C1_Wr(address_N);        // send byte (address of EEPROM location)
  I2C1_Wr(address);          // send byte (address of EEPROM location)
  I2C1_Wr(Wr_data);          // send data (data to be written)
  I2C1_Stop();               // issue I2C stop signal
  Delay_ms(15);
  }
  //Delay_100ms();
  unsigned EEPROM_Rd1(unsigned int address)
  {
  I2C1_Start();              // issue I2C start signal
  I2C1_Wr(0xA0);             // send byte via I2C  (device address + W)
  address_N = address>>8;    // saving higher order address to address_N
  I2C1_Wr(address_N);        // send byte (address of EEPROM location)
  I2C1_Wr(address);          // send byte (data address)
  I2C1_Repeated_Start();     // issue I2C signal repeated start
  I2C1_Wr(0xA1);             // send byte (device address + R)
  Rd_data = I2C1_Rd(0u);     // Read the data (NO acknowledge)
  I2C1_Stop();               // issue I2C stop signal
}

1 comment:
Write comments
  1. Very informative post ... Thanks for sharing

    ReplyDelete

I am very thankful for your precious time

Important Artical

Popular Articals

Total Page views in This Month

© 2019-20 Electronics Garage. Designed by Uzma 0101 & Distributed by elektronicsgarage1.blogspot.com