OpenVINOTM,给你看得见的未来!>>
电子产品世界 » 论坛首页 » 嵌入式开发 » STM32 » STM32G070RB探测16-OLED显示

共6条 1/1 1 跳转至

STM32G070RB探测16-OLED显示

高工
2021-01-07 00:11:35    评分

上篇稍微介绍了我画的扩展板,今天主要对上边的一个模块进行操作,就是这个OLED模块,就是使用I2C驱动0.96’OLED屏幕,因为不习惯用ST的硬件IIC,所以我直接是用了软件模拟IIC配置IIC端口

添加库文件

我们这里直接使用现成的库,因为是模拟IIC,所以只用修改GPIO驱动即可(驱动在文章末尾可以下载)

我们需要4个文件oled.c,oled.h,bmp.h,oledfont.h oled.c:IIC驱动,OLED初始化函数以及显示函数 oled.h:GPIO操作的宏定义 bmp.h:图片取的模 oledfont.h:字模

修改GPIO驱动

#define OLED_SCLK_Clr() HAL_GPIO_WritePin(SCL_GPIO_Port,SCL_Pin,GPIO_PIN_RESET) //SCL
#define OLED_SCLK_Set() HAL_GPIO_WritePin(SCL_GPIO_Port,SCL_Pin,GPIO_PIN_SET)

#define OLED_SDIN_Clr() HAL_GPIO_WritePin(SDA_GPIO_Port,SDA_Pin,GPIO_PIN_RESET); //SDA
#define OLED_SDIN_Set() HAL_GPIO_WritePin(SDA_GPIO_Port,SDA_Pin,GPIO_PIN_SET)

编写函数

首先main函数中以用头文件

#include "oled.h"
#include "bmp.h"

然后初始化OLED屏,添加一个变量和一个数组,让OLED显示变量

OLED_Init();
uint8_t num = 0;
uint8_t buf[3];

然后while(1)中编写程序

num++;
OLED_Fill(0x00);
OLED_ShowCHinese(0u,0u,0);
OLED_ShowCHinese(16u,0u,1);
OLED_ShowCHinese(32u,0u,2);
OLED_ShowCHinese(48u,0u,3);
OLED_ShowCHinese(64u,0u,4);
OLED_ShowCHinese(80u,0u,5);
OLED_P8x16Str(16u,2u,(uint8_t *)"EEPW & STM32");
OLED_P8x16Str(0u,4u,(uint8_t *)"variable:");
sprintf(buf,"%d",num);
OLED_P8x16Str(73u,4u,(uint8_t *)buf);
HAL_Delay(5000);
OLED_Fill(0x00);
Draw_BMP(0u,0u,128u,8u,BMP1);
HAL_Delay(5000);

效果如下

下边写一下如何取字模

首先到网上下载这个软件

下载打开

修改选项

点生成字模,生成的字模复制到oledfont.h中Hzk数组里

char Hzk[][32]={
{0x00,0x00,0xF8,0x88,0x88,0x88,0x88,0xFF,0x88,0x88,0x88,0x88,0xF8,0x00,0x00,0x00},
{0x00,0x00,0x1F,0x08,0x08,0x08,0x08,0x7F,0x88,0x88,0x88,0x88,0x9F,0x80,0xF0,0x00},/*"电",0*/
/* (16 X 16 , 宋体 )*/

{0x80,0x82,0x82,0x82,0x82,0x82,0x82,0xE2,0xA2,0x92,0x8A,0x86,0x82,0x80,0x80,0x00},
{0x00,0x00,0x00,0x00,0x00,0x40,0x80,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"子",1*/
/* (16 X 16 , 宋体 )*/

{0x00,0x04,0x84,0x84,0x94,0xE4,0x85,0x86,0x84,0xC4,0xB4,0x84,0x84,0x84,0x80,0x00},
{0x80,0x60,0x1F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"产",2*/
/* (16 X 16 , 宋体 )*/

{0x00,0x00,0x00,0x7E,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x7E,0x00,0x00,0x00,0x00},
{0x00,0xFE,0x42,0x42,0x42,0xFE,0x00,0x00,0x00,0xFE,0x42,0x42,0x42,0xFE,0x00,0x00},/*"品",3*/
/* (16 X 16 , 宋体 )*/

{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},/*"世",4*/
/* (16 X 16 , 宋体 )*/

{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},/*"界",5*/
/* (16 X 16 , 宋体 )*/
};

然后按顺序显示即可

OLED_ShowCHinese(0u,0u,0);
OLED_ShowCHinese(16u,0u,1);
OLED_ShowCHinese(32u,0u,2);
OLED_ShowCHinese(48u,0u,3);
OLED_ShowCHinese(64u,0u,4);
OLED_ShowCHinese(80u,0u,5);

 oled.zip



高工
2021-01-08 07:10:21    评分
2楼

谢谢分享!


助工
2021-01-08 18:57:29    评分
3楼

感谢分享


工程师
2021-01-10 21:13:35    评分
4楼

代码写的非常不错哦


工程师
2021-01-12 22:15:26    评分
5楼

是个不错的器件


工程师
2021-01-12 22:56:32    评分
6楼

学到了 确实不错的


共6条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]