has been tested on the STM32. This example uses the PC0 pin to connect the DHT11 data pins.
The Dth11_test function in the following code is invoked in the main function.
Data definition: //----The following variables are global variables-------- //----temperature is high 8 bit = = u8t_data_h------ //----temperature low 8 bit = = u8t_data_l------ //----Humidity High 8-bit = = u8rh_data_h----- //---- Humidity low 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; //Hanging 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-pull high host delay 20US Udelay (10);
udelay (10);
udelay (10);
udelay (10);
//host set to input to judge the response signal dht11_set_databit (TRUE);
dht11_datapin_configure_input (); //Determine if there is a low level response signal from the machine if not responding to jump out, the response is running down if (! Dht11_get_databit ()) //t! { & nbsp //Determine whether the 80US low level response signal from the machine is terminated while (!
Dht11_get_databit ())); //printf ("DHT11 answers.\r\n");
//judge whether the 80us high level is emitted from the machine, such as incoming data receiving status 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 is 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);
}}