123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- #include "drive_dht11.h"
- void dht11_gpio_input(void) //DHT11 IO配置成输入模式
- {
- GPIO_InitTypeDef g;
-
- RCC_APB2PeriphClockCmd(DHT11_RCC, ENABLE);
-
- g.GPIO_Pin = DHT11_GPIO_PIN;
- g.GPIO_Speed = GPIO_Speed_50MHz;
- g.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
-
- GPIO_Init(DHT11_GPIO_TYPE, &g);
- }
- void dht11_gpio_output(void)//DHT11 IO配置成输出模式
- {
- GPIO_InitTypeDef g;
-
- RCC_APB2PeriphClockCmd(DHT11_RCC, ENABLE);
-
- g.GPIO_Pin = DHT11_GPIO_PIN;
- g.GPIO_Speed = GPIO_Speed_50MHz;
- g.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
- GPIO_Init(DHT11_GPIO_TYPE, &g);
- }
- void dht11_reset(void)//DHT11初始化
- {
- // 按照DHT11手册步骤
- dht11_gpio_output();
- DHT11_OUT_L;
- //delay_us(19000);
- delay_ms(20);
- DHT11_OUT_H;
- delay_us(30);
- dht11_gpio_input();
- }
- u16 dht11_scan(void)
- {
- return DHT11_IN;
- }
- u16 dht11_read_bit(void)//向DHT11读取一个位函数
- {
- while (DHT11_IN == RESET);
- delay_us(40);
- if (DHT11_IN == SET)
- {
- while (DHT11_IN == SET);
- return 1;
- }
- else
- {
- return 0;
- }
- }
- u16 dht11_read_byte(void)//向DHT11读取一个字节函数
- {
- u16 i;
- u16 data = 0;
- for (i = 0; i < 8; i++)
- {
- data <<= 1;
- data |= dht11_read_bit();
- }
- return data;
- }
- u16 dht11_read_data(u8 buffer[5])//读DHT11数据函数
- {
- u16 i = 0;
- u8 checksum;
- dht11_reset();
- if (dht11_scan() == RESET)
- {
- //检测到DHT11响应
- while (dht11_scan() == RESET);
- while (dht11_scan() == SET);
- for (i = 0; i < 5; i++)
- {
- buffer[i] = dht11_read_byte();
- }
-
- while (dht11_scan() == RESET);
- dht11_gpio_output();
- DHT11_OUT_H;
-
- checksum = buffer[0] + buffer[1] + buffer[2] + buffer[3];
- if (checksum != buffer[4])
- {
- // checksum error
- return 1;
- }
- }
-
- return 0;
- }
|