OLED屏显示和汉字点阵编码原理

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • 一、认识I2C协议
  • 二、基于IIC协议在OLED屏上实现显示相关信息
    • 1、显示自己的学号和姓名
    • 2、显示AHT20的温度和湿度
    • 3、显示一段诗词


前言

提示:这里可以添加本文要记录的大概内容:

例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。


提示:以下是本篇文章正文内容,下面案例可供参考

一、认识I2C协议

参考博客

二、基于IIC协议在OLED屏上实现显示相关信息

所使用的工具:
Keil、字模软件

所需函数内容:

void GUI_DrawPoint(u8 x, u8 y, u8 color);
void GUI_Fill(u8 sx,u8 sy,u8 ex,u8 ey,u8 color);
void GUI_DrawLine(u8 x1, u8 y1, u8 x2, u8 y2,u8 color);
void GUI_DrawRectangle(u8 x1, u8 y1, u8 x2, u8 y2,u8 color);
void GUI_FillRectangle(u8 x1, u8 y1, u8 x2, u8 y2,u8 color);
void GUI_DrawCircle(u8 xc, u8 yc, u8 color, u8 r);
void GUI_FillCircle(u8 xc, u8 yc, u8 color, u8 r);
void GUI_DrawTriangel(u8 x0,u8 y0,u8 x1,u8 y1,u8 x2,u8 y2,u8 color);
void GUI_FillTriangel(u8 x0,u8 y0,u8 x1,u8 y1,u8 x2,u8 y2,u8 color);
void GUI_ShowChar(u8 x,u8 y,u8 chr,u8 Char_Size,u8 mode);
void GUI_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 Size,u8 mode);
void GUI_ShowString(u8 x,u8 y,u8 *chr,u8 Char_Size,u8 mode);
void GUI_ShowFont16(u8 x,u8 y,u8 *s,u8 mode);
void GUI_ShowFont24(u8 x,u8 y,u8 *s,u8 mode);
void GUI_ShowFont32(u8 x,u8 y,u8 *s,u8 mode);
void GUI_ShowCHinese(u8 x,u8 y,u8 hsize,u8 *str,u8 mode);
void GUI_DrawBMP(u8 x,u8 y,u8 width, u8 height, u8 BMP[], u8 mode); 

上述函数即可在通过GUI方式在OLED屏上显示相关内容

1、显示自己的学号和姓名

1、在oledfont.h函数中添加所要输出汉字的字模(使用上述工具即可知道所要汉字字模):

oledfont.h
"周",0x00,0x00,0x3F,0xF8,0x21,0x08,0x21,0x08,0x2F,0xE8,0x21,0x08,0x21,0x08,0x3F,0xF8,
	0x20,0x08,0x27,0xC8,0x24,0x48,0x24,0x48,0x27,0xC8,0x40,0x08,0x40,0x28,0x80,0x10,/*"周",0*/
	"经",0x10,0x00,0x11,0xFC,0x20,0x08,0x24,0x10,0x44,0x30,0xF8,0x48,0x10,0x84,0x23,0x02,
	0x40,0x00,0xFD,0xFC,0x40,0x20,0x00,0x20,0x1C,0x20,0xE0,0x20,0x43,0xFE,0x00,0x00,/*"经",0*/
	"涛",0x00,0x40,0x20,0x40,0x17,0xFC,0x10,0x40,0x83,0xF8,0x40,0x80,0x47,0xFE,0x10,0x90,
	0x11,0x10,0x21,0xFE,0xE2,0x10,0x22,0x90,0x24,0x50,0x28,0x10,0x20,0x50,0x00,0x20,/*"涛",0*/

2、在主函数中添加如下代码以实现在OLED显示所要输出字符:

main.c
int main()
{
	GUI_ShowString(0,0,"632007060222",16,1);
	GUI_ShowString(0,16,"周经涛",16,1);
}

效果展示:

利用OLED显示自己的学号和姓名

2、显示AHT20的温度和湿度

1、在oledfont.h函数中添加所要输出汉字的字模:

oledfont.h
"温",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,/*"温",1*/
	"湿",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,/*"湿",2*/
	"度",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,/*"度",3*/
	"传",0x08,0x40,0x08,0x40,0x08,0x40,0x13,0xF8,0x10,0x40,0x30,0x80,0x37,0xFE,0x50,0x80,
	0x91,0x00,0x13,0xF8,0x10,0x08,0x11,0x10,0x10,0xA0,0x10,0x40,0x10,0x20,0x10,0x20,/*"传",4*/
	"感",0x00,0x28,0x00,0x24,0x3F,0xFE,0x20,0x20,0x2F,0xA4,0x20,0x24,0x2F,0xA8,0x28,0x98,
	0x28,0x92,0x4F,0xAA,0x40,0x46,0x80,0x82,0x01,0x00,0x48,0x84,0x48,0x12,0x87,0xF2,/*"感",5*/
	"器",0x00,0x00,0x3E,0x7C,0x22,0x44,0x22,0x44,0x3E,0x7C,0x01,0x20,0x01,0x10,0xFF,0xFE,
	0x02,0x80,0x0C,0x60,0x30,0x18,0xC0,0x06,0x3E,0x7C,0x22,0x44,0x22,0x44,0x3E,0x7C,/*"器",6*/
	":",0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x30,0x00,0x30,0x00,0x00,0x00,0x30,0x00,0x30,0x00,0x00,0x00,0x00,0x00,/*":",0*/

2、在main.c中添加如下代码,实现AHT20温湿度和OLED的桥接:

main.c
int main(void)
{
    uint32_t CT_data[2];
	volatile int  c1,t1;
	float c,t;
	/***********************************************************************************/
	/**///上电初始化SDASCLIO/***********************************************************************************/
	Init_I2C_Sensor_Port();
	/***********************************************************************************/
	/**///①刚上电,产品芯片内部就绪需要时间,延时100~500ms,建议500ms
	/***********************************************************************************/
	Delay_1ms(500);
	/***********************************************************************************/
	/**///②上电第一次发0x71读取状态字,判断状态字是否为0x18,如果不是0x18,进行寄存器初始化
	/***********************************************************************************/
	if((AHT20_Read_Status()&0x18)!=0x18)
	{
	AHT20_Start_Init(); //重新初始化寄存器
	Delay_1ms(10);
	}
	Serial_Init();
	
	delay_init();	    	       //延时函数初始化	  
	NVIC_Configuration(); 	   //设置NVIC中断分组2:2位抢占优先级,2位响应优先级 	
	OLED_Init();			         //初始化OLED  
	OLED_Clear(0);             //清屏(全黑)
	/***********************************************************************************/
	/**///③根据客户自己需求发测量命令读取温湿度数据,当前while1)循环发测量命令读取温湿度数据,仅供参考
	/***********************************************************************************/
	while(1)
	{
		// AHT20_Read_CTdata(CT_data);       //不经过CRC校验,直接读取AHT20的温度和湿度数据    推荐每隔大于1S读一次
		AHT20_Read_CTdata_crc(CT_data);  //crc校验后,读取AHT20的温度和湿度数据 
		

		 c1 = CT_data[0]*100*10/1024/1024;  //计算得到湿度值c1(放大了10倍)
		 t1 = CT_data[1]*200*10/1024/1024-500;//计算得到温度值t1(放大了10倍)
		 c=c1/10.0;
		 t=t1/10.0;
		
		下一步客户处理显示数据,
		/***********************************************************************************/
		GUI_ShowCHinese(0,0,16,"温湿度传感器",1);
		GUI_ShowCHinese(0,17,16,"温度:",1);
		//GUI_ShowString(34,17,":",16,1);
		GUI_ShowNum(50,17,c,2,16,1);
		GUI_ShowCHinese(0,35,16,"湿度:",1);
		//GUI_ShowString(34,35,":",16,1);
		GUI_ShowNum(50,35,t,2,16,1);
		
	 }

 }	

效果展示:

利用OLED显示AHT20的温度和湿度

3、显示一段诗词

1、控制字符滚动的函数:

oled.c
void OLED_WR_Byte(unsigned dat,unsigned cmd)
{
	if(cmd)
	{
		OLED_DC_Set();
	}
	else
	{
		OLED_DC_Clr();
	}
	OLED_CS_Clr();
	SPI_WriteByte(SPI2,dat);
	OLED_CS_Set();
}

2、在oledfont.h函数中添加所要输出汉字的字模:

oledfont.h
"先",0x01,0x00,0x11,0x00,0x11,0x00,0x1F,0xF8,0x21,0x00,0x41,0x00,0x01,0x00,0xFF,0xFE,
	0x04,0x40,0x04,0x40,0x04,0x40,0x08,0x40,0x08,0x42,0x10,0x42,0x20,0x3E,0xC0,0x00,/*"先",0*/
	"行",0x08,0x00,0x09,0xFC,0x10,0x00,0x20,0x00,0x48,0x00,0x08,0x00,0x13,0xFE,0x30,0x20,
	0x50,0x20,0x90,0x20,0x10,0x20,0x10,0x20,0x10,0x20,0x10,0x20,0x10,0xA0,0x10,0x40,/*"行",0*/
	"其",0x08,0x20,0x08,0x20,0x7F,0xFC,0x08,0x20,0x08,0x20,0x0F,0xE0,0x08,0x20,0x08,0x20,
	0x0F,0xE0,0x08,0x20,0x08,0x20,0xFF,0xFE,0x00,0x00,0x08,0x20,0x10,0x10,0x20,0x08,/*"其",0*/
	"言",0x02,0x00,0x01,0x00,0xFF,0xFE,0x00,0x00,0x00,0x00,0x3F,0xF8,0x00,0x00,0x00,0x00,
	0x3F,0xF8,0x00,0x00,0x00,0x00,0x3F,0xF8,0x20,0x08,0x20,0x08,0x3F,0xF8,0x20,0x08,/*"言",0*/
	"而",0x00,0x00,0x7F,0xFC,0x01,0x00,0x01,0x00,0x02,0x00,0x3F,0xF8,0x24,0x88,0x24,0x88,
	0x24,0x88,0x24,0x88,0x24,0x88,0x24,0x88,0x24,0x88,0x24,0x88,0x20,0x28,0x20,0x10,/*"而",0*/
	"后",0x00,0x10,0x00,0xF8,0x1F,0x00,0x10,0x00,0x10,0x00,0x1F,0xFE,0x10,0x00,0x10,0x00,
	0x10,0x00,0x17,0xF8,0x14,0x08,0x24,0x08,0x24,0x08,0x44,0x08,0x87,0xF8,0x04,0x08,/*"后",0*/
	"从",0x08,0x20,0x08,0x20,0x08,0x20,0x08,0x20,0x08,0x20,0x08,0x20,0x08,0x20,0x08,0x20,
	0x08,0x50,0x14,0x50,0x12,0x50,0x12,0x88,0x20,0x88,0x21,0x04,0x42,0x04,0x84,0x02,/*"从",0*/
	"之",0x02,0x00,0x01,0x00,0x01,0x00,0x00,0x00,0x7F,0xF8,0x00,0x10,0x00,0x20,0x00,0x40,
	0x00,0x80,0x01,0x00,0x02,0x00,0x04,0x00,0x18,0x00,0x24,0x00,0x43,0xFE,0x00,0x00,/*"之",0*/

3、在main.c中写入以下代码:

main.c
//All rights reserved
/****************************************************************************************************
//=========================================电源接线================================================//
// OLED模块               STM32单片机
//   VCC         接       DC 5V/3.3V      //OLED屏电源正
//   GND         接          GND          //OLED屏电源地
//=======================================液晶屏数据线接线==========================================//
//本模块默认数据总线类型为4线制SPI
// OLED模块               STM32单片机
//   D1          接          PB15        //OLED屏SPI写信号
//=======================================液晶屏控制线接线==========================================//
// OLED模块               STM32单片机
//   CS          接          PB11        //OLED屏片选控制信号
//   RES         接          PB12        //OLED屏复位控制信号
//   DC          接          PB10        //OLED屏数据/命令选择控制信号
//   D0          接          PB13        //OLED屏SPI时钟信号
//=========================================触摸屏接线=========================================//
//本模块不带触摸功能,所以不需要触摸屏接线
****************************************************************************************************/	
/***************************************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, QD electronic SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
****************************************************************************************************/	
#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);        //开启滚动
	
}

效果展示:

利用OLED显示一段诗词

你可能感兴趣的:(嵌入式硬件,stm32,开发语言)