Course contentsShow
← Embedded Systems & Computer Architecture
Lesson 371 of 9365. Communication Protocols and Data TransferFree lesson

Implementing Seven-Bit Device Addressing and Read-Write Flags

Construct address frames that include the target slave device identifier and the direction bit to initiate bus communication.

In this lesson, you will learn how to construct the very first byte transmitted after an I2C "Start" condition to correctly identify your target slave device and specify whether you intend to read from or write to it.

The Anatomy of the Address Byte

In the I2C protocol, every device on the bus is assigned a unique 7-bit identifier. Since an I2C frame is 8 bits long, we have one extra bit to signal our intentions. When a master initiates communication, it sends a single byte known as the Address Frame.

This byte is structured as follows:

  • Bits [7:1]: The 7-bit slave address.
  • Bit [0]: The Read/Write (R/W) flag.

Setting the Direction

The R/W flag is the "mode switch" for the transaction:

  • Write (0): Setting the LSB to 0 tells the slave device that the master will be sending data to the slave following the address.
  • Read (1): Setting the LSB to 1 tells the slave device that it should prepare to transmit data back to the master.

Implementation Logic

To create this byte in your code, you take your slave's 7-bit address, shift it left by one position to make room for the flag, and then perform a bitwise OR operation with your chosen R/W bit.

// Example: Slave address is 0x2A (binary 010 1010)
uint8_t slave_addr = 0x2A;
uint8_t write_op = 0; // Set to 0 for Write
uint8_t read_op = 1;  // Set to 1 for Read

// Constructing the byte
uint8_t address_frame_write = (slave_addr << 1) | write_op; // Result: 0x54
uint8_t address_frame_read  = (slave_addr << 1) | read_op;  // Result: 0x55

By shifting the address, you ensure the 7 bits occupy the upper positions of the byte. Once this byte is transmitted, every device on the bus compares the upper 7 bits to its own internal address. If they match, that specific device will pull the data line low during the following clock pulse to acknowledge (ACK) its presence, effectively "claiming" the bus for the subsequent data transfer.

Key Takeaway: The I2C address frame is an 8-bit value formed by shifting the 7-bit device identifier left by one bit and appending the R/W flag in the least significant bit position.