stm32 realizes the acquisition of temperature and humidity (AHT20) through I2C interface

Keywords: Embedded system

1, I2C bus protocol

1. What is the I2C protocol

I2C communication protocol (Inter Integrated Circuit) is developed by Phiilps company. Due to its few pins, simple hardware implementation and strong scalability, it does not need external transceiver equipment of USART, CAN and other communication protocols. Now it is widely used in the communication between multiple integrated circuits (ICS) in the system.

2. Physical layer and protocol layer of I2C protocol

① Physical layer
I2C is a bus that supports devices. It can connect multiple I2C communication devices and support multiple communication hosts and multiple communication slaves. For I2C bus, only two bus lines are used, one bidirectional serial data line (SDA) and one serial clock line (SCL).
Common connection modes of I2C communication equipment (refer to the figure in the wildfire data)
② Protocol layer
It mainly defines the start and stop signals of communication, data validity, response, arbitration, clock synchronization and address broadcasting.
Start and stop signals of communication

Data validity

It can be seen from the figure that during I2C communication, SDA data transmission is effective only when SCL is at high level. SDA signal line is used to transmit data, and SCL signal line is used to ensure data synchronization.

When SDA transmits data, the receiver will respond to the received data. If you want to continue transmitting data, respond to the response signal (low level), otherwise respond to the non response signal (high level).

3. Two ways of I2C - hardware I2C and software I2C
① Hardware I2C
Directly use the hardware I2C peripherals in STM32 chip.

Hardware I2C Use of
 As long as the corresponding registers are configured, the peripherals will generate the timing of standard serial port protocol. After initialization I2C After the peripheral, it only needs to set a register to position 1. At this time, the peripheral will control the corresponding register SCL and SDA Automatic line generation I2C The start signal does not require the core to directly control the level of the pin.

② Software I2C
Directly use the CPU core to control the high and low level of GPIO output according to the requirements of I2C protocol, so as to simulate I2C.

Software I2C Use of
 Need to be generated in control I2C When the start signal is, the control acts as SCL Linear GPIO The pin outputs a high level and then controls as SDA Linear GPIO During this period, the pin completes the switching from high level to low level, and finally controls SCL The line is switched to low level, so a standard output is output I2C Start signal.

③ The difference between the two
Hardware I2C directly uses peripherals to control pins, which can reduce the burden of CPU. However, when using hardware I2C, some fixed pins must be used as SCL and SDA, while software analog I2C can use any GPIO pin, which is relatively flexible. The usage of hardware I2C is more complex, and the flow of software I2C is clearer. If you want to understand I2C protocol in detail, you may better understand this process by using software I2C. In the process of using I2C, hardware I2C communication may be faster and more stable.

2, Realization of AHT20 acquisition program

Application process of AHT20 chip

void  read_AHT20_once(void)

	reset_AHT20();//Reset AHT20 chip

	init_AHT20();//Initialize AHT20 chip

	startMeasure_AHT20();//Start testing AHT20 chip

	read_AHT20();//Read the data collected by AHT20

AHT20 chip reads data

void read_AHT20(void)
	uint8_t   i;

	for(i=0; i<6; i++)
	I2C_Start();//I2C start

	I2C_WriteByte(0x71);//I2C write data
	ack_status = Receive_ACK();//Received response information
	readByte[0]= I2C_ReadByte();//I2C read data
	Send_ACK();//Send response information

	readByte[1]= I2C_ReadByte();

	readByte[2]= I2C_ReadByte();

	readByte[3]= I2C_ReadByte();

	readByte[4]= I2C_ReadByte();

	readByte[5]= I2C_ReadByte();

	I2C_Stop();//I2C stop function
	//Judge whether the first byte read is 0x08, which is specified in the chip reading process. If there is no problem in the reading process, process the read data accordingly
	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;
		AHT20_OutData[0] = 0xFF;
		AHT20_OutData[1] = 0xFF;

		AHT20_OutData[2] = 0xFF;
		AHT20_OutData[3] = 0xFF;
		printf("Read failed!!!");

	//According to the calculation formula of temperature and humidity in AHT20 chip, the final result is obtained and displayed through serial port

The results show that

3, References

Posted by johno on Fri, 26 Nov 2021 00:24:01 -0800