Tools used in this experiment:
STM32 wildfire mini development board, AHT20 temperature and humidity sensor, 0.96 inch OLED display
1, Realize temperature and humidity data acquisition and display through serial port
Objective: to check whether the temperature and humidity acquisition of AHT20 sensor can be realized
Please refer to another blog of Xiaobian for the specific operation process: STM32 AHT20 temperature and humidity data acquisition based on I2C
2, Realize the display of temperature and humidity collected data to OLED screen
This process is based on the project of realizing serial port display above, and adds the data display code of OLED screen.
See the reference link for the complete engineering code download of this experiment.
1. Code download
Click this link to download the OLED data display code:
https://github.com/Sunlight-Dazzling/stm32_AHT20_OLED/tree/main/User/usart
After downloading, add it to the previous project
2. Compilation of some codes
2.1. Code showing temperature and humidity
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("temperature:%d%d.%d",T1/100,(T1/10)%10,T1%10); printf("humidity:%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); //Call the Sprintf function to format the temperature data of DHT11 into the string array variable strTemp sprintf(strHumi,"%.1f",b); //Call the Sprintf function to format the humidity data of DHT11 into the string array variable strHumi //printf(strTemp); //printf("/r/n"); GUI_ShowCHinese(16,00,16,"Temperature and humidity display",1); GUI_ShowCHinese(16,20,16,"temperature",1); GUI_ShowString(53,20,strTemp,16,1); GUI_ShowCHinese(16,38,16,"humidity",1); GUI_ShowString(53,38,strHumi,16,1); delay_ms(1500); delay_ms(1500); delay_ms(1500); delay_ms(1500); }
2.2. Add corresponding dot matrix representation code
The following links can be used for Chinese character extraction (without downloading the font extraction software): https://www.23bei.com/tool-218.html
"temperature",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,/*"Temperature ", 10*/ "wet",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,/*"Wet ", 12*/ "degree",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,/*"Degrees ", 11*/ "display",0x00,0x00,0x1F,0xF0,0x10,0x10,0x10,0x10,0x1F,0xF0,0x10,0x10,0x10,0x10,0x1F,0xF0, 0x04,0x40,0x44,0x44,0x24,0x44,0x14,0x48,0x14,0x50,0x04,0x40,0xFF,0xFE,0x00,0x00,/*"Display ", 13*/ "show",0x00,0x00,0x3F,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFE,0x01,0x00, 0x01,0x00,0x11,0x10,0x11,0x08,0x21,0x04,0x41,0x02,0x81,0x02,0x05,0x00,0x02,0x00,/*"Display ", 14*/
2.3. main function
int main(void) { delay_init(); //Delay function initialization uart_init(115200); IIC_Init(); NVIC_Configuration(); //Set NVIC interrupt packet 2: 2-bit preemption priority and 2-bit response priority OLED_Init(); //Initialize OLED OLED_Clear(0); while(1) { read_AHT20_once(); OLED_Clear(0); delay_ms(1500); } }
3. Compile and burn
Click the compile button in the upper left corner to compile and generate the corresponding. hex file
Open mcuisp software to burn. hex file
4. Operation results
Since the AHT20 sensor has not been connected, the temperature and humidity display is 0. In addition, the acquisition speed can be set by ourselves. The acquisition interval I set here is relatively long.
3, Summary
Based on the realization of temperature and humidity data acquisition, serial port display and OLED screen data display code, the temperature and humidity data collected by AHT20 is not complex. Among them, the process of temperature and humidity data acquisition uses analog I2C, and the data display of OLED screen uses SPI.
4, Reference link
1.https://blog.csdn.net/qq_43279579/article/details/111678857
2.https://blog.csdn.net/qq_54496810/article/details/121426752
3.Complete engineering code
Extraction code: 1234