Embedded Lecture 08
Embedded Lecture 08
Part-3
I2C Protocol
1
Serial Communication Interfaces in AVR
I2C Protocol
• Inter-Integrated Circuit (I2C) is a serial communication
protocol crafted to facilitate interaction among
integrated circuits, enabling numerous devices to
communicate through a common bus.
• This protocol is recognized for its straightforwardness
and effectiveness in transmitting data.
• I2C is a synchronous, multi-master, multi-slave,
packet-switched serial communication protocol.
• It was developed by Philips (now NXP) and is widely used for communication between
integrated circuits, sensors, and peripherals in embedded systems.
• Like UART communication, I2C uses only two wires: a bidirectional Serial Data Line
(SDA) for data transfer and a unidirectional Serial Clock Line (SCL) for synchronization.
• Devices on the I2C bus are either masters, which is responsible for generation of clock
signal, initiation and termination of transfer, or slaves, which respond to master
requests.
• Each device on the bus has a unique 7-bit address (10-bit addressing is also supported),
allowing communication with multiple devices on the same bus.
• I2C combines the best features of SPI and UARTs. With I2C, you can connect multiple
slaves to a single master (like SPI) and you can have multiple masters controlling single,
or multiple slaves. Collision detection and arbitration mechanisms resolve conflicts
when multiple masters attempt to communicate simultaneously.
• This is really useful when you want to have more than one microcontroller logging data
2
to a single memory card or displaying text to a single LCD.
• But the most common configuration is a single master and many slaves
Serial Communication Interfaces in AVR
I2C Protocol
I2C supported frequencies
3
Serial Communication Interfaces in AVR
I2C Protocol
I2C Message Format
4
Serial Communication Interfaces in AVR
I2C Protocol
I2C Message Format
5
Serial Communication Interfaces in AVR
I2C Protocol
I2C Message Format
6
Serial Communication Interfaces in AVR
I2C Protocol
Steps of Data Transmission
7
Serial Communication Interfaces in AVR
I2C Protocol
Steps of Data Transmission
8
Serial Communication Interfaces in AVR
I2C Protocol
Steps of Data Transmission
9
Serial Communication Interfaces in AVR
I2C Protocol
I2C Connection Models
10
Serial Communication Interfaces in AVR
I2C Protocol
I2C Connection Models
11
Serial Communication Interfaces in AVR
I2C Protocol
ADVANTAGES AND DISADVANTAGES OF I2C
12
I2C support in AVR
ATmega16/32 I2C Module
13
I2C support in AVR
ATmega16/32 I2C Module
14
I2C support in AVR 15
I2C Registers
TWBR: TWI Bit Rate Register
TWI bit rate register used in generating SCL frequency while operating in
master mode
TWI stands for "Two-Wire Interface." It's essentially another name for the I2C protocol.
The term "TWI" is often used to refer to the same protocol, especially by Atmel (now Microchip
Technology), which developed it for their microcontrollers due to trademark issues with the term "I2C."
I2C support in AVR
I2C Registers
➢ Bit 6 – TWEA: TWI enable acknowledgment bit
• This is TWI acknowledgment enable bit, it is set in receiver mode to generate
acknowledgment and cleared in transmit mode.
➢ Bit 5 – TWSTA: TWI START condition bit
• The master device set this bit to generate START condition by monitoring free bus
status to take control over the TWI bus.
➢ Bit 4 – TWSTO: TWI STOP condition bit
• The master device set this bit to generate STOP condition to leave control over the
TWI bus.
➢ Bit 3 – TWWC: TWI write collision
• This bit gets set when writing to the TWDR register before the current transmission
not complete i.e. TWINT is low.
➢ Bit 2 – TWEN: TWI enable bit
• This bit set to enables the TWI interface in the device and takes control over the
I/O pins.
➢ Bit 1 - Reserved
➢ Bit 0 – TWIE: TWI interrupt enable
• This bit is used to enable TWI to interrupt routine while the I-bit of SREG is set as
long as the TWINT flag is high.
16
I2C support in AVR
I2C Registers
TWSR: TWI Status Register
➢ Bit 7:Bit 3 - TWS7: TWS3: TWI status bits
• TWI status bits shows the status of TWI control and bus
17
I2C support in AVR
I2C Registers
TWDR: TWI Data Register
TWDR contains data to be transmitted or received. It’s not writable while TWI is
in process of shifting a byte. The data remains stable as long as TWINT is set.
19
I2C support in AVR
Basic Configuration Steps of I2C AVR Devices
Master Mode Configuration Slave Mode Configuration
1.Initialize I2C: 1.Initialize I2C:
A. Enable the I2C interface. A. Enable the I2C interface.
B. Set the clock frequency (by B. Set the slave address (by writing to the TWAR register).
setting the prescaler and 2.Enable Address Recognition:
TWBR register). A. Enable the TWGCE bit in the TWAR register if you want to
2.Start Condition: respond to general call addresses.
A. Send a start condition to 3.Enable ACK and TWI:
begin the communication. A. Enable the ACK bit in the TWCR register to acknowledge the
B. Send the address of the address.
slave device (with the R/W B. Enable the TWI (Two-Wire Interface) by setting the TWEN
bit indicating write). bit in the TWCR register.
3.Send Data: 4.Handling Data Transfer:
A. Send data to the slave A. Wait for the address match and read/write command.
device (e.g., register B. If it's a write operation, receive the data from the master.
address or data to write). C. If it's a read operation, send the requested data to the
4.Stop Condition: master.
A. Send a stop condition to 5.Stop or Repeated Start Detection:
end the communication. A. Detect stop or repeated start conditions to determine when
the communication ends or a new transaction starts.
6.Disable TWI:
A. Disable the TWI by clearing the TWEN bit in the TWCR 20
register when communication is complete.
Example
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
// Initialize I2C
void i2c_init() {
// Set TWI bitrate (100 kHz)
TWBR = 32; // ((F_CPU / SCL frequency) - 16) / (2 * prescaler value)
TWSR &= ~(1 << TWPS0); // Prescaler = 1
23