Collect DHT11 temperature and humidity data, use STM32F103ZET6 GPIOG11 pin to read SHT11 data, print the temperature and humidity of the collection via serial port
dht11.c file:
#include"Dht11.h"#include"Delay.h"//Reset DHT11voidDht11_rst (void) {dht11_io_out (); //SET OUTPUTdht11_dq_out=0;//Pull Low DQDelay_ms ( -);//pull down at least 18msdht11_dq_out=1;//dq=1Delay_us ( -);//main engine Pull high 20~40us}//waiting for DHT11 's response//return 1: The presence of DHT11 is not detected//return 0: PresenceU8 Dht11_check (void) {U8 Retry=0; Dht11_io_in ();//SET INPUT while(dht11_dq_in&&retry< -)//DHT11 will pull down 40~80us .{Retry++; Delay_us (1); }; if(retry>= -)return 1; Elseretry=0; while(! dht11_dq_in&&retry< -)//DHT11 will pull up again after the low 40~80us{Retry++; Delay_us (1); }; if(retry>= -)return 1; return 0;}//read a bit from DHT11//return value: 1/0U8 Dht11_read_bit (void) {U8 Retry=0; while(dht11_dq_in&&retry< -)//wait becomes low level{Retry++; Delay_us (1); } Retry=0; while(! dht11_dq_in&&retry< -)//wait for the high level to change{Retry++; Delay_us (1); } delay_us ( +);//wait 40us if(dht11_dq_in)return 1; Else return 0; }//read a byte from DHT11//return value: The data readU8 Dht11_read_byte (void) {U8 i,dat; DAT=0; for(i=0;i<8; i++) {dat<<=1; DAT|=dht11_read_bit (); } returndat;}//read data once from DHT11//Temp: Temperature Value (range: 0~50°)//Humi: Humidity value (range: 20%~90%)//return value: 0, normal; 1, read failedU8 Dht11_read_data (U8 *temp,u8 *Humi) {U8 buf[5]; U8 I; Dht11_rst (); if(Dht11_check () = =0) { for(i=0;i<5; i++)//read 40-bit data{Buf[i]=Dht11_read_byte (); } if((buf[0]+buf[1]+buf[2]+buf[3]) ==buf[4]) { *humi=buf[0]; *temp=buf[2]; } }Else return 1; return 0; }//Initialize DHT11 IO port DQ to detect the presence of DHT11 simultaneously//return 1: not present//return 0: PresenceU8 Dht11_init (void) {gpio_inittypedef gpio_initstructure; Rcc_apb2periphclockcmd (Rcc_apb2periph_gpiog, ENABLE); //Enable PG Port ClockGpio_initstructure.gpio_pin= Gpio_pin_11;//PG11 Port ConfigurationGpio_initstructure.gpio_mode = gpio_mode_out_pp;//push-Pull outputGpio_initstructure.gpio_speed =Gpio_speed_50mhz; Gpio_init (Gpiog,&gpio_initstructure);//Initialize IO portGpio_setbits (Gpiog,gpio_pin_11);//PG11 Output HighDht11_rst (); //Reset DHT11 returnDht11_check ();//waiting for DHT11 's response}
Dht11.h file
#ifndef __dht11_h#define__dht11_h#include"Sys.h" //io Direction setting#defineDht11_io_in () {gpiog->crh&=0xffff0fff; gpiog->crh|=8<<12;}#defineDht11_io_out () {gpiog->crh&=0xffff0fff; gpiog->crh|=3<<12;}////io operation function#defineDht11_dq_out Pgout (11)//Data Port#defineDht11_dq_in Pgin (11)//Data PortU8 Dht11_init (void);//Initialize DHT11U8 Dht11_read_data (U8 *temp,u8 *humi);//read temperature and humidity levelU8 Dht11_read_byte (void);//read out a byteU8 Dht11_read_bit (void);//read out a bitU8 Dht11_check (void);//detect if DHT11 existsvoidDht11_rst (void);//Reset DHT11#endif
MAIN.C file:
#include"led.h"#include"Delay.h"#include"Sys.h"#include"usart.h"#include"Dht11.h" intMainvoid) {U8 T=0; U8 temperature; U8 Humidity; Delay_init (); //initialization of the delay functionNvic_prioritygroupconfig (nvic_prioritygroup_2);//set interrupt Priority group to group 2:2-bit preemption priority, 2-bit response priorityUart_init (115200);//serial port initialized to 115200Led_init ();//initializing the hardware interface to the LED connection while(Dht11_init ())//DHT11 Initialization{printf ("DHT11 error\r\n"); Delay_ms ( the); } while(1) { if(t% -==0)//Read once per 100ms{dht11_read_data (&temperature,&humidity);//read temperature and humidity valuesprintf"\ r \ n temperature:%d\r\n", temperature); printf ("Humidity:%d\r\n", humidity); } Delay_ms (Ten); T++; if(t== $) {T=0; LED0=!LED0; } }}
Serial Reception Effect:
SOURCE Link: https://pan.baidu.com/s/1vyIc6Zu2OOWwDsgLQonKKw
Extract code: Yolu
12-Development Board access Small Five things to achieve remote control (STM32 acquisition temperature and humidity sensor DHT11)