SSD1306/SSD1315 OLED
0.96 128x64 OLED LCD screen is the most common screen in embedded applications. There are two common pin types for this screen: I2C and SPI. The driver is usually SSD1306, and recently some are SSD1315.
I2C type OLED has only 4 pins, the connection mode cannot be modified, and it is easier to identify
SPI type OLED has 6/7/8 pin types. The most common one is 7 pin, a module with protocol options. The back PCB of this module will have three mode selection instructions. The Rx listed at the back represents the positions where resistors need to be soldered. Usually these resistors are 5KR. The default factory mode is 4-wire SPI.
In SPI mode, the drive mode of SSD1306 and SSD1315 is the same. Because W806 can use hardware SPI, the refresh speed is nearly 10 times faster than I2C mode. The following is the drive mode of 4-wire SPI.
connect
Although it is a 4-wire SPI, all 7 wires need to be connected. The 4 wires do not include VCC, GND and Reset. The connection method for this demonstration is as follows
PB14 -> CS
PB15 -> SCK/CLK/D0
PB17 -> MOSI/SDA/D1
PB10 -> RES (Reset)
PB11 -> DC
GND -> GND
3.3V -> VCC
Library file
This example is improved from the I2C example, adding the SPI transmission method and updating the demonstration function. This library file supports
Draw a dot
Line drawing
Draw a circle
Output text (soft font library required)
Drawing
use
When driving SSD1306/SSD1315, the files that need to be imported are:
ssd1306.c
ssd1306.h
ssd1306_fonts.c
ssd1306_fonts.h
Add the header file to main.c and initialize SPI_HandleTypeDef
#include "ssd1306.h"
SPI_HandleTypeDef hspi;
static void SPI_Init(void)
{
hspi.Instance = SPI;
hspi.Init.Mode = SPI_MODE_MASTER;
hspi.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi.Init.NSS = SPI_NSS_SOFT;
hspi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_40;
hspi.Init.FirstByte = SPI_LITTLEENDIAN;
if (HAL_SPI_Init(&hspi) != HAL_OK)
{
Error_Handler();
}
}
static void GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIO_CLK_ENABLE();
GPIO_InitStruct.Pin = SSD1306_RES_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(SSD1306_RES_PORT, &GPIO_InitStruct);
GPIO_InitStruct.Pin = SSD1306_DC_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(SSD1306_DC_PORT, &GPIO_InitStruct);
}
In ssd1306.h, confirm that the mode is set to SPI
/**
* Mode switch: 0 - SPI, 1 - I2C
*/
#define SSD1306_MODE_I2C 0
Modify the parameters corresponding to the current screen: width, height
/* SSD1306 settings */
/* SSD1306 width in pixels */
#ifndef SSD1306_WIDTH
#define SSD1306_WIDTH 128
#endif
/* SSD1306 LCD height in pixels */
#ifndef SSD1306_HEIGHT
#define SSD1306_HEIGHT 64
#endif
Then you can use it in your code
# Initialization
GPIO_Init();
SPI_Init();
uint8_t res = SSD1306_Init();
printf("OLED init: %dn", res);
# Locate the starting point of the drawing
SSD1306_GotoXY(5, 5);
# Output characters
SSD1306_Puts("OLED:11x18", &Font_11x18, 1);
# Update the display
SSD1306_UpdateScreen(); // display
Clear screen
SSD1306_Fill(0); // clear oled
Line drawing
int y1 = 64, y2 = 0;
while (y1 > 0)
{
SSD1306_DrawLine(0, y1, 128, y2, 1);
SSD1306_UpdateScreen();
y1 -= 2;
y2 += 2;
}
Fill with inverted color, draw a circle
SSD1306_Fill(1); // clear oled
SSD1306_UpdateScreen();
SSD1306_DrawCircle(64, 32, 25, 0);
SSD1306_UpdateScreen();
SSD1306_DrawCircle(128, 32, 25, 0);
SSD1306_UpdateScreen();
SSD1306_DrawCircle(0, 32, 25, 0);
SSD1306_UpdateScreen();
SSD1306_DrawCircle(32, 32, 25, 0);
SSD1306_UpdateScreen();
SSD1306_DrawCircle(96, 32, 25, 0);
SSD1306_UpdateScreen();
Related code
The code has been updated to the SDK's I2C example (supports both I2C and SPI)
Github: wm-sdk-w806/tree/main/demo/i2c/ssd1306_oled
Gitee: wm-sdk-w806/tree/main/demo/i2c/ssd1306_oled
refer to
OLED displays (SSD1306 and SSD1315) http://cholla.mmto.org/electronics/displays/oled/
Previous article:HLK-W806 (Part 9): Software SPI and hardware SPI driver for ST7789V LCD
Next article:Lianshengde HLK-W806 (VII): Compatible with development board LuatOS Air103
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- [National Technology N32WB452 Review] Second issue, lighting and ADC usage in the full version of RT-Thread
- Voltage stabilization
- Please advise, where can I modify the USB device instance path generated by STM32?
- Msp430f149 microcontroller controls stepper motor C language program
- Spectrum Analyzer Principle
- TDA2030 audio amplifier circuit diagram
- dsp reports an error after importing a new project!
- Learn more about logic analyzers
- [NXP Rapid IoT Review] Adding Bluetooth Capabilities with NXP Rapid IoT
- On the saturation problem of operational amplifier circuit