御神楽的学习记录之SPI接口OLED屏显示温湿度

文章目录

  • 前言
  • 一、简单汉字显示
    • 1.添加汉字字模
    • 2.汉字显示代码
    • 3.汉字显示效果
  • 二、OLED显示温湿度
    • 1.主函数代码
    • 2.效果展示
  • 参考


前言

SPI串行外设接口(Serial Peripheral Interface)的缩写。SPI,是一种高速的全双工同步的通信总线,并且在芯片的管脚上只占用四根线,节约了芯片的管脚,同时为PCB的布局上节省空间,提供方便,正是出于这种简单易用的特性,如今越来越多的芯片集成了这种通信协议,比如AT91RM9200。


一、简单汉字显示

1.添加汉字字模

1、在文件oledfont.h中添加汉字字模

"御",0x14,0x00,0x14,0x00,0x27,0xDE,0x49,0x12,0x91,0x12,0x11,0x12,0x2F,0xD2,0x61,0x12,
	0xA1,0x12,0x25,0xD2,0x25,0x12,0x25,0x1A,0x25,0xD4,0x3E,0x10,0x28,0x10,0x20,0x10,
	"神",0x20,0x20,0x10,0x20,0x10,0x20,0xF9,0xFC,0x09,0x24,0x11,0x24,0x11,0x24,0x39,0xFC,
  0x55,0x24,0x95,0x24,0x11,0x24,0x11,0xFC,0x11,0x24,0x10,0x20,0x10,0x20,0x10,0x20,
  "楽",0x01,0x00,0x42,0x04,0x27,0xC8,0x14,0x50,0x04,0x40,0x17,0xD0,0x24,0x48,0xC4,0x44,
  0x07,0xC0,0x01,0x00,0xFF,0xFE,0x05,0x40,0x09,0x20,0x31,0x18,0xC1,0x06,0x01,0x00,
	"温",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*/
  "湿",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,/*"湿",1*/
  "度",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,/*"度",2*/
	"℃",0x60,0x00,0x91,0xF4,0x96,0x0C,0x6C,0x04,0x08,0x04,0x18,0x00,0x18,0x00,0x18,0x00,
  0x18,0x00,0x18,0x00,0x18,0x00,0x08,0x00,0x0C,0x04,0x06,0x08,0x01,0xF0,0x00,0x00,/*"℃",0*/

2.汉字显示代码

    GUI_ShowString(28,0,"YuKaguraNe",16,1);//英文姓名
	GUI_ShowCHinese(40,20,16,"御神楽",1);//中文姓名
	GUI_ShowString(20,48,"631807060417",16,1);//数字详细
		delay_ms(100);

函数原型
字符串显示

void GUI_ShowString(u8 x,u8 y,u8 *chr,u8 Char_Size,u8 mode);

汉字显示

void GUI_ShowCHinese(u8 x,u8 y,u8 hsize,u8 *str,u8 mode);

3.汉字显示效果

御神楽的学习记录之SPI接口OLED屏显示温湿度_第1张图片

二、OLED显示温湿度

温湿度代码参考:御神楽的学习记录之基于I2C协议的AHT20温湿度传感器的数据采集

1.主函数代码

#include "delay.h"
#include "sys.h"
#include "oled.h"
#include "gui.h"
#include "test.h"
#include "AHT20.h" 
uint32_t CT_data[2]={0,0};
volatile int  c1,t1;
u8 temp[10];  
u8 hum[10];
int main(void)
{	
	delay_init();	    	       //延时函数初始化	  
	OLED_Init();			         //初始化OLED  
	OLED_Clear(0);  	//清屏(全黑)
	AHT20_Init();
	Delay_1ms(500);
	if((AHT20_Read_Status()&0x18)!=0x18)
	{
		AHT20_Start_Init(); //重新初始化寄存器
		Delay_1ms(10);
	}
	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(0x02,OLED_CMD); //终止页 2

	OLED_WR_Byte(0x00,OLED_CMD); //虚拟字节

	OLED_WR_Byte(0xFF,OLED_CMD); //虚拟字节
	
	GUI_ShowCHinese(10,0,16,"御神楽",1);//中文姓名
		while(1){
		AHT20_Read_CTdata(CT_data);;  //crc校验后,读取AHT20的温度和湿度数据 
		c1 = CT_data[0]*1000/1024/1024;  //计算得到湿度值c1(放大了10倍)
		t1 = CT_data[1]*2000/1024/1024-500;//计算得到温度值t1(放大了10倍)


		temp[0]=t1/100+'0';
		temp[1]=(t1/10)%10+'0';
		temp[2]='.';
		temp[3]=t1%10+'0';
		temp[4]='\0';
		
		hum[0]=c1/100+'0';
		hum[1]=(c1/10)%10+'0';
		hum[2]='.';
		hum[3]=c1%10+'0';
		hum[4]=32;
		hum[5]='%';
		hum[6]='\0';
		
		GUI_ShowCHinese(15,28,16,"温度",1);
		GUI_ShowString(47,28,":",16,1);
		GUI_ShowString(62,28,temp,16,1);
		GUI_ShowCHinese(96,28,16,"℃",1);

		GUI_ShowCHinese(15,48,16,"湿度",1);
		GUI_ShowString(47,48,":",16,1);
		GUI_ShowString(62,48,hum,16,1);
		
		OLED_WR_Byte(0x2F,OLED_CMD); //开启滚动
		Delay_1ms(3135);
		
	}

2.效果展示


参考

https://blog.csdn.net/qq_47281915/article/details/121439706?spm=1001.2014.3001.5501

你可能感兴趣的:(嵌入式系统基础,stm32,单片机,c语言)