【AT-START-F403A Review】+DHT22 temperature and humidity detection and display
[Copy link]
In addition to driving display devices, driving various digital sensors is also very beneficial.
Here we introduce a driving method for a single-bus digital temperature and humidity sensor. The device is DHT22, which has higher accuracy than DHT11 and can reach 1 decimal place.
When using this device, its data pin is connected to PB11, so there are the following definition pins for outputting high and low levels and reading pin status:
#define DHT22_D0_H GPIOB->BSRE = GPIO_Pins_15
#define DHT22_D0_L GPIOB->BRE = GPIO_Pins_15
#define DHT22_D0_R GPIO_ReadInputDataBit(GPIOB, GPIO_Pins_15)
The function to initialize DHT22 is:
uint8_t DHT22_Init(void)
{
GPIO_InitType GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOB, ENABLE);
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pins = GPIO_Pins_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT_PP;
GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_10MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
DHT22_Rst();
return DHT22_Check();
}
The function to read DHT22 data is:
uint8_t DHT22_Read_Data(uint8_t *temp,uint8_t *humi)
{
uint8_t buf[5];
uint8_t i;
DHT22_Rst();
if(DHT22_Check()==0)
{
for(i=0;i<5;i++)
{
buf[i]=DHT22_Read_Byte();
}
if((buf[0]+buf[1]+buf[2]+buf[3])==buf[4])
{
*humi=(buf[0]*256+buf[1])/10;
*temp=(buf[2]*256+buf[3])/10;
}
}else return 1;
return 0;
}
The program to realize the temperature and humidity detection function is as follows:
if(i==4)
{
showhanzi16h(80,100,5,1);
showhanzi16h(80,84,6,1);
showhanzi16h(180,280,13,0);
showhanzi16h(180,264,14,0);
while(GPIO_ReadInputDataBit(GPIOA, GPIO_Pins_0)==0);
LCD_Clear(WHITE);
show_imageh(10,300,0); //200*200 * 1
BACK_COLOR=WHITE;
POINT_COLOR=RED;
LCD_ShowStringH(60,90,"DHT22 TEST");
senflag=DHT22_Init();
if(senflag) LCD_ShowStringH(80,90,"NO");
else LCD_ShowStringH(80,90,"OK");
while(GPIO_ReadInputDataBit(GPIOA, GPIO_Pins_0)==0)
{
if(!senflag)
{
DHT22_Read_Data(&temperature,&humidity);
LCD_ShowCharh(100,90,'T',1);
LCD_ShowCharh(100,80,'e',1);
LCD_ShowCharh(100,70,'m',1);
LCD_ShowCharh(100,60,'p',1);
LCD_ShowCharh(100,50,':',1);
LCD_ShowCharh(100,40,temperature/10+'0',0);
LCD_ShowCharh(100,30,temperature%10+'0',0);
LCD_ShowCharh(120,90,'H',1);
LCD_ShowCharh(120,80,'u',1);
LCD_ShowCharh(120,70,'m',1);
LCD_ShowCharh(120,60,'i',1);
LCD_ShowCharh(120,50,':',1);
LCD_ShowCharh(120,40,humidity/10+'0',0);
LCD_ShowCharh(120,30,humidity%10+'0',0);
}
Delay_ms(200u);
}
LCD_Clear(WHITE);
AT32_jm();
showhanzi16h(80,100,5,1);
showhanzi16h(80,84,6,1);
showhanzi16h(180,280,13,0);
showhanzi16h(180,264,14,0);
}
The running effect of the program is shown in Figure 1 and Figure 2, where Figure 1 is the selection of the environment detection function, and Figure 2 is the temperature and humidity display interface.
Figure 1 Function selection
Figure 2 Temperature and humidity display
|