本文基于STM32硬件I2C驅動SSD1306 OLED屏幕,提供完整的代碼實現及關鍵注意事項,適用于128x32或128x64分辨率屏幕。代碼通過模塊化設計,支持顯示字符、數字、漢字及位圖,并優化了顯存刷新機制。
零、完整代碼
完整代碼:
1)oled.c
/*** @file oled.c* @brief OLED顯示驅動層,支持SSD1306控制器* @details 提供OLED初始化、基本繪圖、字符顯示、顯存刷新等功能* @note 本驅動針對128x32分辨率和128x64OLED屏適配,需要修改106-110行的啟動參數*/#include "oledfont.h"
#include "string.h"
#include "oled.h"
#include "stdlib.h"
#include "main.h"extern I2C_HandleTypeDef hi2c1; ///< I2C外設句柄/*** @brief OLED繪制位圖* @param x0 起始列坐標(0~127)* @param y0 起始頁號(0~3)* @param x1 結束列坐標(需大于x0)* @param y1 結束頁號(需大于y0)* @param BMP 位圖數據數組指針* @note 頁號與屏幕行的對應關系:每頁8行,128x32屏共4頁*/
void OLED_DrawBMP(unsigned char x0, unsigned char y0, unsigned char x1, unsigned char y1, unsigned char BMP[])
{unsigned int j = 0;unsigned char x, y;if (y1 % 8 == 0)y = y1 / 8;elsey = y1 / 8 + 1;for (y = y0; y < y1; y++){OLED_Set_Pos(x0, y);for (x = x0; x < x1; x++){OLED_WR_Byte(BMP[j++], OLED_DATA);}}
}// OLED的顯存
// 存放格式如下.
//[0]0 1 2 3 ... 127
//[1]0 1 2 3 ... 127
//[2]0 1 2 3 ... 127
//[3]0 1 2 3 ... 127
//[4]0 1 2 3 ... 127
//[5]0 1 2 3 ... 127
//[6]0 1 2 3 ... 127
//[7]0 1 2 3 ... 127
// (128x64顯存,8頁)
uint8_t OLED_GRAM[128][8];/*** @brief 刷新顯存到OLED物理屏幕* @details 將GRAM數組內容通過I2C寫入OLED的GDDRAM*/
void OLED_Refresh_Gram(void)
{uint8_t i, n;for (i = 0; i < 4; i++){ // 僅刷新前4頁(原為8)OLED_WR_Byte(0xb0 + i, OLED_CMD);OLED_WR_Byte(0x00, OLED_CMD);OLED_WR_Byte(0x10, OLED_CMD);for (n = 0; n < 128; n++)OLED_WR_Byte(OLED_GRAM[n][i], OLED_DATA);}
}/*** @brief 初始化SSD1306控制器* @details 配置顯示參數、硬件選項,執行上電序列*/
void OLED_Init(void)
{OLED_WR_Byte(0xAE,OLED_CMD);//關閉顯示屏OLED_WR_Byte(0x40,OLED_CMD);//設置起始行地址OLED_WR_Byte(0xB0,OLED_CMD);//設置頁面起始地址為頁面尋址模式,0-7OLED_WR_Byte(0xC8,OLED_CMD);//上下反置關(行重映射),C8,從COM[N-1]掃描到COM0;C0,設置 從COM0掃描到COM[N-1],N為復用率OLED_WR_Byte(0x81,OLED_CMD);// 設置對比度OLED_WR_Byte(0xff,OLED_CMD);// 選擇0xff對比度,選擇范圍0x00-0xffOLED_WR_Byte(0xa1,OLED_CMD);// 左右反置關(段重映射),A0H 設置GDDRAM的COL0映射到驅動器輸出SEG0,A1H 設置COL127映射到SEG0OLED_WR_Byte(0xa6,OLED_CMD);// 正常顯示(1亮0滅)OLED_WR_Byte(0xa8,OLED_CMD);// 設置多路傳輸比率,顯示行數OLED_WR_Byte(0x1f,OLED_CMD);// MUX=31 (顯示31行)OLED_WR_Byte(0xd3,OLED_CMD);// 設置垂直顯示偏移(向上)OLED_WR_Byte(0x00,OLED_CMD);// 偏移0行OLED_WR_Byte(0xd5,OLED_CMD);// 設置DCLK分頻和OSC頻率OLED_WR_Byte(0xf0,OLED_CMD);// 頻率最高OLED_WR_Byte(0xd9,OLED_CMD);// 設置預充電的持續時間OLED_WR_Byte(0x22,OLED_CMD);OLED_WR_Byte(0xda,OLED_CMD);// 設置COM引腳配置// 128*32OLED_WR_Byte(0x02,OLED_CMD);// 序列COM配置,禁用左右反置// 128*64// OLED_WR_Byte(0x12, OLED_CMD); // 交替COM,左右不反置OLED_WR_Byte(0xdb,OLED_CMD);//調整Vcomh調節器的輸出OLED_WR_Byte(0x49,OLED_CMD);OLED_WR_Byte(0x8d,OLED_CMD);// 啟用電荷泵OLED_WR_Byte(0x14,OLED_CMD);// 啟用電荷泵OLED_WR_Byte(0xaf,OLED_CMD);// 開OLED顯示OLED_Clear();
}/*** @brief 顯示單個ASCII字符* @param x 列坐標(0-127)* @param y 頁號(0-3)* @param chr 要顯示的字符* @param Char_Size 字體大小:8或16* @note 8像素字體使用6x8點陣,16像素使用8x16點陣*/
void OLED_ShowChar(uint8_t x, uint8_t y, uint8_t chr, uint8_t Char_Size)
{unsigned char c = 0, i = 0;c = chr - ' '; // 得到偏移后的值if (x > Max_Column - 1){x = 0;// 原代碼:y += 2(適用于8頁,改為y += 1適應4頁)y = y + 1; // 每頁高度為8像素,32行共4頁}if (Char_Size == 16){// 16像素字體占2頁(原為兩行,需確保不超過4頁)OLED_Set_Pos(x, y);for (i = 0; i < 8; i++)OLED_WR_Byte(F8X16[c * 16 + i], OLED_DATA);OLED_Set_Pos(x, y + 1);for (i = 0; i < 8; i++)OLED_WR_Byte(F8X16[c * 16 + i + 8], OLED_DATA);}else if (Char_Size == 8){OLED_Set_Pos(x, y);for (i = 0; i < 6; i++)OLED_WR_Byte(F6x8[c][i], OLED_DATA);}
}/*** @brief 顯示無符號整數* @param x,y 起始坐標* @param num 要顯示的數字(0~4294967295)* @param len 顯示位數(自動截斷高位)* @param size2 字體大小(需為8或16)*/
void OLED_ShowNum(uint8_t x, uint8_t y, uint32_t num, uint8_t len, uint8_t size2) // OLED_ShowNum(103,6,t,3,16);//顯示ASCII字符的碼值
{uint8_t t, temp;uint8_t enshow = 0;for (t = 0; t < len; t++){temp = (num / oled_pow(10, len - t - 1)) % 10;if (enshow == 0 && t < (len - 1)){if (temp == 0){OLED_ShowChar(x + (size2 / 2) * t, y, ' ', size2);continue;}elseenshow = 1;}OLED_ShowChar(x + (size2 / 2) * t, y, temp + '0', size2);}
}/*** @brief 計算m的n次方* @param m 底數* @param n 指數* @return m^n的值*/
uint32_t oled_pow(uint8_t m, uint8_t n)
{uint32_t result = 1;while (n--)result *= m;return result;
}/*** @brief 顯示字符串* @param x,y 起始坐標* @param chr 字符串數組指針* @param Char_Size 字體大小(8或16)* @note 字符串以'\0'結尾,自動計算長度*/
void OLED_ShowString(uint8_t x, uint8_t y, uint8_t chr[], uint8_t Char_Size)
{unsigned char j = 0;while (chr[j] != '\0'){OLED_ShowChar(x, y, chr[j], Char_Size);x += 8;if (x > Max_Column - 8){x = 0;y += 2;}j++;}
}/*** @brief 顯示漢字* @param x,y 起始坐標* @param no 漢字索引(0~7)* @note 漢字數據存儲在ZH數組中,索引范圍為0~7*/
void OLED_ShowCHinese(uint8_t x, uint8_t y, uint8_t no)
{uint8_t t, adder = 0;OLED_Set_Pos(x, y);for (t = 0; t < 16; t++){OLED_WR_Byte(ZH[2 * no][t], OLED_DATA);adder += 1;}OLED_Set_Pos(x, y + 1);for (t = 0; t < 16; t++){OLED_WR_Byte(ZH[2 * no + 1][t], OLED_DATA);adder += 1;}
}/*** @brief 設置OLED顯示位置* @param x 列坐標(0~127)* @param y 頁號(0~3)* @note 頁號與屏幕行的對應關系:每頁8行,128x32屏共4頁*/
void OLED_Set_Pos(unsigned char x, unsigned char y)
{OLED_WR_Byte(0xb0 + y, OLED_CMD);OLED_WR_Byte(((x & 0xf0) >> 4) | 0x10, OLED_CMD);OLED_WR_Byte((x & 0x0f), OLED_CMD);
}// 打開OLED顯示
void OLED_Display_On(void)
{OLED_WR_Byte(0X8D, OLED_CMD); // SET DCDC命令OLED_WR_Byte(0X14, OLED_CMD); // DCDC ONOLED_WR_Byte(0XAF, OLED_CMD); // DISPLAY ON
}// 關閉OLED顯示
void OLED_Display_Off(void)
{OLED_WR_Byte(0X8D, OLED_CMD); // SET DCDC命令OLED_WR_Byte(0X10, OLED_CMD); // DCDC OFFOLED_WR_Byte(0XAE, OLED_CMD); // DISPLAY OFF
}/*** @brief 清空OLED顯示* @note 將所有像素設置為0(黑色)*/
void OLED_Clear(void)
{uint8_t i, n;for (i = 0; i < 8; i++){OLED_WR_Byte(0xb0 + i, OLED_CMD); // 設置頁地址(0~7)OLED_WR_Byte(0x00, OLED_CMD); // 設置顯示位置—列低地址OLED_WR_Byte(0x10, OLED_CMD); // 設置顯示位置—列高地址for (n = 0; n < 128; n++)OLED_WR_Byte(0, OLED_DATA);} // 更新顯示
}/*** @brief 打開OLED顯示* @note 將所有像素設置為1(白色)*/
void OLED_On(void)
{uint8_t i, n;for (i = 0; i < 8; i++){OLED_WR_Byte(0xb0 + i, OLED_CMD); // 設置頁地址(0~7)OLED_WR_Byte(0x00, OLED_CMD); // 設置顯示位置—列低地址OLED_WR_Byte(0x10, OLED_CMD); // 設置顯示位置—列高地址for (n = 0; n < 128; n++)OLED_WR_Byte(1, OLED_DATA);} // 更新顯示
}/*** @brief 填充OLED顯示* @param fill_Data 填充數據(0x00或0xFF)* @note 0x00填充黑色,0xFF填充白色*/
void fill_picture(unsigned char fill_Data)
{unsigned char m, n;for (m = 0; m < 8; m++){OLED_WR_Byte(0xb0 + m, 0); // page0-page1OLED_WR_Byte(0x00, 0); // low column start addressOLED_WR_Byte(0x10, 0); // high column start addressfor (n = 0; n < 128; n++){OLED_WR_Byte(fill_Data, 1);}}
}/*** @brief 寫入數據或命令到OLED* @param dat 要寫入的數據* @param cmd 0表示命令,1表示數據* @note 通過I2C將數據發送到OLED控制器*/
void OLED_WR_Byte(uint8_t dat, uint8_t cmd)
{static uint8_t cmd_data[2];if (cmd == OLED_CMD){cmd_data[0] = 0x00;}else{cmd_data[0] = 0x40;}cmd_data[1] = dat;HAL_I2C_Master_Transmit(&hi2c1, 0X78, cmd_data, 2, 10);
}/*** @brief 寫入I2C命令或數據* @param I2C_Command 要寫入的命令或數據* @note 通過I2C將數據發送到OLED控制器*/
void Write_IIC_Command(unsigned char I2C_Command) // 寫命令
{uint8_t *pData;pData = &I2C_Command;HAL_I2C_Mem_Write(&hi2c1, 0x78, 0x00, I2C_MEMADD_SIZE_8BIT, pData, 1, 100);
}/*** @brief 寫入I2C數據* @param IIC_Data 要寫入的數據* @note 通過I2C將數據發送到OLED控制器*/
void Write_IIC_Data(unsigned char IIC_Data) // 寫數據
{uint8_t *pData;pData = &IIC_Data;HAL_I2C_Mem_Write(&hi2c1, 0x78, 0x40, I2C_MEMADD_SIZE_8BIT, pData, 1, 100);
}
2)oled.h
#ifndef __OLED_H
#define __OLED_H
// #include "sys.h"#include "main.h"
// OLED模式設置
// 0: 4線串行模式 (模塊的BS1,BS2均接GND)
#define OLED_MODE 0
#define X_WIDTH 128
#define Y_WIDTH 32
#define Max_Column 128 // 列
#define Max_Row 32 // 行#define SIZE 8
#define XLevelL 0x00
#define XLevelH 0x10#define Brightness 0xFF#define OLED_CMD 0 // 寫命令
#define OLED_DATA 1 // 寫數據void Write_IIC_Data(unsigned char IIC_Data);
void Write_IIC_Command(unsigned char I2C_Command);
void OLED_WR_Byte(uint8_t dat, uint8_t cmd);
void fill_picture(unsigned char fill_Data);
void OLED_On(void);
void OLED_Clear(void);
void OLED_Display_Off(void);
void OLED_Display_On(void);void OLED_Set_Pos(unsigned char x, unsigned char y);
void OLED_ShowCHinese(uint8_t x, uint8_t y, uint8_t no);
void OLED_ShowString(uint8_t x, uint8_t y, uint8_t chr[], uint8_t Char_Size); // OLED_ShowString(0,0,a,sizeof(a));
void OLED_ShowNum(uint8_t x, uint8_t y, uint32_t num, uint8_t len, uint8_t size2); // OLED_ShowNum(103,6,t,3,16);//顯示ASCII字符的碼值uint32_t oled_pow(uint8_t m, uint8_t n);
void OLED_ShowChar(uint8_t x, uint8_t y, uint8_t chr, uint8_t Char_Size);
void OLED_Init(void);
void OLED_Refresh_Gram(void);
void OLED_DrawBMP(unsigned char x0, unsigned char y0, unsigned char x1, unsigned char y1, unsigned char BMP[]);/*
x x x x x x x x x x x x x x x x ==16個字符列 y=0行
x x x x x x x x x x x x x x x x ==16個字符列 y=1行
x x x x x x x x x x x x x x x x ==16個字符列 y=2行
x x x x x x x x x x x x x x x x ==16個字符列 y=3行
x x x x x x x x x x x x x x x x ==16個字符列 y=4行
x x x x x x x x x x x x x x x x ==16個字符列 y=5行
x x x x x x x x x x x x x x x x ==16個字符列 y=6行
x x x x x x x x x x x x x x x x ==16個字符列 y=7行
*/#endif
3) oledfont.h
#ifndef __OLEDFONT_H
#define __OLEDFONT_H
//常用ASCII表
//偏移量32
//ASCII字符集: !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
//PC2LCD2002取模方式設置:陰碼+逐列式+順向+C51格式
//總共:3個字符集(12*12、16*16和24*24),用戶可以自行新增其他分辨率的字符集。
//每個字符所占用的字節數為:(size/8+((size%8)?1:0))*(size/2),其中size:是字庫生成時的點陣大小(12/16/24...)//12*12 ASCII字符集點陣
const unsigned char F6x8[][6] =
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,// sp
0x00, 0x00, 0x00, 0x2f, 0x00, 0x00,// !
0x00, 0x00, 0x07, 0x00, 0x07, 0x00,// "
0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14,// #
0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12,// $
0x00, 0x62, 0x64, 0x08, 0x13, 0x23,// %
0x00, 0x36, 0x49, 0x55, 0x22, 0x50,// &
0x00, 0x00, 0x05, 0x03, 0x00, 0x00,// '
0x00, 0x00, 0x1c, 0x22, 0x41, 0x00,// (
0x00, 0x00, 0x41, 0x22, 0x1c, 0x00,// )
0x00, 0x14, 0x08, 0x3E, 0x08, 0x14,// *
0x00, 0x08, 0x08, 0x3E, 0x08, 0x08,// +
0x00, 0x00, 0x00, 0xA0, 0x60, 0x00,// ,
0x00, 0x08, 0x08, 0x08, 0x08, 0x08,// -
0x00, 0x00, 0x60, 0x60, 0x00, 0x00,// .
0x00, 0x20, 0x10, 0x08, 0x04, 0x02,// /
0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E,// 0
0x00, 0x00, 0x42, 0x7F, 0x40, 0x00,// 1
0x00, 0x42, 0x61, 0x51, 0x49, 0x46,// 2
0x00, 0x21, 0x41, 0x45, 0x4B, 0x31,// 3
0x00, 0x18, 0x14, 0x12, 0x7F, 0x10,// 4
0x00, 0x27, 0x45, 0x45, 0x45, 0x39,// 5
0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30,// 6
0x00, 0x01, 0x71, 0x09, 0x05, 0x03,// 7
0x00, 0x36, 0x49, 0x49, 0x49, 0x36,// 8
0x00, 0x06, 0x49, 0x49, 0x29, 0x1E,// 9
0x00, 0x00, 0x36, 0x36, 0x00, 0x00,// :
0x00, 0x00, 0x56, 0x36, 0x00, 0x00,// ;
0x00, 0x08, 0x14, 0x22, 0x41, 0x00,// <
0x00, 0x14, 0x14, 0x14, 0x14, 0x14,// =
0x00, 0x00, 0x41, 0x22, 0x14, 0x08,// >
0x00, 0x02, 0x01, 0x51, 0x09, 0x06,// ?
0x00, 0x32, 0x49, 0x59, 0x51, 0x3E,// @
0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C,// A
0x00, 0x7F, 0x49, 0x49, 0x49, 0x36,// B
0x00, 0x3E, 0x41, 0x41, 0x41, 0x22,// C
0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C,// D
0x00, 0x7F, 0x49, 0x49, 0x49, 0x41,// E
0x00, 0x7F, 0x09, 0x09, 0x09, 0x01,// F
0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A,// G
0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F,// H
0x00, 0x00, 0x41, 0x7F, 0x41, 0x00,// I
0x00, 0x20, 0x40, 0x41, 0x3F, 0x01,// J
0x00, 0x7F, 0x08, 0x14, 0x22, 0x41,// K
0x00, 0x7F, 0x40, 0x40, 0x40, 0x40,// L
0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F,// M
0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F,// N
0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E,// O
0x00, 0x7F, 0x09, 0x09, 0x09, 0x06,// P
0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E,// Q
0x00, 0x7F, 0x09, 0x19, 0x29, 0x46,// R
0x00, 0x46, 0x49, 0x49, 0x49, 0x31,// S
0x00, 0x01, 0x01, 0x7F, 0x01, 0x01,// T
0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F,// U
0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F,// V
0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F,// W
0x00, 0x63, 0x14, 0x08, 0x14, 0x63,// X
0x00, 0x07, 0x08, 0x70, 0x08, 0x07,// Y
0x00, 0x61, 0x51, 0x49, 0x45, 0x43,// Z
0x00, 0x00, 0x7F, 0x41, 0x41, 0x00,// [
0x00, 0x55, 0x2A, 0x55, 0x2A, 0x55,// 55
0x00, 0x00, 0x41, 0x41, 0x7F, 0x00,// ]
0x00, 0x04, 0x02, 0x01, 0x02, 0x04,// ^
0x00, 0x40, 0x40, 0x40, 0x40, 0x40,// _
0x00, 0x00, 0x01, 0x02, 0x04, 0x00,// '
0x00, 0x20, 0x54, 0x54, 0x54, 0x78,// a
0x00, 0x7F, 0x48, 0x44, 0x44, 0x38,// b
0x00, 0x38, 0x44, 0x44, 0x44, 0x20,// c
0x00, 0x38, 0x44, 0x44, 0x48, 0x7F,// d
0x00, 0x38, 0x54, 0x54, 0x54, 0x18,// e
0x00, 0x08, 0x7E, 0x09, 0x01, 0x02,// f
0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C,// g
0x00, 0x7F, 0x08, 0x04, 0x04, 0x78,// h
0x00, 0x00, 0x44, 0x7D, 0x40, 0x00,// i
0x00, 0x40, 0x80, 0x84, 0x7D, 0x00,// j
0x00, 0x7F, 0x10, 0x28, 0x44, 0x00,// k
0x00, 0x00, 0x41, 0x7F, 0x40, 0x00,// l
0x00, 0x7C, 0x04, 0x18, 0x04, 0x78,// m
0x00, 0x7C, 0x08, 0x04, 0x04, 0x78,// n
0x00, 0x38, 0x44, 0x44, 0x44, 0x38,// o
0x00, 0xFC, 0x24, 0x24, 0x24, 0x18,// p
0x00, 0x18, 0x24, 0x24, 0x18, 0xFC,// q
0x00, 0x7C, 0x08, 0x04, 0x04, 0x08,// r
0x00, 0x48, 0x54, 0x54, 0x54, 0x20,// s
0x00, 0x04, 0x3F, 0x44, 0x40, 0x20,// t
0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C,// u
0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C,// v
0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C,// w
0x00, 0x44, 0x28, 0x10, 0x28, 0x44,// x
0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C,// y
0x00, 0x44, 0x64, 0x54, 0x4C, 0x44,// z
0x14, 0x14, 0x14, 0x14, 0x14, 0x14,// horiz lines
};/****************************************8*16的點陣************************************/
const unsigned char F8X16[]=
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// 00x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,//! 10x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//" 20x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,//# 30x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00,0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,//$ 40xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,//% 50x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,//& 60x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//' 70x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,//( 80x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,//) 90x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,//* 100x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,//+ 110x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,//, 120x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,//- 130x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,//. 140x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,/// 150x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,//0 160x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//1 170x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//2 180x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,//3 190x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,//4 200x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,//5 210x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,//6 220x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,//7 230x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//8 240x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,//9 250x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//: 260x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,//; 270x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,//< 280x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,//= 290x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,//> 300x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,//? 310xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,//@ 320x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,//A 330x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,//B 340xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,//C 350x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,//D 360x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,//E 370x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,//F 380xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,//G 390x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//H 400x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//I 410x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,//J 420x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,//K 430x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L 440x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,//M 450x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,//N 460xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,//O 470x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,//P 480xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,//Q 490x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//R 500x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,//S 510x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//T 520x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//U 530x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,//V 540xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,//W 550x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,//X 560x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//Y 570x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,//Z 580x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,//[ 590x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,//\ 600x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,//] 610x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^ 620x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,//_ 630x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//` 640x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,//a 650x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,//b 660x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,//c 670x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,//d 680x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,//e 690x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//f 700x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,//g 710x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//h 720x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//i 730x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,//j 740x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,//k 750x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//l 760x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,//m 770x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//n 780x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//o 790x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,//p 800x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,//q 810x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,//r 820x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,//s 830x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,//t 840x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,//u 850x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,//v 860x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,//w 870x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,//x 880x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,//y 890x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,//z 900x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,//{ 910x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,//| 920x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,//} 930x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~ 94
};char ZH[][32]={ {0x40,0x44,0x54,0x65,0xC6,0x64,0x54,0x44,0x00,0xFC,0x44,0x44,0xC4,0x42,0x40,0x00},
{0x20,0x12,0x4A,0x82,0x7F,0x02,0x0A,0x92,0x60,0x1F,0x00,0x00,0xFF,0x00,0x00,0x00},/*"新",0*/
{0x00,0x20,0x18,0xC7,0x44,0x44,0x44,0x44,0xFC,0x44,0x44,0x44,0x44,0x04,0x00,0x00},
{0x04,0x04,0x04,0x07,0x04,0x04,0x04,0x04,0xFF,0x04,0x04,0x04,0x04,0x04,0x04,0x00},/*"年",1*/
{0x00,0xE0,0x00,0xFF,0x10,0x20,0x08,0x08,0x08,0xFF,0x08,0x08,0xF8,0x00,0x00,0x00},
{0x01,0x00,0x00,0xFF,0x00,0x81,0x41,0x31,0x0D,0x03,0x0D,0x31,0x41,0x81,0x81,0x00},/*"快",2*/
{0x00,0x00,0xE0,0x9C,0x84,0x84,0x84,0xF4,0x82,0x82,0x83,0x82,0x80,0x80,0x00,0x00},
{0x00,0x20,0x10,0x08,0x06,0x40,0x80,0x7F,0x00,0x00,0x02,0x04,0x08,0x30,0x00,0x00},/*"樂",3*/
{0x40,0x30,0x11,0x96,0x90,0x90,0x91,0x96,0x90,0x90,0x98,0x14,0x13,0x50,0x30,0x00},
{0x04,0x04,0x04,0x04,0x04,0x44,0x84,0x7E,0x06,0x05,0x04,0x04,0x04,0x04,0x04,0x00},/*"學",4*/
{0x80,0x80,0x80,0x80,0xFF,0x80,0x80,0xA0,0x90,0x88,0x84,0x82,0x80,0x80,0x80,0x00},
{0x00,0x00,0x00,0x00,0xFF,0x40,0x21,0x12,0x04,0x08,0x10,0x20,0x20,0x40,0x40,0x00},/*"長",5*/
{0x20,0x24,0x24,0x24,0xFE,0x23,0x22,0x20,0x20,0xFF,0x20,0x22,0x2C,0xA0,0x20,0x00},
{0x00,0x08,0x48,0x84,0x7F,0x02,0x41,0x40,0x20,0x13,0x0C,0x14,0x22,0x41,0xF8,0x00},/*"我",6*/
{0x10,0x0C,0x04,0x24,0x24,0x24,0x25,0x26,0x24,0x24,0x24,0x24,0x04,0x14,0x0C,0x00},
{0x00,0x81,0x81,0x41,0x31,0x0F,0x01,0x01,0x01,0x7F,0x81,0x81,0x81,0xF1,0x00,0x00},/*"完",7*/
{0x00,0x00,0xF8,0x88,0x88,0x88,0x88,0x08,0x08,0xFF,0x08,0x09,0x0A,0xC8,0x08,0x00},
{0x80,0x60,0x1F,0x00,0x10,0x20,0x1F,0x80,0x40,0x21,0x16,0x18,0x26,0x41,0xF8,0x00},/*"成",8*/
{0x00,0xFC,0x04,0xFC,0x00,0x10,0x10,0xFF,0x90,0xC8,0x09,0x0E,0x08,0xE8,0x08,0x00},
{0x00,0x0F,0x04,0x0F,0x00,0x44,0x82,0x7F,0x00,0x40,0x4F,0x60,0x5C,0x43,0x40,0x00},/*"啦",9*/{0x10,0x88,0xC4,0x33,0x00,0x20,0x10,0x28,0x24,0xE3,0x24,0x28,0x10,0x20,0x20,0x00},
{0x01,0x00,0xFF,0x00,0x20,0x11,0x0D,0x41,0x81,0x7F,0x01,0x05,0x09,0x31,0x00,0x00},/*"徐",10*/
/* (16 X 16 , 宋體 )*/{0x00,0xFE,0x22,0xFE,0x00,0xFE,0x22,0xFE,0x00,0xFC,0x16,0x25,0x84,0xFC,0x00,0x00},
{0x60,0x1F,0x22,0xBF,0x40,0x3F,0x82,0xFF,0x00,0x13,0x12,0x12,0x52,0x82,0x7E,0x00},/*"鵬",11*/
/* (16 X 16 , 宋體 )*/{0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0xFE,0x40,0xA0,0x10,0x08,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x0C,0x10,0x21,0x42,0xF0,0x00},/*"飛",12*/
/* (16 X 16 , 宋體 )*///每個字必須帶大括號否則顯示效果不好
};#endif
(注:需根據具體硬件調整I2C引腳和屏幕參數)
一、硬件連接
- OLED屏幕:SSD1306控制器,支持I2C通信。
- STM32引腳配置:
- SCL:I2C時鐘線(如PB6)
- SDA:I2C數據線(如PB7)
- I2C地址:0x78(若屏幕帶RESET引腳,需額外配置)
二、代碼結構
1. 核心文件說明
- oled.c:驅動層實現,包含初始化、顯存管理、字符顯示等功能。
- oled.h:定義宏、函數原型及屏幕參數。
- oledfont.h:存儲ASCII和漢字點陣數據(6x8、8x16字體)。
2. 關鍵函數解析
- OLED_Init():初始化SSD1306控制器,配置顯示參數。
void OLED_Init(void) {OLED_WR_Byte(0xAE, OLED_CMD); // 關閉顯示OLED_WR_Byte(0xD5, OLED_CMD); // 設置時鐘分頻OLED_WR_Byte(0xA8, OLED_CMD); // 設置復用率// ... 其他配置命令OLED_Clear(); ????????????????// 清屏
}
- OLED_Refresh_Gram():將顯存數據刷新到物理屏幕,通過I2C逐頁寫入。
- OLED_ShowChar():顯示單個字符,支持8/16像素字體,自動換行。
三、關鍵實現細節
1. 顯存管理
- 顯存數組:OLED_GRAM[128][8],每頁8行,共4頁(128x32)或8頁(128x64)。
- 刷新邏輯:僅刷新有效頁,減少I2C傳輸量。
void OLED_Refresh_Gram(void) {for (uint8_t i = 0; i < 4; i++) { // 僅刷新前4頁OLED_Set_Pos(0, i);HAL_I2C_Mem_Write(&hi2c1, 0x78, 0x40, I2C_MEMADD_SIZE_8BIT, OLED_GRAM[i], 128, 100);}
}
2. 字體顯示優化
- ASCII字符:使用F6x8和F8X16點陣數據,通過偏移量快速定位。
- 漢字顯示:預存儲常用漢字點陣(如“新年快樂”),通過索引調用。
void OLED_ShowCHinese(uint8_t x, uint8_t y, uint8_t no) {OLED_Set_Pos(x, y);for (uint8_t t = 0; t < 16; t++)OLED_WR_Byte(ZH[2*no][t], OLED_DATA); // 寫入上半部分OLED_Set_Pos(x, y+1);for (uint8_t t = 0; t < 16; t++)OLED_WR_Byte(ZH[2*no+1][t], OLED_DATA); // 寫入下半部分
}
四、注意事項(重點!)
-
頭文件包含問題
- main.c中禁止包含oledfont.h,否則報錯:工程\工程名.axf: Error: L6200E: Symbol F6x8 multiply defined (by gpio.o and main.o).等。
- 正確做法:僅在oled.c中包含oledfont.h,其他文件通過extern引用。
-
屏幕參數調整
- 修改初始化參數(oled.c第106-110行),否則字符高度異常。
// 128x32屏配置
OLED_WR_Byte(0xDA, OLED_CMD);
OLED_WR_Byte(0x02, OLED_CMD); // COM引腳配置
// 128x64屏需改為:OLED_WR_Byte(0x12, OLED_CMD);
-
I2C地址與速率
- 確認OLED的I2C地址(通常為0x78或0x7A)。
- 調整I2C時鐘頻率(STM32默認100kHz,SSD1306支持400kHz)。
- extern I2C_HandleTypeDef hi2c1; ///< I2C外設句柄,根據實際情況改
?
五、使用示例
顯示字符串和數字
OLED_ShowString(0, 0, "Hello World!", 16); // 16像素字體
OLED_ShowNum(0, 2, 2023, 4, 16); ?????????// 顯示數字2023
顯示自定義位圖
const uint8_t BMP[] = {0xFF, 0x81, 0xBD, ...}; // 位圖數據
OLED_DrawBMP(0, 0, 128, 4, BMP); ?????????????// 全屏顯示位圖
六、常見問題
- 屏幕不顯示:檢查I2C地址、引腳配置及初始化序列。
- 字符錯位:確認OLED_Set_Pos()的頁號與顯存對應關系。
- 刷新閃爍:提高刷新頻率(建議200Hz以上),或使用雙緩沖機制。
感謝婷婷后援團的小徐同學???