文章目录[隐藏]
一、开发基础概述:
1、0.96 寸 OLED 模块接口定义:
SPI 接口模块:
- GND 电源地
- VCC 电源正(3 ~ 5.5V)
- D0 OLED 的 D0 脚,在 SPI 和 IIC 通信中为时钟管脚
- D1 OLED 的 D1 脚,在 SPI 和 IIC 通信中为数据管脚
- RES OLED 的 RES 管脚,用于复位(低电平复位)
- DC OLED 的 D/C 管脚,数据和命令控制管脚
- CS OLED 的 CS 管脚,片选管脚
IIC 接口模块:
- GND 电源地
- VCC 电源正(3 ~ 5.5V)
- SCL OLED 的 D0 脚,在 SPI 和 IIC 通信中为时钟管脚
- SDA OLED 的 D1 脚,在 SPI 和 IIC 通信中为数据管脚
2、0.96寸 OLED 模块 128×64 分辨率解析:
0.96寸 OLED(128×64分辨率)模块,128×64分辨率即 OLED 显示横坐标有 128 个像素格,纵坐标有 64 个像素格,但是由于 OLED 不能一次控制一个点阵,只能控制 8 个点阵,而且是垂直方向扫描控制,因此垂直方向坐标可选为 0~7 页(一页 8 个像素格),水平方向可选坐标 0~128。一般一个中文字体占 16×16 个像素格,字母以及符号占 8×16 个像素格。
3、基于 STM32 的 OLED 应用,需要做哪些事情:
- 移植 OLED 的底层驱动函数库
- 准备需要的中文字符和图片等数据
- 调用 OLED 驱动库中的底层函数进行应用开发
二、STM32CubeMX 配置:
1、此处我们使用 SPI 接口,接线如图:
2、STM32CubeMX 配置:
把PB12
、PB13
、PB14
、PB15
均设置为 GPIO 输出功能
三、
1、移植 OLED 的底层驱动函数库:
① 将 4 个驱动文件拷贝到工程文件(和main.c
同一目录)中,将oled.c
文件添加到工程代码文件中,并在main.c
中引入oled.h
头文件(.c
、.h
文件的文件名根据自己手上的驱动文件而不同,可以来自淘宝商家、技术社区等)
示例文件:
main.c
中引入oled.h
头文件:
② 根据所选用的芯片型号,修改oled.h
头文件中所用的芯片的头文件:
头文件名可以参考main.h
中的头文件
③ 根据电路原理图,修改oled.h
中 OLED 的引脚定义:
并检查其余定义是否正确
④ 查看OLED_Init(void)
初始化函数的源码,根据电路接口和应用需要进行修改:
void OLED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
__HAL_RCC_GPIOB_CLK_ENABLE();
//CS
GPIO_InitStruct.Pin = OLED_CS_GPIO_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(OLED_CS_GPIO, &GPIO_InitStruct);
//DC
GPIO_InitStruct.Pin = OLED_DC_GPIO_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(OLED_DC_GPIO, &GPIO_InitStruct);
//SCLK
GPIO_InitStruct.Pin = OLED_SCLK_GPIO_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(OLED_SCLK_GPIO, &GPIO_InitStruct);
//SDIN
GPIO_InitStruct.Pin = OLED_SDIN_GPIO_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(OLED_SDIN_GPIO, &GPIO_InitStruct);
HAL_GPIO_WritePin(OLED_CS_GPIO, OLED_CS_GPIO_PIN, GPIO_PIN_SET);
HAL_GPIO_WritePin(OLED_DC_GPIO, OLED_DC_GPIO_PIN, GPIO_PIN_SET);
HAL_GPIO_WritePin(OLED_SCLK_GPIO, OLED_SCLK_GPIO_PIN, GPIO_PIN_SET);
HAL_GPIO_WritePin(OLED_SDIN_GPIO, OLED_SDIN_GPIO_PIN, GPIO_PIN_SET);
2、准备需要的中文字符和图片等数据:
使用取字模软件 PCtoLCD2002:
PCtoLCD2002 的使用方法本文不作介绍。
3、OLED 驱动库中常用的函数:
void OLED_Init(void); // OLED 初始化函数
void OLED_Clear(void); // OLED 清屏函数
void OLED_ShowString(u8 x, u8 y, u8 *chr); // 显示英文字符串函数
void OLED_ShowChinese(u8 x, u8 y, u8 no); // 显示中文字符串函数
void OLED_ShowPicture(u8 x0, u8 y0, u8 x1, u8 y1, u8 BMP[]); // 显示图片函数
四、程序编写:
1、main.c
:
/* USER CODE BEGIN 0 */
extern unsigned char BMP1[];
void OLED_display_info()
{
OLED_Clear();
OLED_ShowString(0, 0, (uint8_t *)"CSDN @");
OLED_ShowCHinese(48, 0, 0);
OLED_ShowCHinese(64, 0, 1);
OLED_ShowCHinese(80, 0, 1);
OLED_DrawBMP(77, 2, 127, 7, BMP1);
}
/* USER CODE END 0 */
/* USER CODE BEGIN 2 */
OLED_Init();
OLED_display_info();
/* USER CODE END 2 */
2、oledfont.h
:
const unsigned char Hzk[][32]={
{0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x00},
{0x00,0x02,0x01,0x00,0x00,0x00,0x01,0x01,0x01,0x02,0x02,0x02,0x01,0x00,0x00,0x00},/*"~",0*/
/* (16 X 16 , 宋体 )*/
{0x04,0x04,0x24,0x24,0xAF,0x24,0x2C,0x34,0x24,0x24,0xAF,0x24,0x24,0x04,0x04,0x00},
{0x02,0x12,0x12,0x12,0x12,0x13,0x12,0xFE,0x12,0x13,0x12,0x12,0x12,0x12,0x02,0x00},/*"莘",1*/
/* (16 X 16 , 宋体 )*/
};
3、bmp.h
:
const unsigned char BMP1[] =
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0xC0,0xC0,
0xE0,0xE0,0xF0,0x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x00,0xFC,0xFF,0x3F,
0xC7,0xFB,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xC1,0x3F,0xFF,0xFE,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,
0x03,0x00,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,
0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x00,
0x03,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
/* (50 X 50 )*/
};
4、运行:
版权声明:本文为CSDN博主「~莘莘」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lcx1837/article/details/119936510
暂无评论