I2C Communication(I2C)-OLED

The tutorial demonstrates the hardware I2C driving 0.91-inch OLED display to display content.

Hardware connection

image-20240825203539408

Just install it to the corresponding interface:

PeripheralsDevelopment BoardDescription
OLED: VCC5VOLED power supply
OLED: GNDGNDOLED common ground
OLED: SCLPB8Serial clock line (SCL)
OLED: SDAPB9Serial data line (SDA)

Control principle

The I2C (Inter-Integrated Circuit) bus is a serial communication protocol consisting of a serial data line (SDA) and a serial clock line (SCL).

Serial Data Line (SDA): used to transmit data

Serial Clock Line (SCL): used to synchronize data transmission

Multi-device communication: The I2C interface uses an address-based device identification mechanism to select a specific device to communicate with.

IIC bus timing diagram

image-20231024180945251

Idle state

SCL high level, SDA high level;

Start condition

SCL high level, SDA falling edge;

Stop condition

SCL high level, SDA rising edge;

Data transmission

SCL low level, SDA rising or falling edge;

Response signal

After receiving data from the device, it will send a low level to the master device to indicate successful reception;

Data validity

When the clock line is high, the data line must remain stable; when the clock line is low, the data line is allowed to change.

Software configuration

Pin definition

Main control chipPinMain function (after reset)Default multiplexing functionRedefine function
STM32F103RCT6PB8PB8TIM4_CH3I2C1_SCL/CAN_RX
STM32F103RCT6PB9PB9TIM4_CH4I2C1_SDA/CAN_TX

Software code

Since the default function of the pin is the ordinary IO pin function, we need to use the redefine function.

Control function

The tutorial only briefly introduces the code, you can open the project source code to read the details.

OLED_I2C_Init

OLED_Draw_String

OLED_Draw_Line

Experimental phenomenon

The I2c.hex file generated by the project compilation is located in the OBJ folder of the I2C project. Find the corresponding I2c.hex file of the project and use FlyMcu software to download the program into the development board.

After the program is downloaded successfully: OLED will display oled init success! And Hello wrold content.