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:
Pull up resistors with +Vcc
connected because SDA and SCL line are open drain drives.
·
I2C Communication start and
stop by master device.
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.
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.
Programming in MikroC:
01 | // *** I2C Protocol Communication *** // |
02 | // *** By: Elektronics Garage *** // |
03 | // *** Author: Fayyaz Hussain *** // |
05 | unsigned int Rd_data; // store the Read data in this variable |
06 | unsigned int address_N; // address modification variable if needed |
07 | void EEPROM_Wr(unsigned int address, unsigned int my_data);// EEPROM Write function |
08 | unsigned EEPROM_Rd1(unsigned int address); // EEPROM Read function |
11 | unsigned int i,j,a=1; // variable for loop count |
12 | CMCON = 0x07; // To turn off comparators |
13 | ADCON1 = 0x06; // To turn off analog to digital converters |
14 | I2C1_Init(100000); // satndard speed |
15 | TRISB = 0; // Configure PORTB as output |
26 | { Rd_data = EEPROM_Rd1 (j); |
33 | void EEPROM_Wr(unsigned int address, unsigned int Wr_data) |
35 | // initialize I2C communication |
36 | I2C1_Start(); // issue I2C start signal |
37 | I2C1_Wr(0xA0); // send byte via I2C (device address + W) |
38 | address_N = address>>8; // saving higher order address to address_N |
39 | I2C1_Wr(address_N); // send byte (address of EEPROM location) |
40 | I2C1_Wr(address); // send byte (address of EEPROM location) |
41 | I2C1_Wr(Wr_data); // send data (data to be written) |
42 | I2C1_Stop(); // issue I2C stop signal |
46 | unsigned EEPROM_Rd1(unsigned int address) |
48 | I2C1_Start(); // issue I2C start signal |
49 | I2C1_Wr(0xA0); // send byte via I2C (device address + W) |
50 | address_N = address>>8; // saving higher order address to address_N |
51 | I2C1_Wr(address_N); // send byte (address of EEPROM location) |
52 | I2C1_Wr(address); // send byte (data address) |
53 | I2C1_Repeated_Start(); // issue I2C signal repeated start |
54 | I2C1_Wr(0xA1); // send byte (device address + R) |
55 | Rd_data = I2C1_Rd(0u); // Read the data (NO acknowledge) |
56 | I2C1_Stop(); // issue I2C stop signal |
Very informative post ... Thanks for sharing
ReplyDelete