Week 10-11---I2C/SPI based temperature and humidity collection and OLED display

Table of contents

One I2C bus protocol

1.1 Hardware I2C and Analog I2C

2. Realize the acquisition program of AHT20

3. Temperature and humidity collection - OLED display

Chinese character dot matrix:

 Four summary

Five Reference Links


One I2C bus protocol

       The I2C protocol stipulates that the transmission of data on the bus must use a start signal as a start condition, and an end signal as a stop condition for transmission. Start and stop signals are always generated by the master. When the bus is in an idle state, both SCL and SDA remain high. When SCL is high and SDA transitions from high to low, it means that a start condition is generated; when SCL is high and SDA transitions from low to high Transition, indicating that a STOP condition has been generated. After the start condition is generated, the bus is in a busy state, which is exclusively occupied by the master-slave device of this data transmission, and other I2C devices cannot access the bus; after the stop condition is generated, the master-slave device of this data transmission will release the bus, and the bus Idle again.
 

1.1 Hardware I2C and Analog I2C

1. Hardware I2C

The so-called hardware I2C corresponds to the I2C peripheral on the chip, and has a corresponding I2C driver circuit, and the I2C pins used are also dedicated; software I2C generally uses GPIO pins, and uses software to control the state of the pins to simulate I2C communication waveforms.
2. Analog I2C

The analog I2C is through GPIO, the software simulates the working mode of the register, and the hardware (firmware) I2C directly calls the internal register for configuration. If you want to look at the specific hardware, you can read the chip manual. Because the port of firmware I2C is fixed, it will be different.
3. Difference

    The efficiency of hardware I2C is much higher than that of software, and software I2C has a more flexible interface because it is not limited by pins.
    You can look at the underlying configuration, such as the IO port configuration. If the function of the IO port (IIC function) is configured, it is the firmware IIC. Otherwise, it is a simulation. You can look
    at the IIC writing function to see if there is any call to a ready-made function or assign a value to a certain register. , if there is, it must be the firmware IIC function, if not, the data must be simulated and sent bit by bit, and the cycle must be used, then it is simulation.
    Judging by the amount of code, the amount of code in the simulation must be larger than that in the firmware.
 

2. Realize the acquisition program of AHT20

Main code:

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 chip read data

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

Compile:

 Burning:

 run:

3. Temperature and humidity collection - OLED display

code:

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

Chinese character dot matrix:

 main function call:

#include "delay.h"
#include "usart.h"
#include "bsp_i2c.h"
#include "sys.h"

#include "oled.h"
#include "gui.h"
#include "test.h"

int main(void)
{	
	delay_init();	    	       //延时函数初始化    	  
	uart_init(115200);	 
	IIC_Init();
		  
	NVIC_Configuration(); 	   //设置NVIC中断分组2:2位抢占优先级,2位响应优先级 	
	OLED_Init();			         //初始化OLED  
	OLED_Clear(0); 
	while(1)
	{
		//printf("温度湿度显示");
		read_AHT20_once();
		OLED_Clear(0); 
		delay_ms(1500);
  }
}

Compile:

Burning:

run:

 

 Four summary

In this experiment, I learned the specific content of the I2C bus protocol and the main differences between hardware I2C and analog I2C. The efficiency of hardware I2C is much higher than that of software, while software I2C has a more flexible interface because it is not limited by pins. Learned how to use stm32 to realize the measurement of temperature and humidity.

Five Reference Links

Stm32 realizes the collection of temperature and humidity (AHT20) through the I2C interface - Programmer Sought

Guess you like

Origin blog.csdn.net/qq_52445967/article/details/122462548