• 通过STM32F103C8T6配置完成基于SPI协议的0.96OLED屏显



    前言


    一、SPI协议

    1、应用

    SPI 协议是由摩托罗拉公司提出的通讯协议(Serial Peripheral Interface),即串行外围设备接口,是一种高速全双工的通信总线。它被广泛地使用在 ADC、LCD 等设备与 MCU 间,要求通讯速率较高的场合。

    2、组成

    与I2C协议相同,把它分为物理层和协议层。

    - SPI通讯设备之间的常用连接方式
    在这里插入图片描述

    - 物理层
    SPI 通讯使用 3 条总线及片选线,3 条总线分别为 SCK、MOSI、MISO,片选线为 NSS。

    NSS :SPI 协议中没有设备地址,它使用 NSS 信号线来寻址,当主机要选择从设备时,把该从设备的 NSS 信号线设置为低电平,该从设备即被选中,即片选有效,接着主机开始与被选中的从设备进行 SPI通讯。
    SCK (Serial Clock):时钟信号线,用于通讯数据同步。
    MOSI (Master Output, Slave Input):主设备输出/从设备输入引脚。
    MISO(Master Input,,Slave Output):主设备输入/从设备输出引脚。

    - 协议层

    与 I2C 的类似,SPI 协议定义了通讯的起始和停止信号、数据有效性、时钟同步等环节。

    - STM32的SPI架构:
    在这里插入图片描述

    • 基本通讯过程
      在这里插入图片描述

    • 通讯的起始和停止信号

    NSS 信号线由高变低,是 SPI 通讯的起始信号。NSS 是每个从机各自独占的信号线,当从机在自己的 NSS 线检测到起始信号后,就知道自己被主机选中了,开始准备与主机通讯。NSS 信号由低变高,是 SPI 通讯的停止信号,表示本次通讯结束,从机的选中状态被取消。

    • 数据的有效性

    MOSI 及 MISO 的数据在 SCK 的上升沿期间变化输出,在SCK 的下降沿时被采样。即在 SCK 的下降沿时刻MOSI 及 MISO 的数据有效,高电平时表示数据“1”,为低电平时表示数据“0”。在其它时刻,数据无效,MOSI及 MISO 为下一次表示数据做准备。(这只是对上图情况的分析,上图并不是唯一的通讯模式。)

    • SPI的四种模式

    在这里插入图片描述

    这里知识简单回顾一下大致内容,详细见后面的参考资料。

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

    1、汉字点阵编码原理

    1、 在汉字的点阵字库中,每个字节的每个位都代表一个汉字的一个点,每个汉字都是由一个矩形的点阵组成,0代表没有,1代表有点,将0和1分别用不同颜色画出,就形成了一个汉字,常用的点阵矩阵有12x12, 14x14, 16x16三种字库。
    2、 字库根据字节所表示点的不同有分为横向矩阵和纵向矩阵,目前多数的字库都是横向矩阵的存储方式(用得最多的应该是早期UCDOS字库),纵向矩阵一般是因为有某些液晶是采用纵向扫描显示法,为了提高显示速度,于是便把字库矩阵做成纵向,省得在显示时还要做矩阵转换。我们接下去所描述的都是指横向矩阵字库。

    详细-------->汉字点阵原理

    2、OLED屏显

    OLED(Organic Light Emitting Display,中文名有机发光显示器)是指有机半导体材料和发光材料在电场驱动下,通过载流子注入和复合导致发光的现象。其原理是用ITO透明电极和金属电极分别作为器件的阳极和阴极,在一定电压驱动下,电子和空穴分别从阴极和阳极注入到电子和空穴传输层,电子和空穴分别经过电子和空穴传输层迁移到发光层,并在发光层中相遇,形成激子并使发光分子激发,后者经过辐射弛豫而发出可见光。

    来自------->百度百科

    三、OLED模块

    在这里插入图片描述
    在这里插入图片描述

    其他版本或详见:http://www.lcdwiki.com/zh/0.96inch_SPI_OLED_Module

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

    将此次实验所需要的demo下载下来。

    - 解压进入到工程目录下,双击文件打开keil进行代码修改
    在这里插入图片描述

    - 编译一次,这样.c文件下的文件才出来
    在这里插入图片描述

    - 通过取字模软件,取到相应的汉字。

    设置C51格式

    在这里插入图片描述
    在这里插入图片描述

    - 将取到的字模复制,放到gui.c下的oledfont.h

    在每一个取到的字模前面用英文的引号对真实的字修饰。

    在这里插入图片描述

    - 打开test.c,修改TEST_MainPage函数
    在这里插入图片描述

    GUI_ShowString() 的参数
    参数一:X 坐标
    参数二:Y 坐标
    参数三:字符串(ASCLL码中的)
    参数四:bit (表示字符显示格式,这里我用的 16 ,和汉字一样高)
    参数五:显示样式(1:白字黑底;0:黑字白底)
    GUI_ShowChinese() 的参数
    参数一:X 坐标
    参数二:Y 坐标
    参数三:汉字点阵大小(这里使用的是 16×16 的,参数应该是 16)
    参数四:要显示的汉字
    参数五:显示样式(1:白字黑底;0:黑字白底)

    - 修改main.c文件的main函数

    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);
    	}
    	while(1) 
    	{		
    		
              TEST_MainPage();
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    编译烧录均无问题。

    - 效果
    在这里插入图片描述

    五、显示温湿度&滑动显示长字符

    这是我上篇没用oled的串口输出温湿度:通过STM32Cube配置完成基于I2C协议的AHT20温湿度传感器的数据采集

    - 为了读取温湿度我们需要将温湿度传感器厂商(奥松AHT20)提供的文件进行配置,方便后续调用

    • 新建一个AHT20文件夹,在里面创建AHT20-21_DEMO_V1_3.c和AHT20-21_DEMO_V1_3.h两个空白文件。放到下面文件夹中:
      在这里插入图片描述
      2. 添加文件到项目中
      在这里插入图片描述
      3. 添加文件路径
      在这里插入图片描述

    • 写入AHT20-21_DEMO_V1_3.c文件

    /*******************************************/
    /*@??????У?????????????????          */
    /*@????????????????????                */
    /*@?汾??V1.2                              */
    /*******************************************/
    
    
    #include "AHT20-21_DEMO_V1_3.h" 
    
    
    
    void Delay_N10us(uint32_t t)//???????
    {
      uint32_t k;
    
       while(t--)
      {
        for (k = 0; k < 2; k++);//110
      }
    }
    
    void SensorDelay_us(uint32_t t)//???????
    {
    		
    	for(t = t-2; t>0; t--)
    	{
    		Delay_N10us(1);
    	}
    }
    
    void Delay_4us(void)		//???????
    {	
    	Delay_N10us(1);
    	Delay_N10us(1);
    	Delay_N10us(1);
    	Delay_N10us(1);
    }
    void Delay_5us(void)		//???????
    {	
    	Delay_N10us(1);
    	Delay_N10us(1);
    	Delay_N10us(1);
    	Delay_N10us(1);
    	Delay_N10us(1);
    
    }
    
    void Delay_1ms(uint32_t t)		//???????
    {
       while(t--)
      {
        SensorDelay_us(1000);//???1ms
      }
    }
    
    
    //void AHT20_Clock_Init(void)		//???????
    //{
    //	RCC_APB2PeriphClockCmd(CC_APB2Periph_GPIOB,ENABLE);
    //}
    
    void SDA_Pin_Output_High(void)   //??PB7???????? ?? ????????????? PB7???I2C??SDA
    {
    	GPIO_InitTypeDef  GPIO_InitStruct;
    	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;//???????
    	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
    	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    	GPIO_Init(GPIOB,& GPIO_InitStruct);
    	GPIO_SetBits(GPIOB,GPIO_Pin_7);
    }
    
    void SDA_Pin_Output_Low(void)  //??P7????????  ???????????
    {
    
    	GPIO_InitTypeDef  GPIO_InitStruct;
    	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;//???????
    	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
    	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    	GPIO_Init(GPIOB,& GPIO_InitStruct);
    	GPIO_ResetBits(GPIOB,GPIO_Pin_7);
    }
    
    void SDA_Pin_IN_FLOATING(void)  //SDA?????????????
    {
    
    	GPIO_InitTypeDef  GPIO_InitStruct;
    	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;//
    	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
    	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    	GPIO_Init( GPIOB,&GPIO_InitStruct);
    }
    
    void SCL_Pin_Output_High(void) //SCL?????????P6???I2C??SCL
    {
    	GPIO_SetBits(GPIOB,GPIO_Pin_6);
    }
    
    void SCL_Pin_Output_Low(void) //SCL???????
    {
    	GPIO_ResetBits(GPIOB,GPIO_Pin_6);
    }
    
    void Init_I2C_Sensor_Port(void) //?????I2C???,????????
    {	
    	GPIO_InitTypeDef  GPIO_InitStruct;
    	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;//???????
    	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
    	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    	GPIO_Init(GPIOB,& GPIO_InitStruct);
    	GPIO_SetBits(GPIOB,GPIO_Pin_15);//???????
    	
    	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;//???????
    	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
    	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    	GPIO_Init(GPIOB,& GPIO_InitStruct);
    	GPIO_SetBits(GPIOB,GPIO_Pin_15);//???????
    	
    }
    void I2C_Start(void)		 //I2C????????START???
    {
    	SDA_Pin_Output_High();
    	SensorDelay_us(8);
    	SCL_Pin_Output_High();
    	SensorDelay_us(8);
    	SDA_Pin_Output_Low();
    	SensorDelay_us(8);
    	SCL_Pin_Output_Low();
    	SensorDelay_us(8);   
    }
    
    
    void AHT20_WR_Byte(uint8_t Byte) //??AHT20д??????
    {
    	uint8_t Data,N,i;	
    	Data=Byte;
    	i = 0x80;
    	for(N=0;N<8;N++)
    	{
    		SCL_Pin_Output_Low(); 
    		Delay_4us();	
    		if(i&Data)
    		{
    			SDA_Pin_Output_High();
    		}
    		else
    		{
    			SDA_Pin_Output_Low();
    		}	
    			
        SCL_Pin_Output_High();
    		Delay_4us();
    		Data <<= 1;
    		 
    	}
    	SCL_Pin_Output_Low();
    	SensorDelay_us(8);   
    	SDA_Pin_IN_FLOATING();
    	SensorDelay_us(8);	
    }	
    
    
    uint8_t AHT20_RD_Byte(void)//??AHT20?????????
    {
    	uint8_t Byte,i,a;
    	Byte = 0;
    	SCL_Pin_Output_Low();
    	SDA_Pin_IN_FLOATING();
    	SensorDelay_us(8);	
    	for(i=0;i<8;i++)
    	{
        SCL_Pin_Output_High();		
    		Delay_5us();
    		a=0;
    		if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7)) a=1;
    		Byte = (Byte<<1)|a;
    		SCL_Pin_Output_Low();
    		Delay_5us();
    	}
      SDA_Pin_IN_FLOATING();
    	SensorDelay_us(8);	
    	return Byte;
    }
    
    
    uint8_t Receive_ACK(void)   //??AHT20????л??ACK
    {
    	uint16_t CNT;
    	CNT = 0;
    	SCL_Pin_Output_Low();	
    	SDA_Pin_IN_FLOATING();
    	SensorDelay_us(8);	
    	SCL_Pin_Output_High();	
    	SensorDelay_us(8);	
    	while((GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7))  && CNT < 100) 
    	CNT++;
    	if(CNT == 100)
    	{
    		return 0;
    	}
     	SCL_Pin_Output_Low();	
    	SensorDelay_us(8);	
    	return 1;
    }
    
    void Send_ACK(void)		  //???????ACK???
    {
    	SCL_Pin_Output_Low();	
    	SensorDelay_us(8);	
    	SDA_Pin_Output_Low();
    	SensorDelay_us(8);	
    	SCL_Pin_Output_High();	
    	SensorDelay_us(8);
    	SCL_Pin_Output_Low();	
    	SensorDelay_us(8);
    	SDA_Pin_IN_FLOATING();
    	SensorDelay_us(8);
    }
    
    void Send_NOT_ACK(void)	//?????????ACK
    {
    	SCL_Pin_Output_Low();	
    	SensorDelay_us(8);
    	SDA_Pin_Output_High();
    	SensorDelay_us(8);
    	SCL_Pin_Output_High();	
    	SensorDelay_us(8);		
    	SCL_Pin_Output_Low();	
    	SensorDelay_us(8);
        SDA_Pin_Output_Low();
    	SensorDelay_us(8);
    }
    
    void Stop_I2C(void)	  //???Э?????
    {
    	SDA_Pin_Output_Low();
    	SensorDelay_us(8);
    	SCL_Pin_Output_High();	
    	SensorDelay_us(8);
    	SDA_Pin_Output_High();
    	SensorDelay_us(8);
    }
    
    uint8_t AHT20_Read_Status(void)//???AHT20?????????
    {
    
    	uint8_t Byte_first;	
    	I2C_Start();
    	AHT20_WR_Byte(0x71);
    	Receive_ACK();
    	Byte_first = AHT20_RD_Byte();
    	Send_NOT_ACK();
    	Stop_I2C();
    	return Byte_first;
    }
    
    uint8_t AHT20_Read_Cal_Enable(void)  //???cal enableλ????????
    {
    	uint8_t val = 0;//ret = 0,
      val = AHT20_Read_Status();
    	 if((val & 0x68)==0x08)
    		 return 1;
       else  return 0;
     }
    
    void AHT20_SendAC(void) //??AHT20????AC????
    {
    
    	I2C_Start();
    	AHT20_WR_Byte(0x70);
    	Receive_ACK();
    	AHT20_WR_Byte(0xac);//0xAC???????
    	Receive_ACK();
    	AHT20_WR_Byte(0x33);
    	Receive_ACK();
    	AHT20_WR_Byte(0x00);
    	Receive_ACK();
    	Stop_I2C();
    
    }
    
    //CRCУ???????CRC8/MAXIM
    //???????X8+X5+X4+1
    //Poly??0011 0001  0x31
    //??λ?????????? 1000 1100 0x8c
    //C???????
    uint8_t Calc_CRC8(uint8_t *message,uint8_t Num)
    {
            uint8_t i;
            uint8_t byte;
            uint8_t crc=0xFF;
      for(byte=0; byte<Num; byte++)
      {
        crc^=(message[byte]);
        for(i=8;i>0;--i)
        {
          if(crc&0x80) crc=(crc<<1)^0x31;
          else crc=(crc<<1);
        }
      }
            return crc;
    }
    
    void AHT20_Read_CTdata(uint32_t *ct) //???CRCУ?飬?????AHT20?????????????
    {
    	volatile uint8_t  Byte_1th=0;
    	volatile uint8_t  Byte_2th=0;
    	volatile uint8_t  Byte_3th=0;
    	volatile uint8_t  Byte_4th=0;
    	volatile uint8_t  Byte_5th=0;
    	volatile uint8_t  Byte_6th=0;
    	 uint32_t RetuData = 0;
    	uint16_t cnt = 0;
    	AHT20_SendAC();//??AHT10????AC????
    	Delay_1ms(80);//???80ms????	
        cnt = 0;
    	while(((AHT20_Read_Status()&0x80)==0x80))//?????bit[7]?0?????????????????1????????
    	{
    		SensorDelay_us(1508);
    		if(cnt++>=100)
    		{
    		 break;
    		 }
    	}
    	I2C_Start();
    	AHT20_WR_Byte(0x71);
    	Receive_ACK();
    	Byte_1th = AHT20_RD_Byte();//?????????????0x98,?????????bit[7]?1?????0x1C??????0x0C??????0x08????????????bit[7]?0
    	Send_ACK();
    	Byte_2th = AHT20_RD_Byte();//???
    	Send_ACK();
    	Byte_3th = AHT20_RD_Byte();//???
    	Send_ACK();
    	Byte_4th = AHT20_RD_Byte();//???/???
    	Send_ACK();
    	Byte_5th = AHT20_RD_Byte();//???
    	Send_ACK();
    	Byte_6th = AHT20_RD_Byte();//???
    	Send_NOT_ACK();
    	Stop_I2C();
    
    	RetuData = (RetuData|Byte_2th)<<8;
    	RetuData = (RetuData|Byte_3th)<<8;
    	RetuData = (RetuData|Byte_4th);
    	RetuData =RetuData >>4;
    	ct[0] = RetuData;//???
    	RetuData = 0;
    	RetuData = (RetuData|Byte_4th)<<8;
    	RetuData = (RetuData|Byte_5th)<<8;
    	RetuData = (RetuData|Byte_6th);
    	RetuData = RetuData&0xfffff;
    	ct[1] =RetuData; //???
    
    }
    
    
    void AHT20_Read_CTdata_crc(uint32_t *ct) //CRCУ??????AHT20?????????????
    {
    	volatile uint8_t  Byte_1th=0;
    	volatile uint8_t  Byte_2th=0;
    	volatile uint8_t  Byte_3th=0;
    	volatile uint8_t  Byte_4th=0;
    	volatile uint8_t  Byte_5th=0;
    	volatile uint8_t  Byte_6th=0;
    	volatile uint8_t  Byte_7th=0;
    	 uint32_t RetuData = 0;
    	 uint16_t cnt = 0;
    	// uint8_t  CRCDATA=0;
    	 uint8_t  CTDATA[6]={0};//????CRC????????
    	
    	AHT20_SendAC();//??AHT10????AC????
    	Delay_1ms(80);//???80ms????	
        cnt = 0;
    	while(((AHT20_Read_Status()&0x80)==0x80))//?????bit[7]?0?????????????????1????????
    	{
    		SensorDelay_us(1508);
    		if(cnt++>=100)
    		{
    		 break;
    		}
    	}
    	
    	I2C_Start();
    
    	AHT20_WR_Byte(0x71);
    	Receive_ACK();
    	CTDATA[0]=Byte_1th = AHT20_RD_Byte();//?????????????0x98,?????????bit[7]?1?????0x1C??????0x0C??????0x08????????????bit[7]?0
    	Send_ACK();
    	CTDATA[1]=Byte_2th = AHT20_RD_Byte();//???
    	Send_ACK();
    	CTDATA[2]=Byte_3th = AHT20_RD_Byte();//???
    	Send_ACK();
    	CTDATA[3]=Byte_4th = AHT20_RD_Byte();//???/???
    	Send_ACK();
    	CTDATA[4]=Byte_5th = AHT20_RD_Byte();//???
    	Send_ACK();
    	CTDATA[5]=Byte_6th = AHT20_RD_Byte();//???
    	Send_ACK();
    	Byte_7th = AHT20_RD_Byte();//CRC????
    	Send_NOT_ACK();                           //???: ????????NAK
    	Stop_I2C();
    	
    	if(Calc_CRC8(CTDATA,6)==Byte_7th)
    	{
    	RetuData = (RetuData|Byte_2th)<<8;
    	RetuData = (RetuData|Byte_3th)<<8;
    	RetuData = (RetuData|Byte_4th);
    	RetuData =RetuData >>4;
    	ct[0] = RetuData;//???
    	RetuData = 0;
    	RetuData = (RetuData|Byte_4th)<<8;
    	RetuData = (RetuData|Byte_5th)<<8;
    	RetuData = (RetuData|Byte_6th);
    	RetuData = RetuData&0xfffff;
    	ct[1] =RetuData; //???
    		
    	}
    	else
    	{
    		ct[0]=0x00;
    		ct[1]=0x00;//У??????????????????????????????
    	}//CRC????
    }
    
    
    void AHT20_Init(void)   //?????AHT20
    {	
    	Init_I2C_Sensor_Port();
    	I2C_Start();
    	AHT20_WR_Byte(0x70);
    	Receive_ACK();
    	AHT20_WR_Byte(0xa8);//0xA8????NOR??????
    	Receive_ACK();
    	AHT20_WR_Byte(0x00);
    	Receive_ACK();
    	AHT20_WR_Byte(0x00);
    	Receive_ACK();
    	Stop_I2C();
    
    	Delay_1ms(10);//???10ms????
    
    	I2C_Start();
    	AHT20_WR_Byte(0x70);
    	Receive_ACK();
    	AHT20_WR_Byte(0xbe);//0xBE?????????AHT20????????????0xBE,   AHT10????????????0xE1
    	Receive_ACK();
    	AHT20_WR_Byte(0x08);//???????bit[3]??1???У????
    	Receive_ACK();
    	AHT20_WR_Byte(0x00);
    	Receive_ACK();
    	Stop_I2C();
    	Delay_1ms(10);//???10ms????
    }
    void JH_Reset_REG(uint8_t addr)
    {
    	
    	uint8_t Byte_first,Byte_second,Byte_third;
    	I2C_Start();
    	AHT20_WR_Byte(0x70);//?????0x70
    	Receive_ACK();
    	AHT20_WR_Byte(addr);
    	Receive_ACK();
    	AHT20_WR_Byte(0x00);
    	Receive_ACK();
    	AHT20_WR_Byte(0x00);
    	Receive_ACK();
    	Stop_I2C();
    
    	Delay_1ms(5);//???5ms????
    	I2C_Start();
    	AHT20_WR_Byte(0x71);//
    	Receive_ACK();
    	Byte_first = AHT20_RD_Byte();
    	Send_ACK();
    	Byte_second = AHT20_RD_Byte();
    	Send_ACK();
    	Byte_third = AHT20_RD_Byte();
    	Send_NOT_ACK();
    	Stop_I2C();
    	
        Delay_1ms(10);//???10ms????
    	I2C_Start();
    	AHT20_WR_Byte(0x70);///
    	Receive_ACK();
    	AHT20_WR_Byte(0xB0|addr);?????????
    	Receive_ACK();
    	AHT20_WR_Byte(Byte_second);
    	Receive_ACK();
    	AHT20_WR_Byte(Byte_third);
    	Receive_ACK();
    	Stop_I2C();
    	
    	Byte_second=0x00;
    	Byte_third =0x00;
    }
    
    void AHT20_Start_Init(void)
    {
    	JH_Reset_REG(0x1b);
    	JH_Reset_REG(0x1c);
    	JH_Reset_REG(0x1e);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
    • 400
    • 401
    • 402
    • 403
    • 404
    • 405
    • 406
    • 407
    • 408
    • 409
    • 410
    • 411
    • 412
    • 413
    • 414
    • 415
    • 416
    • 417
    • 418
    • 419
    • 420
    • 421
    • 422
    • 423
    • 424
    • 425
    • 426
    • 427
    • 428
    • 429
    • 430
    • 431
    • 432
    • 433
    • 434
    • 435
    • 436
    • 437
    • 438
    • 439
    • 440
    • 441
    • 442
    • 443
    • 444
    • 445
    • 446
    • 447
    • 448
    • 449
    • 450
    • 451
    • 452
    • 453
    • 454
    • 455
    • 456
    • 457
    • 458
    • 459
    • 460
    • 461
    • 462
    • 463
    • 464
    • 465
    • 466
    • 467
    • 468
    • 469
    • 470
    • 471
    • 472
    • 473
    • 474
    • 475
    • 476
    • 477
    • 478
    • 479
    • 480
    • 481
    • 482
    • 483
    • 484
    • 485
    • 486
    • 487
    • 488
    • 489
    • 490
    • 491
    • 492
    • 493
    • 494
    • 495
    • 496
    • 497
    • 498
    • 499
    • 500
    • 501
    • 写入AHT20-21_DEMO_V1_3.h文件
    #ifndef _AHT20_DEMO_
    #define _AHT20_DEMO_
    
    #include "stm32f10x.h"  
    
    void Delay_N10us(uint32_t t);//???????
    void SensorDelay_us(uint32_t t);//???????
    void Delay_4us(void);		//???????
    void Delay_5us(void);		//???????
    void Delay_1ms(uint32_t t);	
    void AHT20_Clock_Init(void);		//???????
    void SDA_Pin_Output_High(void)  ; //??PB15???????? ?? ????????????? PB15???I2C??SDA
    void SDA_Pin_Output_Low(void);  //??P15????????  ???????????
    void SDA_Pin_IN_FLOATING(void);  //SDA?????????????
    void SCL_Pin_Output_High(void); //SCL?????????P14???I2C??SCL
    void SCL_Pin_Output_Low(void); //SCL???????
    void Init_I2C_Sensor_Port(void); //?????I2C???,????????
    void I2C_Start(void);		 //I2C????????START???
    void AHT20_WR_Byte(uint8_t Byte); //??AHT20д??????
    uint8_t AHT20_RD_Byte(void);//??AHT20?????????
    uint8_t Receive_ACK(void);   //??AHT20????л??ACK
    void Send_ACK(void)	;	  //???????ACK???
    void Send_NOT_ACK(void);	//?????????ACK
    void Stop_I2C(void);	  //???Э?????
    uint8_t AHT20_Read_Status(void);//???AHT20?????????
    uint8_t AHT20_Read_Cal_Enable(void);  //???cal enableλ????????
    void AHT20_SendAC(void); //??AHT20????AC????
    uint8_t Calc_CRC8(uint8_t *message,uint8_t Num);
    void AHT20_Read_CTdata(uint32_t *ct); //???CRCУ?飬?????AHT20?????????????
    void AHT20_Read_CTdata_crc(uint32_t *ct); //CRCУ??????AHT20?????????????
    void AHT20_Init(void);   //?????AHT20
    void JH_Reset_REG(uint8_t addr);///???ü????
    void AHT20_Start_Init(void);///?????????????????????
    #endif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    - 修改main.c文件

    #include "delay.h"
    #include "sys.h"
    #include "oled.h"
    #include "gui.h"
    #include "test.h"
    #include "AHT20-21_DEMO_V1_3.h" 
    void TEST_MainPage1(int c1,int t1)
    {	
    		
    	GUI_ShowCHinese(30,0,16,"月薪过万",1);
        GUI_ShowCHinese(5,30,16,"湿度",1);
        GUI_ShowCHinese(5,45,16,"温度",1);
    	GUI_ShowNum(35,30,c1/10,4,16,1);
    	GUI_ShowNum(35,45,t1/10,4,16,1);
    	Delay_1ms(1000);
    }
    volatile int  c1,t1;
    uint32_t CT_data[2]={0,0};
    u8 temp[10];  
    u8 hum[10];
    int main(void)
    {	
    	delay_init();	    	       //延时函数初始化	
    	OLED_Init();			         //初始化OLED  
    	OLED_Clear(0);             //清屏(全黑)
    	
    	/***********************************************************************************/
    	/**///①刚上电,产品芯片内部就绪需要时间,延时100~500ms,建议500ms
    	/***********************************************************************************/
    	AHT20_Init();
    	Delay_1ms(500);
    	/***********************************************************************************/
    	/**///②上电第一次发0x71读取状态字,判断状态字是否为0x18,如果不是0x18,进行寄存器初始化
    	/***********************************************************************************/
      
    	if((AHT20_Read_Status()&0x18)!=0x18)
    	{
    		AHT20_Start_Init(); //重新初始化寄存器
    		Delay_1ms(10);
    	}
    	
    	  
    	//NVIC_Configuration(); 	   //设置NVIC中断分组2:2位抢占优先级,2位响应优先级 	
       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(0x09,OLED_CMD); //终止页 2
    
    	OLED_WR_Byte(0x00,OLED_CMD); //虚拟字节
    
    	OLED_WR_Byte(0xFF,OLED_CMD); //虚拟字节
    	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倍)	
    		TEST_MainPage1(c1,t1);         //界面显示
    		OLED_WR_Byte(0x2F,OLED_CMD); //开启滚动
    	  Delay_1ms(2000);
              
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70

    在修改main.c时要确定屏幕输出的文字已经进行取模并放置好。然后就是编译、烧录均无问题。

    - 效果:

    QQ视频20221120184252


    六、总结

    因为前面都是用的Cube写的,想着用Cube做简单些,做到后面都是问题,像个无头苍蝇一样到处找资料,但是网上资料极少数是用hal库写的,于是兜兜转转又回到了标准库。后面发现我们只是需要在模板上进行适当的增加删除即可,很快就做完了。前面虽然浪费了挺多时间但是在查找资料过程中也学到了许多。对于本次实验主要是复习了SPI协议和汉字点阵的原理及应用。

    七、参考资料

    https://blog.csdn.net/weixin_51087836/article/details/111488021
    https://blog.csdn.net/qq_43279579/article/details/111414037
    https://blog.csdn.net/Mouer__/article/details/121515031?spm=1001.2014.3001.5502

  • 相关阅读:
    源码学习之MyBatis的底层查询原理
    如何把Elasticsearch中的数据导出为CSV格式的文件
    剖析SGI STL内存池总结
    springboot毕设项目超市收银与会员管理系统6l826(java+VUE+Mybatis+Maven+Mysql)
    C++ - Java 调用 C++ 动态库 <.dylib / .so> By CLion
    Kafka生产常见问题分析与总结
    react class改hooks写法
    简述Spring Bean生命周期
    Go语言中ipv4与Uint32转换
    企业实战之 docker 的部署
  • 原文地址:https://blog.csdn.net/qq_52215423/article/details/127899592