概述
- 一、OLED简述
- 1.OLED模块简介
- 2.SSD1306控制芯片驱动
- 3.如何显示字符
- 4.字幕显示原理
- 二、 上下或左右的滑动显示长字符
- 三、 显示AHT20的温度和湿度
- 四、总结
一、OLED简述
1.OLED模块简介
OLED,即有机发光二极管(Organic Light-Emitting Diode),又称为有机电激光显示(Organic Electroluminesence Display, OELD)。OLED由于同时具备自发光,不需背光源、对比度高、厚度薄、视角广、反应速度快、可用于挠曲性面板、使用温度范围广、构造及制程较简单等优异之特性,被认为是下一代的平面显示器新兴应用技术。
LCD都需要背光,而OLED不需要,因为它是自发光的。这样同样的显示,OLED效果要来得好一些。OLED的尺寸难以大型化,但是分辨率确可以做到很高。
2.SSD1306控制芯片驱动
我们以STM32单片机为例,使用5个GPIO模拟SPI总线与SSD1306总线进行通讯。因为STM32与OLED屏的通讯只是单向数据传输,即只存在STM32向OLED屏发送数据,不涉及到读取返回数据,所以我们要实现显示功能之前,需要根据SPI写操作时序图,先实现一个向SSD1306写入一个字节的函数实现。
封装一个向SSD1306写入一个字节的函数,
函数原型为:
void OLED_WR_Byte(uint8_t dat,uint8_t cmd);
其中:
dat为待写入的数据或者命令;
cmd为数据或命令标志,0表示命令;1表示数据。
由上面SPI操作的时序图可知,IO引脚的时序如下所示:
①首选看发送的字节是命令还是数据,如果是命令,OLED_DC(D/C)引脚置0,即低电平;如果是数据,OLED_DC(D/C)引脚置1,即高电平。
if(cmd)
OLED_DC_Set();
else
OLED_DC_Clr();
②片选OLED_CS(CS)引脚置0,即拉低(硬件电路中CS引脚也可以直接与GND相连,这样就不用操作CS引脚了)。
OLED_CS_Clr();
③1个字节的数据有8位,将8位数据由高到低的顺序发出,数据要在OLED_SCLK(D0)引脚的上升沿之前设置好,所以要先将OLED_SCLK(D0)置低,然后将一位数据送至OLED_SDIN(D1)引脚,然后将OLED_SCLK(D0)置高,这样一位数据就发送出去了,如此循环8次,一个字节的数据就发送出去了。
for(i=0;i<8;i++)
{
OLED_SCLK_Clr();
if(dat&0x80)
OLED_SDIN_Set();
else
OLED_SDIN_Clr();
OLED_SCLK_Set();
dat<<=1;
}
④8位数据发送完毕之后,将OLED_CS(CS)引脚拉高,即置1,恢复至初始状态。
OLED_CS_Set();
OLED_CS_Set();
OLED_DC_Set();
3.如何显示字符
在初始化OLED之后,如何在屏幕上显示字符呢?
在这之前,先了解一下SSD1306内部的RAM;SSD1306内部有个SRAM称为GDDRAM(Graphic Display Data RAM),大小是128 × 64 bits,被分为8个 PAGE,用于单色128×64点阵显示,当我们往这个RAM写入数据,就会显示特定内容,如图:
上面’COL’的含义是GDDRAM列(column)
在页寻址模式下,每当对RAM进行读写操作之后,列地址指针会自动+1,直到列地址指针指向列结束地址后,会重新指向列开始地址;而且在该模式下,一定要设置新的页面和列地址才能访问下一个页面的内容,所以设置显示字符之前一般都会调用下面几句语句来确定显示位置:
OLED_WR_Byte (0xb0+i,OLED_CMD); //设置页地址(0~7)OLED_WR_Byte (0x00,OLED_CMD); //设置显示位置—列低地址OLED_WR_Byte (0x10,OLED_CMD); //设置显示位置—列高地址封装设置
显示位置的函数如下:
void OLED_Set_Pos(unsigned char x, unsigned char y)
{
OLED_WR_Byte(0xb0+y,OLED_CMD);
OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD); OLED_WR_Byte((x&0x0f)|0x01,OLED_CMD);
}
例如如果页地址设为B2h,低列地址为03h,而高列地址为00h,那么也就意味着开始列是PAGE2的SEG3,而RAM的获取指针定位到下图中。而输入的数据将会被写入列3的位置。
所以确定好了显示位置,直接输出字符对应的像素数据数组就好了。
4.字幕显示原理
字幕显示原理我们用的OLED液晶屏是由12864个发光点阵组成,即分辨率是12864。 ASCII码可以68、88、816像素,汉字一般显示像素为1616。 一般为了显示效果理想,英文和数字常选用8*16像素显示。
二、 上下或左右的滑动显示长字符
1.水平左右移动
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); //开启滚动
2.垂直水平滚动
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); //开启滚动
3.汉字取模
0x01,0x00,0x01,0x00,0x7F,0xFC,0x01,0x00,0x3F,0xF8,0x02,0x00,0xFF,0xFE,0x08,0x20,
0x10,0x10,0x2F,0xE8,0xC8,0x26,0x08,0x20,0x0F,0xE0,0x08,0x20,0x08,0x20,0x0F,0xE0,/*"春",0*/
0x00,0x00,0x3F,0xF0,0x20,0x10,0x20,0x10,0x28,0x50,0x24,0x50,0x22,0x90,0x22,0x90,
0x21,0x10,0x21,0x10,0x22,0x90,0x22,0x92,0x24,0x4A,0x48,0x4A,0x40,0x06,0x80,0x02,/*"风",1*/
0x10,0x90,0x10,0x90,0x10,0x90,0x12,0x92,0xFD,0x94,0x10,0x98,0x30,0x90,0x39,0x98,
0x56,0x94,0x54,0x92,0x90,0x90,0x10,0x90,0x11,0x12,0x11,0x12,0x12,0x12,0x14,0x0E,/*"桃",2*/
0x01,0x00,0x01,0x00,0x7F,0xFC,0x03,0x80,0x05,0x40,0x09,0x20,0x31,0x18,0xC1,0x06,
0x0F,0xE0,0x00,0x40,0x00,0x80,0xFF,0xFE,0x01,0x00,0x01,0x00,0x05,0x00,0x02,0x00,/*"李",3*/
0x08,0x20,0x08,0x20,0xFF,0xFE,0x08,0x20,0x00,0x00,0x08,0x88,0x08,0x90,0x10,0xA0,
0x30,0xC0,0x50,0x80,0x91,0x80,0x12,0x82,0x14,0x82,0x10,0x82,0x10,0x7E,0x10,0x00,/*"花",4*/
0x00,0x00,0x7F,0xFC,0x08,0x20,0x08,0x20,0x08,0x20,0x08,0x20,0x08,0x20,0xFF,0xFE,
0x08,0x20,0x08,0x20,0x08,0x20,0x08,0x20,0x10,0x20,0x10,0x20,0x20,0x20,0x40,0x20,/*"开",5*/
0x02,0x00,0x01,0x00,0xFF,0xFE,0x08,0x80,0x08,0x80,0x10,0xF8,0x11,0x08,0x31,0x48,
0x52,0x28,0x95,0x10,0x11,0x10,0x10,0xA0,0x10,0x40,0x10,0xA0,0x11,0x18,0x16,0x06,/*"夜",6*/
4.main函数
#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); //开启滚动
}
5.显示数据
void TEST_MainPage(void)
{
//GUI_ShowString(28,0,"Harriet",16,1);
GUI_ShowCHinese(10,20,16,"春风桃李花开夜",1);
//GUI_ShowString(40,32,"64X128",16,1);
delay_ms(1500);
delay_ms(1500);
}
6.运行结果
三、 显示AHT20的温度和湿度
1.显示温湿度代码
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("lyy");
}
/*通过串口显示采集得到的温湿度
printf("rn");
printf("温度:%d%d.%d",T1/100,(T1/10)%10,T1%10);
printf("湿度:%d%d.%d",H1/100,(H1/10)%10,H1%10);
printf("rn");*/
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中
GUI_ShowCHinese(16,00,16,"温湿度显示",1);
GUI_ShowCHinese(16,20,16,"温度",1);
GUI_ShowString(53,20,strTemp,16,1);
GUI_ShowCHinese(16,38,16,"湿度",1);
GUI_ShowString(53,38,strHumi,16,1);
delay_ms(1500);
delay_ms(1500);
}
2.添加点阵字
"温",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*/
3.main函数
#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);
}
}
4.运行结果
四、总结
参考链接
https://blog.csdn.net/qq_61682562/article/details/121561821
https://blog.csdn.net/qq_61682562/article/details/121561821
https://blog.csdn.net/qq_61682562/article/details/121529254
最后
以上就是会撒娇服饰为你收集整理的STM32驱动0.96寸OLED液晶屏一、OLED简述二、 上下或左右的滑动显示长字符三、 显示AHT20的温度和湿度四、总结的全部内容,希望文章能够帮你解决STM32驱动0.96寸OLED液晶屏一、OLED简述二、 上下或左右的滑动显示长字符三、 显示AHT20的温度和湿度四、总结所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复