
1. 项目概述:为什么是STM32、SPI与OLED的组合?
在嵌入式开发领域,尤其是单片机应用,人机交互界面是连接硬件逻辑与使用者感知的桥梁。早期笔者可能用几个LED灯闪烁来表示状态,用串口打印来调试信息,但这显然不够直观和友好。OLED表明屏的浮现,以其自发光、高比较度、超薄、低功耗的特性,迅速成为嵌入式表明思路的热门挑选,尤其是0.96寸、1.3寸这类小尺寸模块,售价亲民,接口简单,格外合适作为STM32这类微控制器的“眼睛”。
而STM32作为意法半导体推出的基于ARM Cortex-M内核的32位微控制器家族,以其多元的外设、强大的生态和极高的性价比,几乎成为了嵌入式工程师的“标准答案”。在STM32与OLED之间建立通信,常见的有I2C和SPI两种方式。I2C接口简单,只需两根线(SDA, SCL),但速度相对较慢;SPI则需要四根线(SCLK, MOSI, MISO, CS),但它是全双工、高速的同步通信协议。对于需要频繁刷新表明内容、或者表明复杂图形、动画的情形,SPI协议在速度上的长处就格外显著了。它能确保画面切换流畅,没有拖影,使用者感受更好。
这个项目的核心,就是打通STM32的SPI硬件外设(或者用GPIO模拟)与OLED屏之间的通信链路,并编写驱动代码,最终达成字符、图形乃至简单界面的稳定表明。这不仅是学习SPI通信协议的绝佳实践,也是掌握嵌入式图形表明基础的关键一步。无论你是正在学习STM32的学生,还是需要为机型添加小型表明屏的工程师,这套流程都具有很高的参考价值。
2. 核心硬件与通信协议解析
2.1 OLED表明模块探秘
笔者常用的0.96寸OLED模块,其核心是一块128x64像素的单色表明屏,驱动芯片通常是SSD1306。这块芯片内部集成了显存(GRAM)、升压电路、振荡器等,笔者只需要借助通信接口向它发送命令和数据,它就能自动完成像素点的控制。
模块的引脚通常涵盖:VCC(3.3V/5V)、GND、SCL(时钟)、SDA(数据,在I2C模式下)/DIN(数据输入,在SPI模式下)、RES(复位)、DC(数据/命令挑选)、CS(片选)。在SPI模式下,笔者主要关注四根线:
- SCLK (Serial Clock): 时钟信号,由主机(STM32)产生,用于同步数据位传输。
- MOSI (Master Out Slave In): 主机输出,从机输入。STM32借助这根线向OLED发送数据或命令。
- DC (Data/Command): 这是一个格外关键的引脚。它告诉OLED芯片,当前在MOSI线上传输的是“命令”还是“表明数据”。通常高电平代表数据,低电平代表命令。例如,设置显示屏亮度、扫描方向等是命令;要表明的点阵信息是数据。
- CS (Chip Select): 片选信号,低电平有效。当有多个SPI从设备挂载在同一总线上时,借助拉低对应设备的CS引脚来选中它。即使只有一个设备,规范的驱动也应在传输前后操作CS引脚。
注意: 有些模块标注为
DIN而非MOSI,有些模块的DC引脚可能标注为D/C或A0,原理相同。务必对照你手中模块的数据手册或卖家给出的资料确认引脚定义。
2.2 SPI通信协议深度解读
SPI是一种同步、全双工、主从式的串行通信协议。它的精髓在于“同步”和“主从”。主机控制时钟,从机在时钟边沿采样或输出数据。协议本身很简单,没有复杂的起始位、停止位或地址,但其“灵活性”也带来了一些需要特别注意的规格点。
核心概念与STM32规格:
-
时钟极性 (CPOL) 与时钟相位 (CPHA): 这是SPI规格中最容易出错的地方。它们共同定义了时钟空闲状态和数据采样的时刻。
- CPOL=0: 时钟空闲时为低电平。
- CPOL=1: 时钟空闲时为高电平。
- CPHA=0: 数据在时钟的第一个边沿(即空闲状态后的第一个跳变沿)被采样。
- CPHA=1: 数据在时钟的第二个边沿被采样。
SSD1306 OLED驱动芯片通常工作在Mode 0 (CPOL=0, CPHA=0)或Mode 3 (CPOL=1, CPHA=1)。依据我手头多个模块的实测和常见驱动库的规格,Mode 0是兼容性最广的模式。这意味着:SCLK空闲时为低电平,数据在SCLK的上升沿被采样(对于从机OLED来说,是在上升沿读取MOSI的数据)。
-
数据位顺序 (MSB/LSB First): 即一个字节的数据,是先发送最高位(MSB)还是最低位(LSB)。SSD1306要求MSB First,即先发送字节的最高位(Bit7)。
-
波特率 (Baud Rate): SPI的时钟频率。OLED屏对速度不敏感,STM32的SPI外设速度远高于其需要。通常可以设置为几MHz到十几MHz。过高的速度在某些布线较长的情形下可能导致通信不稳定。初期调试建议先用一个较低的速度,如1MHz (
SPI_BAUDRATEPRESCALER_8或SPI_BAUDRATEPRESCALER_16),稳定后再逐步提高。
硬件SPI vs 软件模拟SPI (GPIO模拟):
- 硬件SPI: 使用STM32内置的SPI外设。优点是效率高,不占用CPU时间开展位操作,通信过程由硬件自动完成,CPU可以处理其他任务。规格相对复杂,需要了解STM32的SPI寄存器或HAL库函数。
- 软件模拟SPI: 将任意几个GPIO引脚定义为SCLK、MOSI等,借助代码控制引脚电平变化来模拟SPI时序。优点是引脚分配灵活,不受硬件SPI引脚限制,程序简单直观。缺点是CPU占用率高,通信速度慢,在表明复杂图形时可能成为瓶颈。
对于新手,我强烈建议从软件模拟SPI开始。它能让你深刻理解SPI的每一位是如何在时间线上产生的,遇到难题时也更容易调试(可以用逻辑分析仪观察每个GPIO的波形)。待完全掌握后,再迁移到硬件SPI以提高表现。
3. 驱动开发与软件外观思路
3.1 驱动层外观:面向对象的封装思想
一个好的驱动代码应该层次清晰,便于移植和维护。笔者可以将驱动分为几个层次:
-
硬件抽象层 (HAL): 这一层直接与STM32的GPIO和SPI外设打交道。它给出最基础的函数,如:
OLED_SPI_Init(): 初始化SPI相关的GPIO和SPI外设(或模拟SPI的GPIO)。OLED_WriteByte(uint8_t byte): 核心函数,向OLED写入一个字节。内部依据DC引脚的状态,决定这个字节是作为命令还是数据发送。OLED_SetDC(uint8_t level): 设置DC引脚电平。OLED_SetCS(uint8_t level): 设置CS引脚电平。OLED_Reset(): 执行硬件复位(操作RES引脚)。
-
命令/数据层: 基于硬件抽象层,封装对SSD1306芯片的操作。
OLED_WriteCommand(uint8_t cmd): 调用OLED_SetDC(0)后,再调用OLED_WriteByte(cmd)发送命令。OLED_WriteData(uint8_t data): 调用OLED_SetDC(1)后,再调用OLED_WriteByte(data)发送数据。- 这一层达成了SSD1306数据手册中定义的所有初始化命令序列,例如开关表明、设置比较度、设置表明起始行、设置内存地址模式等。
-
显存 (GRAM) 操作层: OLED显示屏的每个像素对应SSD1306内部GRAM中的一个位(1亮,0灭)。这块GRAM可以理解为一块单色的位图(bitmap),尺寸为128x64比特,但通常被组织为8页(Page),每页8行(即128列 x 8行 = 128x64像素)。
OLED_SetCursor(uint8_t page, uint8_t col): 设置当前写入的页地址和列地址。OLED_Clear(): 清屏,即向整个GRAM写入0x00。OLED_Refresh(): 将本地维护的一块“表明缓存区”(一个uint8_t buffer[128*8]数组)的内容,一次性全部更新到OLED的GRAM中。这是达成局部刷新、避免闪烁的关键。
-
应用层: 基于显存操作层,达成具体的表明能力。
OLED_DrawPoint(uint8_t x, uint8_t y, uint8_t mode): 在显示屏坐标(x, y)处画点(亮或灭)。OLED_ShowChar(uint8_t x, uint8_t y, char chr, uint8_t size): 在指定位置表明一个ASCII字符,需要用到字库。OLED_ShowString(uint8_t x, uint8_t y, char *str, uint8_t size): 表明字符串。OLED_ShowNum(...),OLED_ShowFloat(...),OLED_DrawLine(...),OLED_DrawRectangle(...)等高级绘图函数。
实操心得: 务必在驱动层达成一个
表明缓存区。不要每次画点、画线都直接借助SPI写入OLED的GRAM。这样做速度极慢,且会导致显示屏闪烁。正确的做法是:所有绘图操作都先修改本地的buffer数组,修改完成后,调用一次OLED_Refresh()函数,将整个buffer借助SPI DMA(如果使用硬件SPI)或快速循环的方式一次性写入OLED。这是提高表明表现和平滑度的核心技巧。
3.2 字库与图形处理
OLED表明字符和中文,本质上是表明一张张小图片(点阵)。笔者需要字库来存储这些点阵数据。
-
ASCII字库: 可以自己定义,也可以使用现成的。通常对于8x16、6x8等常见尺寸的字库,直接以
const数组的形式存储在代码的Flash中。例如:// 8x16 ASCII字模 (纵向取模,字节高位在下) const uint8_t Font8x16[][16] = { {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, // 空格 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x5F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, // ! // ... 其他字符 };OLED_ShowChar函数的工作就是依据字符chr找到对应的字模数组,然后将其绘制到buffer的相应位置。 -
中文字库: 由于汉字数量多,点阵大(通常至少16x16),将所有汉字存储在单片机内部Flash中会占用大量空间。常见的应对思路有:
- 外置SPI Flash芯片存储: 将完整的字库文件(如GB2312编码)烧录到一片W25Qxx系列的SPI Flash中。需要表明时,STM32借助SPI从Flash中读取对应的字模数据。
- 仅存储项目所需汉字: 提取项目中用到的所有汉字,生成一个小的自定义字库数组。这是最节省空间的方法。
- 使用图形化字库工具: 如PCtoLCD2002等,可以方便地生成各种字体、大小的字模代码。
-
图片表明: 同样需要先将图片(BMP、JPG等)在PC上借助工具(如Img2Lcd)转换为单色位图数组,然后将数组数据写入
buffer。对于动态图片或动画,可以预先计算好每一帧的数组,循环刷新即可。
4. 从零开始的完整实操流程
4.1 硬件连接与引脚定义
假设笔者使用STM32F103C8T6(蓝色pill板)和一款4线SPI接口的0.96寸OLED模块(SSD1306驱动)。
连接方式如下表:
| STM32F103C8T6 引脚 | OLED模块引脚 | 能力说明 |
|---|---|---|
| 3.3V | VCC | 电源(注意:有些模块兼容3.3V/5V,有些仅3.3V) |
| GND | GND | 地 |
| PA5 | SCLK/D0 | SPI时钟线 |
| PA7 | MOSI/D1 | SPI数据线(主机输出) |
| PA4 | CS | SPI片选(低电平有效) |
| PA2 | DC | 数据/命令挑选(高:数据, 低:命令) |
| PA1 | RES | 复位(低电平复位,通常初始化时拉低再拉高) |
注意: 这里PA5、PA7、PA4是STM32F103的SPI1默认硬件引脚。如果你使用软件模拟SPI,则可以任意挑选GPIO,连接时只需在代码中修改对应的引脚定义即可,灵活性大大增加。
4.2 软件模拟SPI驱动代码达成
笔者首先用软件模拟SPI来达成最底层的字节写入函数。以STM32 HAL库为例:
// oled_gpio.h - 引脚定义 #define OLED_SPI_GPIO_PORT GPIOA #define OLED_SPI_SCLK_PIN GPIO_PIN_5 #define OLED_SPI_MOSI_PIN GPIO_PIN_7 #define OLED_SPI_CS_PIN GPIO_PIN_4 #define OLED_DC_PIN GPIO_PIN_2 #define OLED_RES_PIN GPIO_PIN_1 // oled_gpio.c - 底层GPIO操作 void OLED_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; __HAL_RCC_GPIOA_CLK_ENABLE(); // 规格SCLK, MOSI, CS, DC, RES为推挽输出模式 GPIO_InitStruct.Pin = OLED_SPI_SCLK_PIN | OLED_SPI_MOSI_PIN | OLED_SPI_CS_PIN | OLED_DC_PIN | OLED_RES_PIN; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; // 模拟SPI,速度不用太高 HAL_GPIO_Init(OLED_SPI_GPIO_PORT, &GPIO_InitStruct); // 初始状态:CS高(不选中), RES高(不复位), DC任意 HAL_GPIO_WritePin(OLED_SPI_GPIO_PORT, OLED_SPI_CS_PIN, GPIO_PIN_SET); HAL_GPIO_WritePin(OLED_SPI_GPIO_PORT, OLED_RES_PIN, GPIO_PIN_SET); } // 模拟SPI写入一个字节 (Mode 0, MSB First) void OLED_WriteByte(uint8_t byte) { uint8_t i; HAL_GPIO_WritePin(OLED_SPI_GPIO_PORT, OLED_SPI_CS_PIN, GPIO_PIN_RESET); // 选中设备 for (i = 0; i < 8; i++) { // 在时钟上升沿之前,设置数据位 (CPHA=0) if (byte & 0x80) // 先判断最高位(MSB) HAL_GPIO_WritePin(OLED_SPI_GPIO_PORT, OLED_SPI_MOSI_PIN, GPIO_PIN_SET); else HAL_GPIO_WritePin(OLED_SPI_GPIO_PORT, OLED_SPI_MOSI_PIN, GPIO_PIN_RESET); // 制造一个时钟上升沿 (CPOL=0, 空闲为低, 上升沿从低到高) HAL_GPIO_WritePin(OLED_SPI_GPIO_PORT, OLED_SPI_SCLK_PIN, GPIO_PIN_SET); // 这里可以加一个微小的延时(__NOP()),确保数据稳定 // __NOP(); __NOP(); HAL_GPIO_WritePin(OLED_SPI_GPIO_PORT, OLED_SPI_SCLK_PIN, GPIO_PIN_RESET); // 时钟拉低,准备下一位 byte <<= 1; // 左移,准备发送下一位 } HAL_GPIO_WritePin(OLED_SPI_GPIO_PORT, OLED_SPI_CS_PIN, GPIO_PIN_SET); // 取消选中 }
4.3 SSD1306初始化与基础表明函数
有了OLED_WriteByte,笔者就可以构建命令和数据写入函数,并完成初始化序列。
// 写命令 void OLED_Write_Cmd(uint8_t cmd) { HAL_GPIO_WritePin(OLED_SPI_GPIO_PORT, OLED_DC_PIN, GPIO_PIN_RESET); // DC=0, 命令 OLED_WriteByte(cmd); } // 写数据 void OLED_Write_Data(uint8_t data) { HAL_GPIO_WritePin(OLED_SPI_GPIO_PORT, OLED_DC_PIN, GPIO_PIN_SET); // DC=1, 数据 OLED_WriteByte(data); } // OLED初始化序列 (基于SSD1306数据手册) void OLED_Init(void) { OLED_GPIO_Init(); // 硬件复位 HAL_GPIO_WritePin(OLED_SPI_GPIO_PORT, OLED_RES_PIN, GPIO_PIN_RESET); HAL_Delay(100); // 复位保持低电平至少3us,这里延时足够 HAL_GPIO_WritePin(OLED_SPI_GPIO_PORT, OLED_RES_PIN, GPIO_PIN_SET); HAL_Delay(100); // 等待复位完成 // 发送初始化命令序列 OLED_Write_Cmd(0xAE); // 关闭表明 OLED_Write_Cmd(0xD5); // 设置表明时钟分频比/振荡器频率 OLED_Write_Cmd(0x80); // 建议值 OLED_Write_Cmd(0xA8); // 设置多路复用率 (MUX Ratio) OLED_Write_Cmd(0x3F); // 64行 - 1 OLED_Write_Cmd(0xD3); // 设置表明偏移 (Display Offset) OLED_Write_Cmd(0x00); // 无偏移 OLED_Write_Cmd(0x40); // 设置表明起始行 (Set Display Start Line) OLED_Write_Cmd(0xA1); // 设置段重映射 (Segment Re-map) 0xA1: 列地址127映射到SEG0 OLED_Write_Cmd(0xC8); // 设置COM扫描方向 (COM Output Scan Direction) 0xC8: 从COM[N-1]到COM0 OLED_Write_Cmd(0xDA); // 设置COM硬件引脚规格 OLED_Write_Cmd(0x12); // 0x12 for 64行, 0x02 for 32行 OLED_Write_Cmd(0x81); // 设置比较度控制 OLED_Write_Cmd(0xCF); // 比较度值, 0x00~0xFF OLED_Write_Cmd(0xA4); // 禁用总体表明启动 (Resume) OLED_Write_Cmd(0xA6); // 设置正常表明 (非反色) OLED_Write_Cmd(0xD9); // 设置预充电周期 OLED_Write_Cmd(0xF1); // 建议值 OLED_Write_Cmd(0xDB); // 设置VCOMH电压倍率 OLED_Write_Cmd(0x40); // 建议值 OLED_Write_Cmd(0xAF); // 启动表明 OLED_Clear(); // 清屏 OLED_Refresh(); // 更新表明 }
4.4 达成表明缓存与刷新机制
如前所述,直接操作GRAM效率低。笔者需要一个表明缓存。
#define OLED_WIDTH 128 #define OLED_HEIGHT 64 #define OLED_PAGE_NUM (OLED_HEIGHT / 8) // 8行一页,共8页 uint8_t OLED_Display_Buffer[OLED_PAGE_NUM][OLED_WIDTH]; // 表明缓存区, [页][列] // 清空缓存区 void OLED_ClearBuffer(void) { for (int page = 0; page < OLED_PAGE_NUM; page++) { for (int col = 0; col < OLED_WIDTH; col++) { OLED_Display_Buffer[page][col] = 0x00; } } } // 将缓存区内容全部刷新到OLED void OLED_Refresh(void) { for (int page = 0; page < OLED_PAGE_NUM; page++) { OLED_Write_Cmd(0xB0 + page); // 设置页地址 (Page Address) OLED_Write_Cmd(0x00); // 设置列地址低位 (Column Address Low) OLED_Write_Cmd(0x10); // 设置列地址高位 (Column Address High) // 发送该页的128列数据 for (int col = 0; col < OLED_WIDTH; col++) { OLED_Write_Data(OLED_Display_Buffer[page][col]); } } } // 在缓存区指定坐标画点 void OLED_DrawPoint_Buffer(uint8_t x, uint8_t y, uint8_t mode) { if (x >= OLED_WIDTH || y >= OLED_HEIGHT) return; // 防止越界 uint8_t page = y / 8; uint8_t bit_pos = y % 8; if (mode) { OLED_Display_Buffer[page][x] |= (1 << bit_pos); // 置1,点亮 } else { OLED_Display_Buffer[page][x] &= ~(1 << bit_pos); // 清0,熄灭 } }
现在,所有绘图函数(如OLED_ShowChar)都改为操作OLED_Display_Buffer。当一帧画面绘制完成后,只需调用一次OLED_Refresh(),即可将整个画面更新到显示屏上,格外高质。
5. 进阶优化与表现提高
5.1 迁移至硬件SPI与DMA
当表明内容复杂、刷新频繁时,软件模拟SPI的CPU开销和速度瓶颈就显现出来了。此时应迁移到硬件SPI。
-
CubeMX规格:
- 在
Connectivity中启用SPI1(或其它SPI)。 - 模式挑选
Full-Duplex Master。 - 硬件NSS信号挑选
Disable(笔者使用软件控制CS引脚)。 - 参数规格:
Baud Rate设为PCLK2 / 8(约9MHz),Data Size为8 Bits,First Bit为MSB First,CPOL为Low,CPHA为1 Edge(即Mode 0)。 - 引脚会自动分配(PA5, PA6, PA7)。笔者只使用PA5(SCK)和PA7(MOSI)。PA6(MISO)可以忽略或设置为GPIO输入。
- 在
-
修改驱动层: 将
OLED_WriteByte函数改为使用HAL库的SPI发送函数。void OLED_WriteByte(uint8_t byte) { HAL_GPIO_WritePin(OLED_SPI_GPIO_PORT, OLED_SPI_CS_PIN, GPIO_PIN_RESET); // 使用HAL_SPI_Transmit, 超时时间可以设短一些,如10ms HAL_SPI_Transmit(&hspi1, &byte, 1, 10); HAL_GPIO_WritePin(OLED_SPI_GPIO_PORT, OLED_SPI_CS_PIN, GPIO_PIN_SET); } -
引入DMA: 更进一步,可以使用DMA来搬运表明缓存区数据,达成“后台”刷新,彻底解放CPU。
- 在CubeMX中为SPI的TX流规格DMA通道(如SPI1_TX -> DMA1 Channel3),模式为
Normal(非循环)。 - 修改
OLED_Refresh函数,将HAL_SPI_Transmit替换为HAL_SPI_Transmit_DMA。 - 需要注意DMA传输完成中断,在一页数据传输完成后,需要设置下一页的地址并再次启动DMA,直到所有页发送完毕。这能达成极高的刷新率。
- 在CubeMX中为SPI的TX流规格DMA通道(如SPI1_TX -> DMA1 Channel3),模式为
5.2 菜单与UI框架外观
当需要表明多个页面或开展交互时,一个简单的状态机菜单框架格外有用。
typedef enum { MENU_MAIN, MENU_SETTINGS, MENU_INFO, // ... 其他菜单项 } Menu_State_t; typedef struct { const char* title; void (*Show)(void); // 该菜单的表明函数 void (*KeyHandler)(uint8_t key); // 该菜单的按键处理函数 } Menu_Item_t; Menu_State_t current_menu = MENU_MAIN; Menu_Item_t menu_list[] = { [MENU_MAIN] = {"Main", OLED_ShowMainMenu, MainMenu_KeyHandler}, [MENU_SETTINGS] = {"Settings", OLED_ShowSettingsMenu, SettingsMenu_KeyHandler}, // ... }; void OLED_MenuTask(void) { menu_list[current_menu].Show(); // 表明当前菜单 } void Key_Scan_Task(uint8_t key) { if (key != KEY_NONE) { menu_list[current_menu].KeyHandler(key); // 将按键事件交给当前菜单处理 } }
在KeyHandler函数中,依据按键(如上下左右、确认、返回)来更新current_menu状态或修改菜单内的参数。Show函数则依据当前状态绘制对应的界面到OLED_Display_Buffer。这样就达成了一个清晰、可扩展的菜单平台。
6. 调试技巧与常见难题排查
6.1 上电无表明或表明异常
这是最常见的难题,可以按照以下步骤排查:
-
电源与连接:
- 测量电压:用万用表确认OLED的VCC引脚电压是否为稳定的3.3V。电压过低可能导致芯片无法工作。
- 检查接线:确保所有杜邦线连接牢固,没有虚焊或接错。特别是GND,一定要共地。
-
初始化序列:
- 确认复位:确保
RES引脚的上电复位时序正确。有些模块内部有上电复位电路,可以不接外部复位,但最好接上并按规范操作。 - 核对命令:逐条核对发送的初始化命令序列,是否与SSD1306数据手册一致。一个常见的错误是
MUX Ratio(0xA8命令)和COM Pins hardware configuration(0xDA命令)设置不匹配(例如64行的屏设置了32行的参数)。
- 确认复位:确保
-
SPI时序与电平:
- 使用逻辑分析仪:这是最强大的调试工具。将探头连接到SCLK, MOSI, DC, CS引脚,抓取上电后的通信波形。
- 检查片选CS: 发送数据前是否拉低,发送后是否拉高?
- 检查DC电平: 发送命令时是否为低,发送数据时是否为高?
- 检查时钟SCLK和数据MOSI: 是否符合Mode 0时序(空闲低,数据在上升沿稳定)?数据位顺序是否是MSB First?时钟频率是否在合理范围(例如低于10MHz)?
- 软件模拟SPI的延时: 如果使用软件模拟,
SCLK高低电平之间的延时(__NOP())可能太短,导致OLED来不及采样。可以适当增加延时(例如循环几次__NOP())再试试。
- 使用逻辑分析仪:这是最强大的调试工具。将探头连接到SCLK, MOSI, DC, CS引脚,抓取上电后的通信波形。
-
显存与坐标平台:
- 清屏测试: 发送清屏命令(0xAE关表明, 0x25/0x26清屏命令,或写0x00到所有GRAM)后,再打开表明(0xAF),看显示屏是否全黑。
- 画点测试: 尝试在显示屏四个角和中心画点,确认坐标平台是否正确。SSD1306的页(Page)和列(Column)地址设置容易混淆。
6.2 表明内容错乱、花屏或残影
- 缓存区未清空或越界: 确保在绘制新一帧前,正确清空了
OLED_Display_Buffer。检查所有绘图函数的坐标边界判断,防止数组越界写入,破坏了其他内存数据。 - 刷新时机不当: 如果在绘制过程中(例如画一个复杂图形画到一半)就调用
OLED_Refresh(),显示屏上会表明不完整的中间状态。确保在一帧完全绘制好后再刷新。 - SPI通信干扰: 如果布线过长或靠近干扰源,高速SPI通信可能出错。可以尝试降低SPI波特率,或者在MOSI、SCLK线上串联一个几十欧姆的电阻。
- 电源噪声: OLED屏在刷新时电流会有变化,如果电源纹波大,可能导致表明异常。在VCC和GND之间并联一个10uF和0.1uF的电容,可以起到很好的滤波成效。
6.3 表现优化难题
- 刷新慢,闪烁:
- 根本原因: 逐点或逐次调用
OLED_Write_Data直接写GRAM。 - 应对思路: 必须使用表明缓存区+一次性刷新机制,如第4.4节所述。这是消除闪烁、提高表现的必由之路。
- 根本原因: 逐点或逐次调用
- CPU占用率高:
- 软件模拟SPI: 这是主要原因。迁移到硬件SPI。
- 刷新整个缓存区耗时: 即使使用硬件SPI,用
HAL_SPI_Transmit循环发送1024字节(128*8)也需要一定时间。此时应使用DMA传输,让硬件在后台搬运数据,CPU可以继续执行其他任务。 - 局部刷新: 如果只有小部分区域内容变化(如一个数字),可以只刷新对应的页和列,而不是整个显示屏。修改
OLED_Refresh函数,使其可以指定刷新区域。
我个人在多个项目中实践下来的体会是,STM32驱动OLED屏是一个“麻雀虽小,五脏俱全”的经典项目。它串联了GPIO操作、通信协议、外设驱动、内存管理、UI框架等多个嵌入式核心知识点。从软件模拟SPI开始,理解每一位时序,再到硬件SPI优化,最后引入DMA和缓存机制应对表现难题,这个学习路径格外扎实。当你成功点亮显示屏,并让字符、图形流畅表明时,那种成就感会让你对嵌入式平台的理解更深一层。最后一个小技巧:把你的OLED_Display_Buffer想象成一块画布,所有的绘图函数都是在这块画布上作画,OLED_Refresh()则是把画布拍下来贴到显示屏上。基于这个模型去外观代码,结构会清晰很多。
本文来源:http://www.hqwc.cn/a/826921.html
若有版权问题,请来信告知:13951431913#139.com 或在右方提交评论。
1. Project overview: why the STM32 + SPI + OLED combination?
In embedded development, particularly in microcontroller applications, the human-machine interface is the bridge connecting hardware logic to the user's perception. Early on I might indicate status with a few blinking LEDs and debug with serial printouts, but that is clearly not intuitive or user-friendly. The arrival of OLED displays, with their self-emission, high contrast, ultra-thin profile and low power consumption, quickly became a popular choice for embedded display designs — especially small modules of 0.96 inches and 1.3 inches, which are affordable, have simple interfaces and are ideal as the 'eyes' for a microcontroller such as the STM32.
The STM32, as STMicroelectronics' family of 32-bit microcontrollers based on the ARM Cortex-M core, with its rich peripheral set, powerful ecosystem and excellent cost-performance ratio, has almost become the 'standard answer' for embedded engineers. Establishing communication between the STM32 and an OLED typically takes one of two forms: I2C and SPI. I2C has a simple interface needing just two wires (SDA, SCL), but it is relatively slow; SPI needs four wires (SCLK, MOSI, MISO, CS), but it is a full-duplex, high-speed synchronous protocol. For scenarios that refresh display content frequently or render complex graphics and animations, SPI's speed advantage is particularly significant. It ensures smooth screen transitions with no smearing, giving a much better user experience.
The core of this project is to establish the communication link between the STM32's SPI hardware peripheral (or GPIO bit-banging) and the OLED panel, and to write the driver code, ultimately achieving stable display of characters, graphics and even simple interfaces. This is both an excellent practice for learning the SPI protocol and a key step to mastering the basics of embedded graphic display. Whether you are an STM32 student or an engineer adding a small display to a product, this workflow is highly valuable as a reference.
2. Core hardware and protocol analysis
2.1 A look inside the OLED display module
The 0.96-inch OLED module I commonly use is built around a 128x64 pixel monochrome display, usually driven by an SSD1306. This chip integrates display RAM (GRAM), a boost converter and oscillators internally — I only need to send commands and data over the communication interface and it handles pixel control automatically.
The module's pins typically include: VCC (3.3V/5V), GND, SCL (clock), SDA (data, in I2C mode) / DIN (data input, in SPI mode), RES (reset), DC (data/command select) and CS (chip select). In SPI mode I mainly care about four wires:
SCLK (Serial Clock): the clock signal, generated by the host (STM32), used to synchronise data bit transfer.
MOSI (Master Out Slave In): host output, slave input. The STM32 uses this line to send data or commands to the OLED.
DC (Data/Command): a particularly critical pin. It tells the OLED chip whether what is arriving on the MOSI line is a 'command' or 'display data'. High typically means data, low means command. For example, setting display contrast or scan direction are commands, while the dot-matrix pixel information to be displayed is data.
CS (Chip Select): the chip select signal, active low. When multiple SPI slave devices sit on the same bus, the corresponding device is selected by pulling its CS pin low. Even with only one device, a proper driver should toggle the CS pin before and after transfers.
Note: some modules label the pin DIN rather than MOSI, and on some modules the DC pin may be labelled D/C or A0 — the principle is the same. Always confirm the pin definitions against the data manual for your module or the seller's documentation.
2.2 A deep look at the SPI protocol
SPI is a synchronous, full-duplex, master-slave serial protocol. Its essence lies in 'synchronous' and 'master-slave': the host controls the clock, and the slave samples or outputs data on clock edges. The protocol itself is simple, with no complex start bits, stop bits or addresses, but that 'flexibility' also brings several points requiring careful attention.
Core concepts and STM32 settings:
Clock polarity (CPOL) and clock phase (CPHA): the easiest place to get things wrong in SPI. Together they define the idle state of the clock and the moment data is sampled.
CPOL=0: the clock idles low.
CPOL=1: the clock idles high.
CPHA=0: data is sampled on the first edge of the clock (that is, the first transition after the idle state).
CPHA=1: data is sampled on the second edge of the clock.
The SSD1306 OLED driver chip usually operates in Mode 0 (CPOL=0, CPHA=0) or Mode 3 (CPOL=1, CPHA=1). Based on my measurements across several modules and the settings in common driver libraries, Mode 0 has the broadest compatibility. This means: SCLK idles low and data is sampled on the SCLK rising edge (for the slave OLED, that means reading the MOSI data on the rising edge).
Bit order (MSB/LSB first): for each byte of data, whether the most significant bit (MSB) or the least significant bit (LSB) is sent first. The SSD1306 requires MSB First, meaning the highest bit (Bit7) of the byte goes out first.
Baud rate: the SPI clock frequency. OLED panels are not sensitive to speed and the STM32's SPI peripheral is far faster than needed. A few MHz to ten-odd MHz is typically fine. A speed that is too high can lead to unstable communication when wiring is long. For early debugging, start with a lower speed such as 1 MHz (SPI_BAUDRATEPRESCALER_8 or SPI_BAUDRATEPRESCALER_16), then raise it gradually once things are stable.
Hardware SPI vs software bit-banged SPI (GPIO emulation):
Hardware SPI: uses the STM32's built-in SPI peripheral. The advantages are high efficiency, no CPU time spent on bit operations, hardware handling the transfer automatically and the CPU free to handle other tasks. The configuration is relatively involved, and you need to understand the STM32's SPI registers or HAL library functions.
Software bit-banged SPI: defines any few GPIO pins as SCLK, MOSI and so on, and controls the pin levels in code to emulate SPI timing. The advantages are flexible pin assignment, freedom from the hardware SPI pin constraints, and simple, intuitive code. The drawbacks are high CPU occupancy, slower communication, and it can become a bottleneck when rendering complex graphics.
For beginners I strongly recommend starting with software bit-banged SPI. It gives you a deep understanding of how each SPI bit arises on the timeline, and makes debugging easier when problems arise (you can use a logic analyser to watch the waveform on each GPIO). Once you have fully mastered it, migrate to hardware SPI for better performance.
3. Driver development and software architecture
3.1 Driver layer architecture: object-oriented encapsulation thinking
Good driver code should be clearly layered, making it easy to port and maintain. I divide the driver into several layers:
Hardware abstraction layer (HAL): this layer deals directly with the STM32's GPIO and SPI peripherals. It provides the most basic functions, such as:
OLED_SPI_Init(): initialises the SPI-related GPIO and the SPI peripheral (or the GPIOs used for emulated SPI).
OLED_WriteByte(uint8_t byte): the core function, writing one byte to the OLED. Internally it checks the DC pin level to decide whether the byte is sent as a command or as data.
OLED_SetDC(uint8_t level): sets the DC pin level.
OLED_SetCS(uint8_t level): sets the CS pin level.
OLED_Reset(): performs a hardware reset (toggling the RES pin).
Command/data layer: built on the HAL, encapsulating operations on the SSD1306 chip.
OLED_WriteCommand(uint8_t cmd): calls OLED_SetDC(0) and then OLED_WriteByte(cmd) to send a command.
OLED_WriteData(uint8_t data): calls OLED_SetDC(1) and then OLED_WriteByte(data) to send data.
This layer implements all the initialisation command sequences defined in the SSD1306 data manual, such as turning the display on and off, setting contrast, setting the display start line, and setting the memory addressing mode.
Display RAM (GRAM) layer: every pixel on the OLED corresponds to one bit in the SSD1306's internal GRAM (1 lit, 0 off). This GRAM can be understood as a monochrome bitmap of 128x64 bits, usually organised into 8 pages of 8 rows each (that is, 128 columns x 8 rows = 128x64 pixels).
OLED_SetCursor(uint8_t page, uint8_t col): sets the current page address and column address for writing.
OLED_Clear(): clears the screen, i.e. writes 0x00 across the entire GRAM.
OLED_Refresh(): pushes the contents of a locally maintained 'display buffer' (a uint8_t buffer[128*8] array) into the OLED's GRAM all at once. This is the key to partial refresh and avoiding flicker.
Application layer: built on the GRAM layer, implementing concrete display capabilities.
OLED_DrawPoint(uint8_t x, uint8_t y, uint8_t mode): plots a point at display coordinates (x, y), lit or unlit.
OLED_ShowChar(uint8_t x, uint8_t y, char chr, uint8_t size): displays one ASCII character at the given position, requiring a font library.
OLED_ShowString(uint8_t x, uint8_t y, char *str, uint8_t size): displays a string.
Higher-level drawing functions such as OLED_ShowNum(...), OLED_ShowFloat(...), OLED_DrawLine(...) and OLED_DrawRectangle(...).
Practical note: be sure to implement a display buffer in the driver layer. Do not write directly into the OLED's GRAM over SPI every time you plot a point or draw a line. That is extremely slow and makes the display flicker. The correct approach is: all drawing operations first modify the local buffer array, and once drawing is complete, call OLED_Refresh() once to push the entire buffer over SPI — via DMA if you are using hardware SPI, or via a fast loop — in a single transfer. This is the core technique for improving display performance and smoothness.
3.2 Font libraries and graphics handling
Displaying characters and Chinese text on an OLED essentially means displaying lots of tiny images (dot matrices). A font library is needed to store this dot-matrix data.
ASCII font: you can define your own or use an existing one. For common sizes such as 8x16 and 6x8, font data is usually stored in Flash as a const array. For example:
The job of the OLED_ShowChar function is to look up the corresponding glyph array for the character chr, then draw it into the corresponding position in the buffer.
Chinese font: because there are many Chinese characters with large dot matrices (typically at least 16x16), storing every character in the microcontroller's internal Flash consumes a lot of space. Common approaches:
External SPI Flash storage: flash a complete font file (such as a GB2312-encoded one) into a W25Qxx-series SPI Flash. When text needs displaying, the STM32 reads the glyph data from Flash over SPI.
Store only the characters the project needs: extract every character used in the project and generate a small custom font array. This is the most space-efficient method.
Use a graphical font tool: tools such as PCtoLCD2002 conveniently generate glyph code for various fonts and sizes.
Image display: images (BMP, JPG and so on) likewise need to be converted on a PC into a monochrome bitmap array with a tool such as Img2Lcd, then written into the buffer. For dynamic images or animations, you can precompute the array for each frame and cycle through them.
4. A complete hands-on workflow from scratch
4.1 Hardware connections and pin definitions
Let's assume I use an STM32F103C8T6 (the blue pill board) and a 4-wire SPI 0.96-inch OLED module (SSD1306 driver).
The connections are as follows:
Note: PA5, PA7 and PA4 are the default SPI1 hardware pins of the STM32F103. If you use software bit-banged SPI you can choose any GPIOs — you just need to change the pin definitions in the code when wiring, which greatly increases flexibility.
4.2 Implementing the software bit-banged SPI driver code
I start by implementing the lowest-level byte-write function with software bit-banged SPI. Using the STM32 HAL library as an example:
4.3 SSD1306 initialisation and basic display functions
With OLED_WriteByte in place, I can build the command and data write functions and complete the initialisation sequence.
4.4 Implementing the display buffer and refresh mechanism
As mentioned above, operating on the GRAM directly is inefficient. I need a display buffer.
Now all drawing functions (such as OLED_ShowChar) operate on OLED_Display_Buffer instead. Once a frame is fully drawn, a single call to OLED_Refresh() pushes the whole image to the display — extremely high quality.
5. Advanced optimisation and performance improvements
5.1 Migrating to hardware SPI and DMA
When display content is complex and refreshes are frequent, the CPU cost and speed bottleneck of software bit-bangled SPI become apparent. At that point you should migrate to hardware SPI.
CubeMX configuration:
Enable SPI1 (or another SPI) under Connectivity.
Set the mode to Full-Duplex Master.
Set hardware NSS signal to Disable (I control the CS pin in software).
Parameter settings: Baud Rate set to PCLK2 / 8 (about 9 MHz), Data Size 8 Bits, First Bit MSB First, CPOL Low, CPHA 1 Edge (i.e. Mode 0).
Pins are assigned automatically (PA5, PA6, PA7). I only use PA5 (SCK) and PA7 (MOSI). PA6 (MISO) can be ignored or set as a GPIO input.
Modify the driver layer: change the OLED_WriteByte function to use the HAL library's SPI send function.
Introduce DMA: going further, you can use DMA to move the display buffer data, achieving 'background' refresh and freeing the CPU completely.
In CubeMX, assign a DMA channel to the SPI TX stream (for example SPI1_TX -> DMA1 Channel3) in Normal (non-circular) mode.
Modify the OLED_Refresh function, replacing HAL_SPI_Transmit with HAL_SPI_Transmit_DMA.
You need to handle the DMA transfer-complete interrupt: after one page of data is sent, set the address for the next page and start the DMA again, repeating until all pages are sent. This achieves a very high refresh rate.
5.2 Menu and UI framework architecture
When you need to display multiple pages or support interaction, a simple state-machine menu framework is extremely useful.
In the KeyHandler function, update the current_menu state or modify parameters within the menu based on the key pressed (up, down, left, right, confirm, back). The Show function then draws the corresponding interface into OLED_Display_Buffer based on the current state. This gives you a clear, extensible menu platform.
6. Debugging techniques and common troubleshooting
6.1 No display on power-up or abnormal display
This is the most common problem, and you can troubleshoot it in the following steps:
Power and connections:
Measure the voltage: use a multimeter to confirm the OLED's VCC is a steady 3.3 V. Too low a voltage can prevent the chip from working.
Check the wiring: make sure all dupont wires are firmly connected with no cold joints or miswiring. In particular, GND must be common.
Initialisation sequence:
Confirm the reset: ensure the power-on reset timing on the RES pin is correct. Some modules have an internal power-on reset circuit and can skip the external reset, but it is best to connect one and operate it properly.
Verify the commands: check the initialisation command sequence line by line against the SSD1306 data manual. A common mistake is a mismatch between the MUX Ratio (0xA8 command) and the COM Pins hardware configuration (0xDA command) — for example, setting 32-row parameters on a 64-row panel.
SPI timing and levels:
Check chip select CS: is it pulled low before sending and pulled high afterwards?
Check the DC level: is it low when sending commands and high when sending data?
Check clock SCLK and data MOSI: do they follow Mode 0 timing (idle low, data stable on the rising edge)? Is the bit order MSB First? Is the clock frequency within a sensible range (for example, below 10 MHz)?
Software bit-banging delay: if you are bit-banging, the delay between SCLK high and low (__NOP()) may be too short for the OLED to sample in time. Try increasing the delay (for example, several __NOP() in a loop).
GRAM and coordinate system:
Clear-screen test: send the display-off command (0xAE), the clear command (0x25/0x26), or write 0x00 to all of the GRAM, then turn the display on (0xAF) and see whether the panel goes all black.
Plot-point test: try plotting points at the four corners and the centre of the display to confirm the coordinate system is correct. The SSD1306's page and column addressing is easy to get confused.
6.2 Garbled, corrupted or ghosted display content
Buffer not cleared or out-of-bounds: make sure OLED_Display_Buffer is properly cleared before drawing a new frame. Check the coordinate bounds checks in all drawing functions to prevent array out-of-bounds writes corrupting other memory.
Bad refresh timing: if you call OLED_Refresh() while drawing is still in progress (for example, halfway through a complex shape), the display will show an incomplete intermediate state. Make sure you refresh only after a frame is fully drawn.
SPI interference: if wiring is long or runs near sources of interference, high-speed SPI communication can produce errors. Try lowering the SPI baud rate, or putting a few tens of ohms of series resistance on the MOSI and SCLK lines.
Power supply noise: current draw changes during a refresh, so significant supply ripple can cause display problems. Placing a 10uF and a 0.1uF capacitor in parallel between VCC and GND does an excellent job of filtering.
6.3 Performance optimisation problems
Root cause: calling OLED_Write_Data pixel by pixel or line by line to write the GRAM directly.
Approach: you must use the display buffer plus single-shot refresh mechanism described in section 4.4. This is the only way to eliminate flicker and improve performance.
Software bit-banged SPI: this is the main cause. Migrate to hardware SPI.
Time to refresh the entire buffer: even with hardware SPI, looping HAL_SPI_Transmit to send 1024 bytes (128*8) takes time. This is where you should use DMA transfer so the hardware moves data in the background while the CPU continues other tasks.
Partial refresh: if only a small region has changed (a digit, say), you can refresh just the corresponding page and column rather than the whole display. Modify the OLED_Refresh function so the refresh region can be specified.
From my own experience across several projects, driving an OLED from an STM32 is a classic 'small but complete' project. It ties together GPIO manipulation, communication protocols, peripheral drivers, memory management and UI frameworks — many core embedded knowledge points. Starting with software bit-banged SPI to understand each bit's timing, then optimising with hardware SPI, and finally bringing in DMA and buffering to tackle performance — this learning path is exceptionally solid. When you finally light up the display and get characters and graphics flowing smoothly, the sense of achievement gives you a deeper understanding of embedded systems. One last trick: imagine your OLED_Display_Buffer as a canvas, with every drawing function painting on that canvas, and OLED_Refresh() as photographing the canvas and sticking it onto the display. Designing your code around this model makes the structure far clearer.