基于江科大的oled标准库进行移植 到Hal库上
本人参考了许多大佬的源码 进行更改
由于F4和F1主频不一样 由于F4主频太高 在进行软件iic时需要延时一下 才可驱动oled 本人在网上找了一个开源的us延时函数 已经添加进入
文件分享
通过百度网盘分享的文件:delay,oled等2个文件
链接:https://pan.baidu.com/s/17W0AlXyCT9Zx64YfZzSLMg
提取码:30jg
1.移植全过程
先使用cubeMX创建一个HAL库的f4工程
f4zgt6
完成后生成工程
在cubemx生成的工程文件目录下面
Drivers 中新建一个文件夹 oled 和delay
把文件全部复制粘贴到对应的文件夹进去
打开keil
小方框新建目录 添加两个对应的文件
添加头文件的路径
将oled和delay的头文件路径添加进去
每个文件需要包含的文件如图所示
myiic中
//IIC软初始化
void iic_init(void)
{
//GPIO结构体
GPIO_InitTypeDef GPIO_InitType;
//使能GPIOB
__HAL_RCC_GPIOB_CLK_ENABLE();
//配置GPIO(PB6对应SCL,配置成推挽输出)
GPIO_InitType.Pin = GPIO_PIN_4;
GPIO_InitType.Mode = GPIO_MODE_OUTPUT_PP; /* 推挽输出 */
GPIO_InitType.Pull = GPIO_PULLUP;
GPIO_InitType.Speed = GPIO_SPEED_FREQ_HIGH; /* 高速 */
HAL_GPIO_Init(GPIOB, &GPIO_InitType); /* SCL */
//配置GPIO(PB7对应SDA,配置成开漏输出)
GPIO_InitType.Pin = GPIO_PIN_5;
GPIO_InitType.Mode = GPIO_MODE_OUTPUT_OD; /* 开漏输出 */
GPIO_InitType.Pull = GPIO_PULLUP;
GPIO_InitType.Speed = GPIO_SPEED_FREQ_HIGH; /* 高速 */
HAL_GPIO_Init(GPIOB, &GPIO_InitType); /* SDA */
}
这里的iic引脚是PB4和PB5 如果需要修改可以进行引脚修改 改时钟 改引脚
delay.h中
#ifndef __DELAY_H
#define __DELAY_H
#include "main.h"
#define CPU_FREQUENCY_MHZ 168 // STM32时钟主频
void delay_us1(__IO uint32_t delay);
#endif
对应着自己系列的主频进行修改
主函数
#include "main.h"
#include "gpio.h"
#include "oled.h"记得添加这个头文件
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
/* USER CODE BEGIN 2 */
OLED_Init(); 初始化
OLED_ShowChar(0,0,'B',OLED_8X16); 显示字符
OLED_ShowChinese(0,16,"世界");显示汉字
OLED_Update(); 更新oled
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
oled即可正常使用
注意 OLED_ShowChar(0,0,‘B’,OLED_8X16); 最后一个参数是oled 显示汉字数字的大小 字体的大小
2.显示中文
在红线上添加 --no-multibyte-chars
首先需要在添加需要的汉字字模
取模软件的设置如图所示
注意对照
得到对应的字模后到 OLED_Data.c文件中的
const ChineseCell_t OLED_CF16x16[] 这个数组中添加字模
const ChineseCell_t OLED_CF16x16[] = {
",",
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x58,0x38,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,
0x00,0x00,0x18,0x24,0x24,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
"你",
0x00,0x80,0x60,0xF8,0x07,0x40,0x20,0x18,0x0F,0x08,0xC8,0x08,0x08,0x28,0x18,0x00,
0x01,0x00,0x00,0xFF,0x00,0x10,0x0C,0x03,0x40,0x80,0x7F,0x00,0x01,0x06,0x18,0x00,
"好",
0x10,0x10,0xF0,0x1F,0x10,0xF0,0x00,0x80,0x82,0x82,0xE2,0x92,0x8A,0x86,0x80,0x00,
0x40,0x22,0x15,0x08,0x16,0x61,0x00,0x00,0x40,0x80,0x7F,0x00,0x00,0x00,0x00,0x00,
"世",
0x20,0x20,0x20,0xFE,0x20,0x20,0xFF,0x20,0x20,0x20,0xFF,0x20,0x20,0x20,0x20,0x00,
0x00,0x00,0x00,0x7F,0x40,0x40,0x47,0x44,0x44,0x44,0x47,0x40,0x40,0x40,0x00,0x00,
"界",
0x00,0x00,0x00,0xFE,0x92,0x92,0x92,0xFE,0x92,0x92,0x92,0xFE,0x00,0x00,0x00,0x00,
0x08,0x08,0x04,0x84,0x62,0x1E,0x01,0x00,0x01,0xFE,0x02,0x04,0x04,0x08,0x08,0x00,
"啊",
0xFC,0x04,0xFC,0x00,0xFE,0x42,0xBE,0x00,0xF2,0x12,0xF2,0x02,0xFE,0x02,0x00,0x00,
0x0F,0x04,0x0F,0x00,0xFF,0x10,0x0F,0x00,0x0F,0x04,0x4F,0x80,0x7F,0x00,0x00,0x00,
"哈",
0x00,0xFC,0x04,0x04,0xFC,0x20,0x50,0x48,0x44,0x43,0x44,0x48,0x50,0x20,0x20,0x00,
0x00,0x0F,0x04,0x04,0x0F,0x00,0xFE,0x42,0x42,0x42,0x42,0x42,0xFE,0x00,0x00,0x00,/*0*/
/* (16 X 16 , 宋体 )*/
"喽",
0x00,0xFC,0x04,0x04,0xFC,0x00,0x90,0x52,0x34,0x10,0x7F,0x10,0x34,0x52,0x90,0x00,
0x00,0x0F,0x04,0x04,0x0F,0x82,0x82,0x5A,0x56,0x23,0x22,0x52,0x4E,0x82,0x02,0x00,/*1*/
/* (16 X 16 , 宋体 )*/
"大",
0x20,0x20,0x20,0x20,0x20,0x20,0x20,0xFF,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x00,
0x80,0x80,0x40,0x20,0x10,0x0C,0x03,0x00,0x03,0x0C,0x10,0x20,0x40,0x80,0x80,0x00,/*2*/
/* (16 X 16 , 宋体 )*/
"家",
0x10,0x2C,0x24,0x24,0xA4,0xA4,0x65,0xA6,0x24,0x24,0x24,0x24,0xA4,0x34,0x0C,0x00,
0x40,0x49,0x49,0x29,0x24,0x14,0x52,0x89,0x7F,0x04,0x0A,0x11,0x10,0x20,0x20,0x00,/*3*/
/* (16 X 16 , 宋体 )*/
"不",
0x00,0x02,0x02,0x02,0x02,0x82,0x42,0xF2,0x0E,0x42,0x82,0x02,0x02,0x02,0x00,0x00,
0x10,0x08,0x04,0x02,0x01,0x00,0x00,0xFF,0x00,0x00,0x00,0x01,0x02,0x0C,0x00,0x00,/*4*/
/* (16 X 16 , 宋体 )*/
"好",
0x10,0x10,0xF0,0x1F,0x10,0xF0,0x00,0x80,0x82,0x82,0xE2,0x92,0x8A,0x86,0x80,0x00,
0x40,0x22,0x15,0x08,0x16,0x61,0x00,0x00,0x40,0x80,0x7F,0x00,0x00,0x00,0x00,0x00,/*5*/
/* (16 X 16 , 宋体 )*/
/*按照上面的格式,在这个位置加入新的汉字数据*/
//...
/*未找到指定汉字时显示的默认图形(一个方框,内部一个问号),请确保其位于数组最末尾*/
"",
0xFF,0x01,0x01,0x01,0x31,0x09,0x09,0x09,0x09,0x89,0x71,0x01,0x01,0x01,0x01,0xFF,
0xFF,0x80,0x80,0x80,0x80,0x80,0x80,0x96,0x81,0x80,0x80,0x80,0x80,0x80,0x80,0xFF,
};
一定是要按照对应的格式