The 0.96 OLED screen display based on SPI protocol is completed through STM32F103C8T6 configuration


foreword


1. SPI protocol

1. Application

The SPI protocol is a communication protocol (Serial Peripheral Interface) proposed by Motorola, namely the Serial Peripheral Interface, which is a high-speed full-duplex communication bus. It is widely used between ADCs, LCDs and other devices and MCUs, where a high communication rate is required.

2. Composition

The same as the I2C protocol, it is divided into physical layer and protocol layer.

- Common connection methods between SPI communication devices
insert image description here

- The physical layer
SPI communication uses 3 buses and chip select lines, the 3 buses are SCK, MOSI, MISO, and the chip select line is NSS.

NSS : There is no device address in the SPI protocol. It uses the NSS signal line to address. When the host wants to select the slave device, set the NSS signal line of the slave device to low level, and the slave device is selected, that is, chip select Valid, then the host starts SPI communication with the selected slave device.
SCK (Serial Clock) : Clock signal line, used for communication data synchronization.
MOSI (Master Output, Slave Input) : Master output/slave input pin.
MISO (Master Input, Slave Output) : master input/slave output pin.

- protocol layer

Similar to I2C, the SPI protocol defines communication start and stop signals, data validity, and clock synchronization.

- SPI architecture of STM32:
insert image description here

  • Basic communication process
    insert image description here

  • Communication start and stop signals

The NSS signal line changes from high to low, which is the start signal of SPI communication . NSS is the exclusive signal line of each slave. When the slave detects the start signal on its own NSS line, it knows that it has been selected by the master and starts to communicate with the master. The NSS signal changes from low to high, which is the stop signal of SPI communication , indicating that the communication is over and the selected state of the slave is cancelled.

  • data availability

The data of MOSI and MISO changes during the rising edge of SCK and is sampled during the falling edge of SCK. That is to say, the data of MOSI and MISO are valid at the falling edge of SCK, when it is high level, it means data "1", when it is low level, it means data "0". At other times, the data is invalid, and MOSI and MISO prepare for the next representation of data. (This is just an analysis of the situation in the above picture, and the above picture is not the only communication mode.)

  • Four modes of SPI

insert image description here

Here is a brief review of the general content of the knowledge, see the following reference materials for details.

2. Principles of OLED screen display and Chinese character dot matrix coding

1. Chinese character dot matrix coding principle

1. In the dot matrix font library of Chinese characters, each bit of each byte represents a dot of a Chinese character, and each Chinese character is composed of a rectangular dot matrix, 0 means no, 1 means dot, and 0 and 1 is drawn in different colors to form a Chinese character. The commonly used dot matrix has three fonts: 12x12, 14x14, and 16x16.
2. The font library is divided into horizontal matrix and vertical matrix according to the different points represented by the bytes. At present, most of the font libraries are stored in the horizontal matrix (the most used should be the early UCDOS font), and the vertical matrix is ​​generally because there is a certain Some LCDs use the vertical scanning display method. In order to improve the display speed, the font matrix is ​​made vertical, so as to avoid matrix conversion during display. What we describe next refers to the horizontal matrix font.

Details--------> Chinese character dot matrix principle

2. OLED display

OLED (Organic Light Emitting Display, Chinese name Organic Light Emitting Display) refers to the phenomenon that organic semiconductor materials and luminescent materials are driven by an electric field to cause light emission through carrier injection and recombination. The principle is to use the ITO transparent electrode and the metal electrode as the anode and cathode of the device respectively. Driven by a certain voltage, electrons and holes are injected from the cathode and anode to the electron and hole transport layer respectively, and the electrons and holes pass through the electron and hole respectively. The hole transport layer migrates to the emissive layer, where they meet to form excitons and excite the luminescent molecules, which undergo radiative relaxation to emit visible light.

From -------> Baidu Encyclopedia

3. OLED module

insert image description here
insert image description here

Other versions or see: http://www.lcdwiki.com/zh/0.96inch_SPI_OLED_Module

4. Display your student number and name

Download the demo required for this experiment.

- Unzip and enter the project directory, double-click the file to open keil for code modification
insert image description here

- Compile once so that the files under the .c file come out
insert image description here

- Get the corresponding Chinese characters through the font-taking software.

Set C51 format

insert image description here
insert image description here

- Copy the obtained font and put it in oledfont.h under gui.c

Use English quotation marks to modify the real words in front of each font obtained.

insert image description here

- Open test.c, modify the TEST_MainPage function
insert image description here

Parameters of GUI_ShowString()
Parameter 1: X coordinate
Parameter 2: Y coordinate
Parameter 3: string (in ASCLL code)
Parameter 4: bit (indicates the character display format, here I use 16, which is as high as Chinese characters )
Parameter 5: display style (1: white characters with black background; 0: black characters with white background) Parameter 1
of GUI_ShowChinese()
Parameter 1: X coordinate
Parameter 2: Y coordinate
Parameter 3: Chinese character dot matrix size (used here It is 16×16, the parameter should be 16)
Parameter 4: Chinese characters to be displayed
Parameter 5: Display style (1: white characters with black background; 0: black characters with white background)

- Modify the main function of the main.c file

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();
	}

There is no problem with compiling and burning.

- Effect
insert image description here

5. Display temperature and humidity & slide to display long characters

This is the temperature and humidity output from the serial port of my last article that does not use oled: complete the data acquisition of the AHT20 temperature and humidity sensor based on the I2C protocol through STM32Cube configuration

- In order to read the temperature and humidity, we need to configure the files provided by the temperature and humidity sensor manufacturer (Ausong AHT20) to facilitate subsequent calls

  • Create a new AHT20 folder, and create two blank files, AHT20-21_DEMO_V1_3.c and AHT20-21_DEMO_V1_3.h. Put it in the following folder:
    insert image description here
    2. Add the file to the project
    insert image description here
    3. Add the file path
    insert image description here

  • Write AHT20-21_DEMO_V1_3.c file

/*******************************************/
/*@??????У?????????????????          */
/*@????????????????????                */
/*@?汾??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);
}
  • Write AHT20-21_DEMO_V1_3.h file
#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

- Modify the main.c file

#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);
          
	}
}

When modifying main.c, make sure that the text output on the screen has been modeled and placed. Then there is no problem with compiling and burning.

- Effect:

QQ video 20221120184252


6. Summary

Because the front is written in Cube, I thought it would be easier to use Cube, and there will be problems in the end, looking for information everywhere like a headless chicken, but very few online materials are written in hal library, so I went around Back to the standard library again. Later, we found that we just need to add and delete the template appropriately, and it will be done soon. Although a lot of time was wasted in the front, I also learned a lot in the process of searching for information. For this experiment, the principle and application of SPI protocol and Chinese character lattice are reviewed.

7. References

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

Guess you like

Origin blog.csdn.net/qq_52215423/article/details/127899592