实验所需元件清单:
实验现象:
通过串口Ai-m61-32s将湿温度数据发送给上位机。
时序及代码如下:
1- 初始化DHT11, 数据线由MCU控制由高电平变为低,并且保持至少18ms, 之后MCU控制由低变为高,并且保持20-40us
代码如下:
- // 初始化GPIO引脚
- gpio = bflb_device_get_by_name("gpio");
- bflb_gpio_init(gpio, DHT_PIN, GPIO_OUTPUT | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
- // 发送开始信号
- bflb_gpio_set(gpio, DHT_PIN);
- bflb_mtimer_delay_ms(50);
- bflb_gpio_reset(gpio, DHT_PIN);
- bflb_mtimer_delay_ms(20);
- bflb_gpio_set(gpio, DHT_PIN);
- bflb_mtimer_delay_us(30);
复制代码 2- 接收来自DHT11的应答
代码如下
- // 读取DHT11的响应
- while (bflb_gpio_read(gpio, DHT_PIN) == 1)
- {
- }
- while (bflb_gpio_read(gpio, DHT_PIN) == 0)
- {
- }
- while (bflb_gpio_read(gpio, DHT_PIN) == 1)
- {
- }
复制代码 3- 数据读取, After above DHT will send 40 bits data which are including 16 bits 湿度数据,16 bits 温度数据和8bits的校验数据。
代码如下:
- // 读取温湿度数据 共 40bits
- // 8bit湿度整数数据+8bit湿度小数数据
- // +8bi温度整数数据+8bit温度小数数据
- // +8bit校验和
- uint8_t data[5] = {0};
- for (int i = 0; i < 5; i++)
- {
- data[i] = 0;
- for (int j = 0; j < 8; j++)
- {
- while (bflb_gpio_read(gpio, DHT_PIN) == 0)
- {
- }
- bflb_mtimer_delay_us(30);
- if (bflb_gpio_read(gpio, DHT_PIN) == 1)
- {
- data[i] |= (1 << (7 - j));
- while (bflb_gpio_read(gpio, DHT_PIN) == 1)
- {
- }
- }
- }
- }
复制代码 4-转换结果
- // 关闭GPIO
- bflb_gpio_deinit(gpio, DHT_PIN);
- // 解析数据
- uint8_t humidity_integer = data[0];
- uint8_t humidity_decimal = data[1];
- uint8_t temperature_integer = data[2];
- uint8_t temperature_decimal = data[3];
- // 计算小数部分
- float humidity = humidity_integer + (humidity_decimal / 10.0);
- float temperature = temperature_integer + (temperature_decimal / 10.0);
- // 数据有效
- LOG_I("Humidity: %.1f%%\n", humidity);
- LOG_I("Temperature: %.1fC\n", temperature);
复制代码
完整代码如下:
- #include "DHT11.h"
- #include "bflb_gpio.h"
- #include "bflb_mtimer.h"
- #include "log.h"
- #define DHT_PIN 33
- struct bflb_device_s *gpio;
- void DHT_START()
- {
- // 初始化GPIO引脚
- gpio = bflb_device_get_by_name("gpio");
- bflb_gpio_init(gpio, DHT_PIN, GPIO_OUTPUT | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
- // 发送开始信号
- bflb_gpio_set(gpio, DHT_PIN);
- bflb_mtimer_delay_ms(50);
- bflb_gpio_reset(gpio, DHT_PIN);
- bflb_mtimer_delay_ms(20);
- bflb_gpio_set(gpio, DHT_PIN);
- bflb_mtimer_delay_us(30);
- // 切换为输入模式并等待DHT11的响应
- bflb_gpio_init(gpio, DHT_PIN, GPIO_INPUT | GPIO_PULLUP | GPIO_SMT_EN);
- // 读取DHT11的响应
- while (bflb_gpio_read(gpio, DHT_PIN) == 1)
- {
- }
- while (bflb_gpio_read(gpio, DHT_PIN) == 0)
- {
- }
- while (bflb_gpio_read(gpio, DHT_PIN) == 1)
- {
- }
- // 读取温湿度数据 共 40bits
- // 8bit湿度整数数据+8bit湿度小数数据
- // +8bi温度整数数据+8bit温度小数数据
- // +8bit校验和
- uint8_t data[5] = {0};
- for (int i = 0; i < 5; i++)
- {
- data[i] = 0;
- for (int j = 0; j < 8; j++)
- {
- while (bflb_gpio_read(gpio, DHT_PIN) == 0)
- {
- }
- bflb_mtimer_delay_us(30);
- if (bflb_gpio_read(gpio, DHT_PIN) == 1)
- {
- data[i] |= (1 << (7 - j));
- while (bflb_gpio_read(gpio, DHT_PIN) == 1)
- {
- }
- }
- }
- }
- // 关闭GPIO
- bflb_gpio_deinit(gpio, DHT_PIN);
- // 解析数据
- uint8_t humidity_integer = data[0];
- uint8_t humidity_decimal = data[1];
- uint8_t temperature_integer = data[2];
- uint8_t temperature_decimal = data[3];
- // 计算小数部分
- float humidity = humidity_integer + (humidity_decimal / 10.0);
- float temperature = temperature_integer + (temperature_decimal / 10.0);
- // 数据有效
- LOG_I("Humidity: %.1f%%\n", humidity);
- LOG_I("Temperature: %.1fC\n", temperature);
- }
复制代码
DHT11-chinese.pdf
(677.52 KB, 下载次数: 2)
|
|