将厂家资料路径下的【LCD】文件夹,复制到自己的工程中。(工程可以参考
入门手册工程模板
)
复制示意图
我们打开工程文件,将我们刚刚复制到文件夹中的文件,导入C文件和路径。
将
oled.h
文件下的
sys.h
改为
board.h
。
修改oled.h头文件
将
oled.c
文件下的
delay.h
注释掉。
修改oled.c文件
在
oled.h
文件中定义三个宏,u8、u16、u32。
#ifndef u8
#define u8 uint8_t
#endif
#ifndef u16
#define u16 uint16_t
#endif
#ifndef u32
#define u32 uint32_t
#endif
该屏幕需要设置4个接口,具体接口说明见
各引脚说明
。
模块为IIC通信协议的从机,SCL为IIC信号线,SDA为IIC数据线。
当前厂家源码使用的是软件IIC接口,IIC时序部分厂家已经完成,我们只需要将引脚和延时配置好即可。所以对应接入的屏幕引脚请按照你的需要。这里选择的引脚见下图。
选择好引脚后,进入工程开始编写屏幕引脚初始化代码。
在oled.h中定义LCD端口移植宏
#define OLED_RCC_ENABLE() __RCC_GPIOB_CLK_ENABLE()
#define OLED_GPIO_PORT CW_GPIOB
#define OLED_SCL_PIN GPIO_PIN_10
#define OLED_SDA_PIN GPIO_PIN_11
将oled.c源代码中的 void OLED_Init(void) 修改为如下代码。
void OLED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
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;
GPIO_Init(GPIOB, &GPIO_InitStructure);
delay_ms(200);
OLED_WR_Byte(0xAE,OLED_CMD);
OLED_WR_Byte(0x00,OLED_CMD);
OLED_WR_Byte(0x10,OLED_CMD);
OLED_WR_Byte(0x40,OLED_CMD);
OLED_WR_Byte(0x81,OLED_CMD);
OLED_WR_Byte(0xCF,OLED_CMD);
OLED_WR_Byte(0xA1,OLED_CMD);
OLED_WR_Byte(0xC8,OLED_CMD);
OLED_WR_Byte(0xA6,OLED_CMD);
OLED_WR_Byte(0xA8,OLED_CMD);
OLED_WR_Byte(0x3f,OLED_CMD);
OLED_WR_Byte(0xD3,OLED_CMD);
OLED_WR_Byte(0x00,OLED_CMD);
OLED_WR_Byte(0xd5,OLED_CMD);
OLED_WR_Byte(0x80,OLED_CMD);
OLED_WR_Byte(0xD9,OLED_CMD);
OLED_WR_Byte(0xF1,OLED_CMD);
OLED_WR_Byte(0xDA,OLED_CMD);
OLED_WR_Byte(0x12,OLED_CMD);
OLED_WR_Byte(0xDB,OLED_CMD);
OLED_WR_Byte(0x40,OLED_CMD);
OLED_WR_Byte(0x20,OLED_CMD);
OLED_WR_Byte(0x02,OLED_CMD);
OLED_WR_Byte(0x8D,OLED_CMD);
OLED_WR_Byte(0x14,OLED_CMD);
OLED_WR_Byte(0xA4,OLED_CMD);
OLED_WR_Byte(0xA6,OLED_CMD);
OLED_Clear();
OLED_WR_Byte(0xAF,OLED_CMD);
}
将lcd_init.h中的 OLED端口定义 宏。进行修改!
#define OLED_SCL_Clr() GPIO_WritePin(OLED_GPIO_PORT,OLED_SCL_PIN,GPIO_Pin_RESET)
#define OLED_SCL_Set() GPIO_WritePin(OLED_GPIO_PORT,OLED_SCL_PIN,GPIO_Pin_SET)
#define OLED_SDA_Clr() GPIO_WritePin(OLED_GPIO_PORT,OLED_SDA_PIN,GPIO_Pin_RESET)
#define OLED_SDA_Set() GPIO_WritePin(OLED_GPIO_PORT,OLED_SDA_PIN,GPIO_Pin_SET)
原来端口定义
修改后端口定义
打开oled.c文件 找到 void IIC_delay(void) 函数,将里面的内容替换为
/**********************************************************
* 函 数 名 称:IIC_delay
* 函 数 功 能:延时
* 传 入 参 数:无
* 函 数 返 回:无
* 作 者:www.lckfb.com
* 备 注:
**********************************************************/
void IIC_delay(void)
{
delay_us(5);
}
* Change Logs:
* Date Author Notes
* 2024-06-18 LCKFB-LP first version
*/
#include "board.h"
#include "stdio.h"
#include "bsp_uart.h"
#include "oled.h"
int32_t main(void)
{
board_init();
uart1_init(115200);
OLED_Init();
OLED_Clear();
while(1)
{
OLED_ShowString(0,0,(uint8_t *)"ABC",8,1);
OLED_ShowString(0,8,(uint8_t *)"ABC",12,1);
OLED_ShowString(0,20,(uint8_t *)"ABC",16,1);
OLED_ShowString(0,36,(uint8_t *)"ABC",24,1);
OLED_Refresh();
delay_ms(100);
}
}
上电效果:
移植成功案例
(软件和硬件SPI)
:
链接:
https://pan.baidu.com/s/1YnMyfuDg7ax4LpDQ028dWQ?pwd=LCKF
提取码:LCKF