Tests have been performed on the STM32. This example uses the PC0 pin to connect the DHT11 data pin.
In the main function, call the Dth11_test function in the code below.
Data definition: //----The following variables are global variables-------- //----High temperature 8-bit = = u8t_data_h------ //----Temperature Low 8-bit = = u8t_data_l------ //----Humidity 8-bit = = u8rh_data_h----- //---- Low humidity 8-bit = = u8rh_data_l----- //----Check 8-bit = = U8checkdata-----u8 u8t_data_h,u8t_data_l,u8rh_data_h
, U8rh_data_l,u8checkdata;
u8 u8t_data_h_temp,u8t_data_l_temp,u8rh_data_h_temp,u8rh_data_l_temp,u8checkdata_temp; #define BOOL unsigned char #ifndef true #define TRUE 1 #endif #ifndef false #define FALSE 0 #endif static void Dht11_da Tapin_configure_output (void) { GPIO_InitTypeDef gpio_initstructure;
//rcc_apb2periphclockcmd (Rcc_apb2periph_afio, DISABLE); rcc_apb2periphclockcmd (RCC_APB2PERIPH_GPIOC, enable); /enable PC Port clock gpio_initstrucTure. Gpio_pin = gpio_pin_0; //pc.0 port configuration & nbsp; gpio_initstructure.gpio_mode = gpio_mode_out_pp; //push-pull output gpio_initstructure.gpio_speed = Gpio_speed_
50MHz;
gpio_init (GPIOC, &gpio_initstructure); } static void Dht11_datapin_configure_input (void) { GPIO_InitTypeDef datapin;& nbsp; datapin.gpio_pin = gpio_pin_0; datapin.gpio_mode = Gpio_mode_in_ floating; //hover datapin.gpio_speed = Gpio_
Speed_50mhz;
gpio_init (GPIOC, &datapin); } BOOL dht11_get_databit (void) { uint8_t val; val = Gpio_readinputdatabit (GPIOC, gpio_pin_0); if (val = = bit_reset) { return FALSE; }else{&
nbsp; return TRUE;  }} void Dht11_set_databit (BOOL level) { if (level = = TRUE) { &nbs
p; gpio_setbits (GPIOC, gpio_pin_0); }else{ gpio_resetbits (GPIOC, gpio_pin_0); &NBSP}} void Mdelay (u16 ms) { if (ms! = 0) { delay_ms (ms)
;  }} void Udelay (u16 us) { if (us! = 0) {  
;d Elay_us (US); }} static uint8_t dht11_read_byte (void) { uint8_t i; &nbs
P uint8_t data = 0; &NBsp; for (i = 0; i < 8; i+ +) { data & lt;<= 1; while (!
Dht11_get_databit ()));
udelay (10);
udelay (10);
udelay (10); if (Dht11_get_databit ()) {
data |= 0x1;
while (Dht11_get_databit ());  , else{ &
nbsp;} return data; } static uint8_t dht11_start_sampling (void) { dht11_datapin_configure_output ();
//Host pull down 18ms dht11_set_databit (FALSE);
mdelay (18);
dht11_set_databit (TRUE);
//bus pulled by pull-up resistor high host delay 20US Udelay (10);
udelay (10);
udelay (10);
udelay (10);
//host set as input to determine the slave response signal dht11_set_databit (TRUE);
dht11_datapin_configure_input (); //Determine if the slave has a low-level response signal to jump out if it does not respond, and the response runs down if (! Dht11_get_databit ()) //t! { & nbsp //Determine if the slave is emitting a 80us low-level response signal whether it ends while ((!
Dht11_get_databit ())); //printf ("DHT11 answers.\r\n");
//determine if the slave emits a high level of 80US, and then enters the data receiving State while ((Dht11_get_databit ()));
return 1; } return 0; void Dht11_get_data (void) { U8 temp; if (dht11_st Art_sampling ()) { //printf ("DHT11 is ready to transmit data\r\n"); //Data Reception status
u8rh_data_h_temp = Dht11_read_byte ();
u8rh_data_l_temp = Dht11_read_byte ();
u8t_data_h_temp = dht11_read_byte ();
u8t_data_l_temp = dht11_read_byte (); u8checkdata_temp = Dht11_read_byte (); /* Data transmission finishes, pull the bus high */ & nbsp; dht11_datapin_configure_output (); dht11_
Set_databit (TRUE); //data validation temp= (u8t_data_h_
TEMP+U8T_DATA_L_TEMP+U8RH_DATA_H_TEMP+U8RH_DATA_L_TEMP); if (temp==u8checkdata_temp) {
u8rh_data_h=u8rh_data_h_temp;
u8rh_data_l=u8rh_data_l_temp;
u8t_data_h=u8t_data_h_temp;
u8t_data_l=u8t_data_l_temp; u8checkdata=u8checkdata_temp; printf ("DHT11 tempature%d.%d humidity%d. %d \ r \ n ", u8t_data_h, u8t_data_l, U8rh_data_h,
u8t_data_l); }else{ printf ("checksum
Failure \ r \ n "); } }else{ printf ("DHT11 Didn ' t answer. Sampling failed.
\ r \ n ");  }} void Dth11_test () { while (1)   { dht11_get_data () delay_ms (1000);
}}