一、模块来源 
采购链接: 0.96寸OLED显示屏12864液晶屏模块IIC显示屏ssd1306 12864oled
资料下载链接: https://pan.baidu.com/s/1xy2zH8-hs-S8-_AcVtBP_g
资料提取码:0jhj
二、规格参数 
工作电压:3.3V
工作电流:9MA
模块尺寸:27.3 x 27.8 MM
像素大小:128(H) x 64(V)RGB
驱动芯片:SSD1306
通信协议:IIC
管脚数量:4 Pin(2.54mm间距排针)
以上信息见厂家资料文件
三、移植过程 
我们的目标是将例程移植至开发板上。按照以下步骤,即可完成移植。
- 将源码导入工程;
 - 根据编译报错处进行粗改;
 - 修改引脚配置;
 - 修改时序配置;
 - 移植验证。
 
1、查看资料 
打开厂家资料例程(例程下载见百度网盘链接下载)。具体路径见例程路径
2、移植至工程 
将厂家资料路径下的【LCD】文件夹,复制到自己的工程中。(工程可以参考入门手册工程模板)
我们打开工程文件,将我们刚刚复制到文件夹中的文件,导入C文件和路径。

将 oled.h 文件下的 sys.h 改为 stm32f10x.h。
将 oled.c 文件下的 delay.h 改为 board.h。
在oled.h文件中定义三个宏,u8、u16、u32。
c
#ifndef u8
#define u8 uint8_t
#endif
#ifndef u16
#define u16 uint16_t
#endif
#ifndef u32
#define u32 uint32_t
#endif1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
3、引脚选择 
该屏幕需要设置4个接口,具体接口说明见 各引脚说明。
模块为IIC通信协议的从机,SCL为IIC信号线,SDA为IIC数据线。
软件I2C移植 
选择好引脚后,进入工程开始编写屏幕引脚初始化代码。
我们更改 lcd_init.h中的 OLED 端口定义
c
//-----------------OLED端口定义----------------
#define OLED_SCL_Clr() GPIO_ResetBits(GPIOB,GPIO_Pin_10)//SCL
#define OLED_SCL_Set() GPIO_SetBits(GPIOB,GPIO_Pin_10)
#define OLED_SDA_Clr() GPIO_ResetBits(GPIOB,GPIO_Pin_11)//DIN
#define OLED_SDA_Set() GPIO_SetBits(GPIOB,GPIO_Pin_11)
//#define OLED_RES_Clr() GPIO_ResetBits(GPIOD,GPIO_Pin_4)//RES
//#define OLED_RES_Set() GPIO_SetBits(GPIOD,GPIO_Pin_4)1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
将oled.c源代码中的 void OLED_Init(void) 修改为如下代码。
c
//OLED的初始化
void OLED_Init(void)
{
    GPIO_InitTypeDef  GPIO_InitStructure;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//使能GPIO时钟
    //GPIO初始化设置
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出模式
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//100MHz
    GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化
    delay_ms(200);
    OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panel
    OLED_WR_Byte(0x00,OLED_CMD);//---set low column address
    OLED_WR_Byte(0x10,OLED_CMD);//---set high column address
    OLED_WR_Byte(0x40,OLED_CMD);//--set start line address  Set Mapping RAM Display Start Line (0x00~0x3F)
    OLED_WR_Byte(0x81,OLED_CMD);//--set contrast control register
    OLED_WR_Byte(0xCF,OLED_CMD);// Set SEG Output Current Brightness
    OLED_WR_Byte(0xA1,OLED_CMD);//--Set SEG/Column Mapping     0xa0左右反置 0xa1正常
    OLED_WR_Byte(0xC8,OLED_CMD);//Set COM/Row Scan Direction   0xc0上下反置 0xc8正常
    OLED_WR_Byte(0xA6,OLED_CMD);//--set normal display
    OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
    OLED_WR_Byte(0x3f,OLED_CMD);//--1/64 duty
    OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset        Shift Mapping RAM Counter (0x00~0x3F)
    OLED_WR_Byte(0x00,OLED_CMD);//-not offset
    OLED_WR_Byte(0xd5,OLED_CMD);//--set display clock divide ratio/oscillator frequency
    OLED_WR_Byte(0x80,OLED_CMD);//--set divide ratio, Set Clock as 100 Frames/Sec
    OLED_WR_Byte(0xD9,OLED_CMD);//--set pre-charge period
    OLED_WR_Byte(0xF1,OLED_CMD);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
    OLED_WR_Byte(0xDA,OLED_CMD);//--set com pins hardware configuration
    OLED_WR_Byte(0x12,OLED_CMD);
    OLED_WR_Byte(0xDB,OLED_CMD);//--set vcomh
    OLED_WR_Byte(0x40,OLED_CMD);//Set VCOM Deselect Level
    OLED_WR_Byte(0x20,OLED_CMD);//-Set Page Addressing Mode (0x00/0x01/0x02)
    OLED_WR_Byte(0x02,OLED_CMD);//
    OLED_WR_Byte(0x8D,OLED_CMD);//--set Charge Pump enable/disable
    OLED_WR_Byte(0x14,OLED_CMD);//--set(0x10) disable
    OLED_WR_Byte(0xA4,OLED_CMD);// Disable Entire Display On (0xa4/0xa5)
    OLED_WR_Byte(0xA6,OLED_CMD);// Disable Inverse Display On (0xa6/a7)
    OLED_Clear();
    OLED_WR_Byte(0xAF,OLED_CMD);
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
打开 oled.c 文件 找到 void IIC_delay(void) 函数,将里面的内容替换为下方代码。
c
/**********************************************************
 * 函 数 名 称:IIC_delay
 * 函 数 功 能:延时
 * 传 入 参 数:无
 * 函 数 返 回:无
 * 作       者:www.lckfb.com
 * 备       注:
**********************************************************/
void IIC_delay(void)
{
      delay_us(10);
}1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
到这里软件I2C就移植完成了,可移步到第四节进行移植验证。
四、移植验证 
在main.c中输入代码如下:
c
/*
 * 立创开发板软硬件资料与相关扩展板软硬件资料官网全部开源
 * 开发板官网:www.lckfb.com
 * 技术支持常驻论坛,任何技术问题欢迎随时交流学习
 * 立创论坛:https://oshwhub.com/forum
 * 关注bilibili账号:【立创开发板】,掌握我们的最新动态!
 * 不靠卖板赚钱,以培养中国工程师为己任
 *
 Change Logs:
 * Date           Author       Notes
 * 2024-03-14     LCKFB-LP    first version
 */
#include "board.h"
#include "bsp_uart.h"
#include <stdio.h>
#include "oled.h"
int main(void)
{
    board_init();
    uart1_init(115200U);
    OLED_Init();     //初始化OLED
    OLED_Clear();
    while(1)
    {
        OLED_ShowString(0,0,(uint8_t *)"ABC",8,1);//6*8 “ABC”
        OLED_ShowString(0,8,(uint8_t *)"ABC",12,1);//6*12 “ABC”
        OLED_ShowString(0,20,(uint8_t *)"ABC",16,1);//8*16 “ABC”
        OLED_ShowString(0,36,(uint8_t *)"ABC",24,1);//12*24 “ABC”
        OLED_Refresh();
        delay_ms(100);
    }
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
上电效果: