OLED Data:Display (I2C)

The tutorial demonstrates the use of hardware I2C to drive OLED to display characters.

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

Use the I2C1 peripheral interface to communicate with the 0.91-inch OLED display.

Product0.91 inch OLED display
Resolution128*32
Control chipSSD1306
Communication methodI2C (IIC)
Working voltage3.3~5V (5V recommended)
I2C interfaceVCC: positive power supply (5V)
GND: power ground
SCL: I2C bus clock signal
SDA: I2C bus data signal

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 transfer

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 OLed.hex file generated by the project compilation is located in the OBJ folder of the OLED project. Find the OLed.hex file corresponding to the project and download the program into the development board using FlyMcu software.

After the program is downloaded successfully: OLED will display oled init success! And OLED Class! The CONTENT.