STM32 Peripheral driver--dht11 temperature and humidity sensor

Source: Internet
Author: User
Tags bool printf

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;    &nbsp} 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)
;    &nbsp} void Udelay (u16 us)   {    if (US!= 0) {       &nbsp
;d Elay_us (US);    &nbsp} 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;    &nbsp     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 ");    &nbsp} void Dth11_test () {    while (1)    &nbsp {        dht11_get_data ();         delay_ms (1000);




          }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.