OLED(Organic Light Emitting Display,中文名有机发光显示器)是指有机半导体材料和发光材料在电场驱动下,通过载流子注入和复合导致发光的现象。其原理是用ITO透明电极和金属电极分别作为器件的阳极和阴极,在一定电压驱动下,电子和空穴分别从阴极和阳极注入到电子和空穴传输层,电子和空穴分别经过电子和空穴传输层迁移到发光层,并在发光层中相遇,形成激子并使发光分子激发,后者经过辐射弛豫而发出可见光。
上文是单个像素点发光的原理,此次使用的0.96寸OLED屏幕分辨率为128*64。
供电兼容3.3v 和5v
1、芯片: STM32F103C8T6系统最小板
2、0.96寸OLED显示屏
3、IDE: MDK-Keil软件
4、烧录软件:FlyMcu
5、温湿度传感器
此次使用的0.96寸OLED显示屏商家介绍界面:
http://www.lcdwiki.com/zh/0.96inch_SPI_OLED_Module
可以在以上链接中下载stm32示例程序。
OLED实物图:
引脚详情:
依据商家提供的STM32示例程序,看懂下面的几个函数后,再根据自己的需求修改内容。
主要函数如下:
需要理解函数中几个参数所代表的含义。
这里给出GUI_ShowCHinese函数的参数解释内容。
x:the bebinning x coordinate of the Chinese strings
y:the bebinning y coordinate of the Chinese strings
size:the size of Chinese strings
str:the start address of the Chinese strings
mode:0-white background and black character
1-black background and white character
void OLED_Init(void);//初始化OLED
void OLED_ON(void);//唤醒OLED
void OLED_OFF(void);//OLED休眠
void OLED_Refresh_Gram(void);//更新显存到OLED
void OLED_Clear(void);//清屏
void OLED_DrawPoint(u8 x,u8 y,u8 t);//画点
void OLED_Fill(u8 x1,u8 y1,u8 x2,u8 y2,u8 dot);//填充
void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 size,u8 mode);//显示字符
void OLED_ShowString(u8 x,u8 y,const u8 *p,u8 size);//显示字符串
void GUI_ShowCHinese(u8 x,u8 y,u8 hsize,u8 *str,u8 mode);//显示汉字
main.c
int main(void)
{
delay_init(); //延时函数初始化
OLED_Init(); //初始化OLED
OLED_Clear(0); //清屏(全黑)
while(1)
{
TEST_MainPage(); //界面显示
}
}
void TEST_MainPage(void)// 显示的内容函数,在此次修改
{
//GUI_ShowString(28,0,"Vers",16,1);
GUI_ShowCHinese(28,20,16,"陈渝",1);
//GUI_ShowString(40,32,"64X128",16,1);
GUI_ShowString(4,48,"632007060534",16,1);
//GUI_ShowString(4,48,"www.lcdwiki.com",16,1);
delay_ms(1500);
delay_ms(1500);
}
OLED屏幕是以像素点的形式输出数据的,所谓字模也就是此数据在像素点上的表现形式(显示此字符的具体像素点形式)。
显示字符和字母数字,此程序中自带了常用符号的字模,但是想要显示汉字,就需要去自己在代码中添加相应汉字的字模。
这里给一个取字模的网站:
https://www.zhetao.com/fontarray.html
字模放在文件夹USER的gui.c->oledfont.h,结构体typFNT_GB16中。
编译后无错误的话,烧录到STM32芯片上。
和上文的显示固定数据差不多,需要添加滑动命令。
水平向左向右滑动:
OLED_WR_Byte(0x2E,OLED_CMD); //关闭滚动
OLED_WR_Byte(0x26,OLED_CMD); //水平向左或者右滚动 26/27
OLED_WR_Byte(0x00,OLED_CMD); //虚拟字节
OLED_WR_Byte(0x00,OLED_CMD); //起始页 0
OLED_WR_Byte(0x07,OLED_CMD); //滚动时间间隔
OLED_WR_Byte(0x07,OLED_CMD); //终止页 7
OLED_WR_Byte(0x00,OLED_CMD); //虚拟字节
OLED_WR_Byte(0xFF,OLED_CMD); //虚拟字节
OLED_WR_Byte(0x2F,OLED_CMD); //开启滚动
竖直向上向下滑动:
OLED_WR_Byte(0x2e,OLED_CMD); //关闭滚动
OLED_WR_Byte(0x29,OLED_CMD); //水平垂直和水平滚动左右 29/2a
OLED_WR_Byte(0x00,OLED_CMD); //虚拟字节
OLED_WR_Byte(0x00,OLED_CMD); //起始页 0
OLED_WR_Byte(0x07,OLED_CMD); //滚动时间间隔
OLED_WR_Byte(0x07,OLED_CMD); //终止页 1
OLED_WR_Byte(0x01,OLED_CMD); //垂直滚动偏移量
OLED_WR_Byte(0x2F,OLED_CMD); //开启滚动
如果显示的数据中有汉字,就和上文一样取字模,然后添加到代码中。
我这里显示的是英文字符,不需要取字模。
main.c函数:
#include "delay.h"
#include "sys.h"
#include "oled.h"
#include "gui.h"
#include "test.h"
int main(void)
{
delay_init(); //延时函数初始化
NVIC_Configuration(); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
OLED_Init(); //初始化OLED
OLED_Clear(0); //清屏(全黑)
OLED_WR_Byte(0x2E,OLED_CMD); //关闭滚动
OLED_WR_Byte(0x27,OLED_CMD); //水平向左或者右滚动 26/27
OLED_WR_Byte(0x00,OLED_CMD); //虚拟字节
OLED_WR_Byte(0x00,OLED_CMD); //起始页 0
OLED_WR_Byte(0x07,OLED_CMD); //滚动时间间隔
OLED_WR_Byte(0x07,OLED_CMD); //终止页 7
OLED_WR_Byte(0x00,OLED_CMD); //虚拟字节
OLED_WR_Byte(0xFF,OLED_CMD); //虚拟字节
TEST_MainPage();
OLED_WR_Byte(0x2F,OLED_CMD); //开启滚动
}
void TEST_MainPage(void)
{
//GUI_ShowCHinese(28,20,16,"",1);
GUI_ShowString(0,0,"Do not go gentle into that good night.",16,1);
//GUI_ShowString(40,48,"Rage, rage against the dying of the light.",16,1);
//GUI_ShowString(4,48,"www.lcdwiki.com",16,1);
delay_ms(1500);
delay_ms(1500);
}
编译后无错误,烧录到stm32芯片中。
参考链接: https://blog.csdn.net/chenyu128/article/details/127938676?spm=1001.2014.3001.5502
基于之前的串口显示温湿度的代码,添加OLED显示的相关项目文件。
添加后的项目文件夹如下:
#include "delay.h"
#include "usart.h"
#include "bsp_i2c.h"
#include "sys.h"
#include "oled.h"
#include "gui.h"
#include "test.h"
int main(void)
{
delay_init(); //延时函数初始化
uart_init(115200);
IIC_Init();
NVIC_Configuration(); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
OLED_Init(); //初始化OLED
OLED_Clear(0);
while(1)
{
//printf("温度湿度显示");
read_AHT20_once();
OLED_Clear(0);
delay_ms(1500);
}
}
void read_AHT20(void)
{
uint8_t i;
for(i=0; i<6; i++)
{
readByte[i]=0;
}
//-------------
I2C_Start();
I2C_WriteByte(0x71);
ack_status = Receive_ACK();
readByte[0]= I2C_ReadByte();
Send_ACK();
readByte[1]= I2C_ReadByte();
Send_ACK();
readByte[2]= I2C_ReadByte();
Send_ACK();
readByte[3]= I2C_ReadByte();
Send_ACK();
readByte[4]= I2C_ReadByte();
Send_ACK();
readByte[5]= I2C_ReadByte();
SendNot_Ack();
//Send_ACK();
I2C_Stop();
//--------------
if( (readByte[0] & 0x68) == 0x08 )
{
H1 = readByte[1];
H1 = (H1<<8) | readByte[2];
H1 = (H1<<8) | readByte[3];
H1 = H1>>4;
H1 = (H1*1000)/1024/1024;
T1 = readByte[3];
T1 = T1 & 0x0000000F;
T1 = (T1<<8) | readByte[4];
T1 = (T1<<8) | readByte[5];
T1 = (T1*2000)/1024/1024 - 500;
AHT20_OutData[0] = (H1>>8) & 0x000000FF;
AHT20_OutData[1] = H1 & 0x000000FF;
AHT20_OutData[2] = (T1>>8) & 0x000000FF;
AHT20_OutData[3] = T1 & 0x000000FF;
}
else
{
AHT20_OutData[0] = 0xFF;
AHT20_OutData[1] = 0xFF;
AHT20_OutData[2] = 0xFF;
AHT20_OutData[3] = 0xFF;
printf("read fail");
}
printf("\r\n");
printf("温度:%d%d.%d",T1/100,(T1/10)%10,T1%10);
printf("湿度:%d%d.%d",H1/100,(H1/10)%10,H1%10);
printf("\r\n");
t=T1/10;
t1=T1%10;
a=(float)(t+t1*0.1);
h=H1/10;
h1=H1%10;
b=(float)(h+h1*0.1);
sprintf(strTemp,"%.1f",a); //调用Sprintf函数把DHT11的温度数据格式化到字符串数组变量strTemp中
sprintf(strHumi,"%.1f",b); //调用Sprintf函数把DHT11的湿度数据格式化到字符串数组变量strHumi中
//printf(strTemp);
//printf("/r/n");
GUI_ShowCHinese(16,00,16,"温湿度显示",1);
GUI_ShowCHinese(16,20,16,"温度",1);
GUI_ShowString(53,20,strTemp,16,1);
GUI_ShowString(90,20,"^C",16,1);
GUI_ShowCHinese(16,38,16,"湿度",1);
GUI_ShowString(53,38,strHumi,16,1);
GUI_ShowString(90,38,"%",16,1);
delay_ms(1500);
delay_ms(1500);
delay_ms(1500);
delay_ms(1500);
}
"温",0x00,0x00,0x23,0xF8,0x12,0x08,0x12,0x08,0x83,0xF8,0x42,0x08,0x42,0x08,0x13,0xF8,
0x10,0x00,0x27,0xFC,0xE4,0xA4,0x24,0xA4,0x24,0xA4,0x24,0xA4,0x2F,0xFE,0x00,0x00,/*"温",0*/
"度",0x01,0x00,0x00,0x80,0x3F,0xFE,0x22,0x20,0x22,0x20,0x3F,0xFC,0x22,0x20,0x22,0x20,
0x23,0xE0,0x20,0x00,0x2F,0xF0,0x24,0x10,0x42,0x20,0x41,0xC0,0x86,0x30,0x38,0x0E,/*"度",0*/
"湿",0x00,0x00,0x27,0xF8,0x14,0x08,0x14,0x08,0x87,0xF8,0x44,0x08,0x44,0x08,0x17,0xF8,
0x11,0x20,0x21,0x20,0xE9,0x24,0x25,0x28,0x23,0x30,0x21,0x20,0x2F,0xFE,0x00,0x00,/*"湿",0*/
"显",0x00,0x00,0x1F,0xF0,0x10,0x10,0x10,0x10,0x1F,0xF0,0x10,0x10,0x10,0x10,0x1F,0xF0,
0x04,0x40,0x44,0x44,0x24,0x44,0x14,0x48,0x14,0x50,0x04,0x40,0xFF,0xFE,0x00,0x00,/*"显",0*/
"示",0x00,0x00,0x3F,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFE,0x01,0x00,
0x01,0x00,0x11,0x10,0x11,0x08,0x21,0x04,0x41,0x02,0x81,0x02,0x05,0x00,0x02,0x00,/*"示",0*/
编译后无问题后烧录
总结:此次使用STM32芯片驱动OLED屏幕输出数据,虽然所用屏幕是最简单的0.96英寸双色OLED,但是也足够了解到我们日常生活中最常见屏幕的输出原理,实用性很强。
参考资料:https://blog.csdn.net/m0_49297422/article/details/121570274