前言
理解OLED屏显和汉字点阵编码原理,使用STM32F103的SPI或IIC接口实现以下功能:
显示自己的学号和姓名;
显示AHT20的温度和湿度;
一、OLED介绍
1.OLED基础介绍
OLED(Organic Light-Emitting Diode),又称为有机电激光显示、有机发光半导体(Organic Electroluminescence Display,OLED)。OLED属于一种电流型的有机发光器件,是通过载流子的注入和复合而致发光的现象,发光强度与注入的电流成正比。OLED在电场的作用下,阳极产生的空穴和阴极产生的电子就会发生移动,分别向空穴传输层和电子传输层注入,迁移到发光层。当二者在发光层相遇时,产生能量激子,从而激发发光分子最终产生可见光。
OLED作为STM32的一个较为重要的外设,其作用也是为了方便调试代码。OLED模块的驱动可以使用8080、SPI四线、SPI3线、I2C的方法进行驱动。本文主要根据OLED的数据手册分析8080、SPI四线、I2C的使用。
2.OLED接线说明
首先是初始化过程,先确认接线,相比于8080串口接线方法,SPI四线极大的缩小了IO口的使用数量,更加便于使用。
OLED0.96 STM32
GND <-----------> GND
VCC <-----------> 3.3V
SCL <-----------> PE3
SDA <-----------> PE4
DC(Data/Command) <-----------> PE0
RST <-----------> PE2
void OLED_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_SetBits(GPIOE, GPIO_Pin_0 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4);
初始化完成后,也是需要进行判断,判断用户是打算写入数据还是写入命令:
//dat:要写入的数据/命令
//cmd:数据/命令标志 0,表示命令;1,表示数据;
void OLED_WR_Byte(u8 dat,u8 cmd)
{
u8 i;
if(cmd)
OLED_RS_Set();
else
OLED_RS_Clr();
for(i=0;i<8;i++)
{
OLED_SCLK_Clr();
if(dat&0x80)
OLED_SDIN_Set();
else
OLED_SDIN_Clr();
OLED_SCLK_Set();
dat<<=1;
}
OLED_RS_Set();
}
有了这个主要的函数之后,便可以对OLED进行配置:
OLED_RST_Set();
delay_us(100000);
OLED_RST_Clr();
delay_us(20);
OLED_RST_Set();
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(0x40, OLED_CMD);
OLED_WR_Byte(0xA1, OLED_CMD); //A0/A1:控制屏幕显示左右映射翻转的
OLED_WR_Byte(0xC0, OLED_CMD); //C0/C8:控制屏幕上下映射翻转的
OLED_WR_Byte(0xDA, OLED_CMD);
OLED_WR_Byte(0x12, OLED_CMD); //02h:隔行显示 12h:逐行显示
OLED_WR_Byte(0x81, OLED_CMD);
OLED_WR_Byte(0x7F, OLED_CMD);
OLED_WR_Byte(0xA4, OLED_CMD);
OLED_WR_Byte(0xA6, OLED_CMD);
OLED_WR_Byte(0xD5, OLED_CMD);
OLED_WR_Byte(0x80, OLED_CMD);
OLED_WR_Byte(0x8D, OLED_CMD);
OLED_WR_Byte(0x14, OLED_CMD);
OLED_WR_Byte(0xAF, OLED_CMD);
OLED_Clear();
二、程序实现
1.OLED和STM32最小系统板接线
打开取模软件,输入相关内容之后点击Ctrl+Enter,勾选C51格式后点阵生成
2.OLED操作程序
主界面显示函数:
TEST_MainPage(); //主界面显示测试
进入TEST_MainPage()函数修改需要显示的内容
void TEST_MainPage(void)
{
GUI_ShowCHinese(28,20,16,"李胜",1);
GUI_ShowString(4,48,"632107030230",16,1);
delay_ms(1500);
delay_ms(1500);
}
GUI_ShowCHinese(u8 x,u8 y,u8 hsize,u8 *str,u8 mode),其中(x,y)表示x行y列,hsize表示字符串的大小,str表示输入的汉字 ,mode表示输出模式,‘0’表示白色背景,‘1’表示黑色背景
该函数输出汉字通过调用函数GUI_ShowFont16(x,y,str,mode),在该函数中存储输出汉字的模:
typedef struct
{
unsigned char Index[2];
char Msk[32];
}typFNT_GB16;
//宋体
//16*16大小
//逐行式,顺向(高位在前)
const typFNT_GB16 cfont16[] =
{
"李",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,/*"李",10*/
"胜",0x01,0x00,0x01,0x00,0x01,0x00,0x3F,0xF8,0x21,0x08,0x21,0x08,0x3F,0xF8,0x21,0x08,
0x21,0x08,0x21,0x08,0xFF,0xFE,0x20,0x08,0x20,0x08,0x20,0x08,0x20,0x28,0x20,0x10,/*"胜",11*/
};
3.获取温湿度信息并显示
在之前的文章中详细介绍过温湿度传感器信息的获取,这里简单介绍:
AHT20芯片的使用过程 read_AHT20_once函数
void read_AHT20_once(void)
{
delay_ms(10);
reset_AHT20();//重置AHT20芯片
delay_ms(10);
init_AHT20();//初始化AHT20芯片
delay_ms(10);
startMeasure_AHT20();//开始测试AHT20芯片
delay_ms(80);
read_AHT20();//读取AHT20采集的到的数据
delay_ms(5);
}
AHT20芯片读取数据 read_AHT20函数
void read_AHT20(void)
{
uint8_t i;
for(i=0; i<6; i++)
{
readByte[i]=0;
}
I2C_Start();//I2C启动
I2C_WriteByte(0x71);//I2C写数据
ack_status = Receive_ACK();//收到的应答信息
readByte[0]= I2C_ReadByte();//I2C读取数据
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();//I2C停止函数
//判断读取到的第一个字节是不是0x08,0x08是该芯片读取流程中规定的,如果读取过程没有问题,就对读到的数据进行相应的处理
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("读取失败!!!");
}
printf("\r\n");
//根据AHT20芯片中,温度和湿度的计算公式,得到最终的结果,通过串口显示
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");
}
4.通过OLED显示温湿度
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("\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中
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);
}
读取“温湿度显示”的字模
温湿度显示:
GUI_ShowString(53,20,strTemp,16,1);
GUI_ShowString(53,38,strHumi,16,1);