You are on page 1of 18

BITS Pilani

presentation
BITS Pilani Ramani Kalpathi
Automotive Electronics
Pilani Campus
BITS Pilani
Pilani Campus

AEL ZG512 Embedded Systems


Lecture No. 3
Communication Peripherals
- Synchronous & Asynchronous

• I2C
• Hardware Interface
• Sequence for Write / Read
• Program structure
• Bus Waveforms

BITS Pilani, Pilani Campus


Communication Protocols

I2C Communication
(To transfer data to other devices such as
external EEPROM, I/O devices)
I2C – Inter Integrated Circuit
I2C peripherals present in PIC and LPC23xx devices
Operation at 400Kbps speed (clock rate)
Master mode or slave mode
SDA – Serial data
SCL – Serial Clock
Two pins are selected to be changed from GPIO to I2C mode

BITS Pilani, Pilani Campus


I2C Bus Interface

Controller (LPC2100) is interfaced to other devices (I/O port expanders) in


parallel using SDA pin, SCL pin and GND (called I2C Bus)

BITS Pilani, Pilani Campus


I2C Interface Diagram to EEPROM

Sequence: Write data to EEPROM


START sequence
Device Address (7 bit), R/W bit, ACK
Memory Address 8 bit , ACK bit
Data 8 bit, ACK bit
STOP bit

Address pins connected to logic low or logic high based on Address


pins mentioned in Datasheet. Multiple devices can be connected.
Form the 7-bit address based on Device ID + Address bits
If A0 and A1 are both connected to 0, write the address bits as 00 in
those positions. If A0, A1, A2 are connected to 100 use these bits.

BITS Pilani, Pilani Campus


EEPROM 24C02 Pin functions

BITS Pilani, Pilani Campus


START and STOP Sequence

BITS Pilani, Pilani Campus


Write a data of 0x34 at address 0x12 with
Slave ID 1010000 (7-bits)

START: With SCL high, SDA transitions from


High to Low
STOP: With SCL high, SDA transitions from
LOW to HIGH

When Data is transferred, Data bit can


change only when SCL is LOW
Sequence:
START – Address (7 bit), R/W bit, ACK
Address 8 bit , ACK bit
Data 8 bit, ACK bit
STOP bit

BITS Pilani, Pilani Campus


Special Function Registers

Refer the datasheet for the Special function registers for


the controller used for I2C communication
Compiler will use Library functions to access these
registers

BITS Pilani, Pilani Campus


I2C Transaction

BITS Pilani, Pilani Campus


I2C Start – Stop Waveform

Start Condition: With SCL high, SDA should transition from High to Low

Stop Condition: With SCL high, SDA should transition from Low to High

BITS Pilani, Pilani Campus


Data Transfer after Start

After Start condition, 7-bit address is transferred on SDA on every


rising edge of SCL

The eighth bit is high if it is a Read operation


The eighth bit is low if it is a Write operation
For a write operation, the 9th bit is an acknowledge bit from the slave
Note that the SDA is therefore bi-directional

BITS Pilani, Pilani Campus


Completion of data Transfer

Once data transfer is completed, the Master issues


a STOP condition after the last ACK bit is received

BITS Pilani, Pilani Campus


Typical Data Transfer Sequence
Write data to one address of EEPROM
I2C_Init(100000); // Configure the peripheral for I2C
I2C_Start(); // Issue I2C start signal
I2C_Wr(0xA0); // Send slave address + Write bit for 24cO2 device
I2C_Wr(0x12); // Send byte (address of EEPROM location to write)
I2C_Wr(0x34); // Send data (data to be written)
I2C_Stop();

Configure the I2C peripheral


Issue Start sequence
Send 7 bit Device Slave address + low bit for data write
Send address of device memory where data needs to be written
Send data that needs to be loaded into that particular address in
EEPROM

BITS Pilani, Pilani Campus


Data Transfer - Read sequence

I2C_Init(100000); // Configure the peripheral for I2C


I2C_Start(); // Issue I2C start signal
I2C_Wr(0xA2); // Send byte via I2C (device address + W)
I2C_Wr(2); // Send byte (data address)
I2C_Repeated_Start();//Issue I2C signal repeated start
I2C_Wr(0xA3); // Send byte (device address + R)
value = I2C_Rd(0);// Read the data (NO acknowledge)
I2C_Stop();
Configure the I2C peripheral, Issue Start sequence
Send 7 bit Device Slave address + low bit for data write
Send address of device memory where data needs to be read
Send read sequence and store the data in a variable value

BITS Pilani, Pilani Campus


I2C Clock stretching

If the slave is not ready to accept data it can delay the


clock by pulling the clock line low and stretching
the clock thereby delaying data transfer.
If the master finds the clock line not high the data transfer
is delayed. Master generates the clock for any transfer.

BITS Pilani, Pilani Campus


Questions
1. Draw the interface for two EEPROM devices connected to one
microcontroller using the I2C Bus if the Device1 and Device2 have the
upper address bits as 1010 and the address pins A2,A1 and A0 are
available.
2. Write code in C language (using library functions) to do the following in the
same program:
Write the data 0x34 in memory address 0x21 in Device1 which has an
address 1010000, write the data 0x35 in memory address 0x10 in Device2
which has an address 1010001.
3. Write code in C language to read data from memory address 0x40 from
Device1 and store in variable V1 and read data from 0x41 and store in
variable V2 and read data from address 0x41 and store in variable V3
4. Draw the waveform of SCL and SDA for a Data write operation using I2C bus
into Device1 which has a 7 bit address of 1010001 at the memory address
0xA1. Data to be written is 0xC4.

BITS Pilani, Pilani Campus

You might also like