使用RT-Thread和CPK-RA2L1采集DHT11温湿度

一、准备
本篇文章主要介绍使用rt-thread studio 和瑞萨 cpk-ra2l1评估板,使用大佬的轮子采集温湿度
二、硬件准备
cpk-ra2l1评估板, 这个板子的芯片型号是 r7fa2l1ab2dfm,dht11 温湿度传感器。
三、新建工程
1、总线空闲状态为高电平,主机把总线拉低等待dht11响应,主机把总线拉低必须大于18毫秒,保证dht11能检测到起始信号。dht11接收到主机的开始信号后,等待主机开始信号结束,然后发送80us低电平响应信号.主机发送开始信号结束后,延时等待20-40us后, 读取dht11的响应信号,主机发送开始信号后,可以切换到输入模式,或者输出高电平均可, 总线由上拉电阻拉高。
2、总线为低电平,说明dht11发送响应信号,dht11发送响应信号后,再把总线拉高80us,准备发送数据,每一bit数据都以50us低电平时隙开始,高电平的长短定了数据位是0还是1.格式见下面图示.如果读取响应信号为高电平,则dht11没有响应,请检查线路是否连接正常.当最后一bit数据传送完毕后,dht11拉低总线50us,随后总线由上拉电阻拉高进入空闲状态
3、数字0信号
4、数字1信号
四、驱动代码
/*
copyright (c) 2006-2021, rt-thread development teamspdx-license-identifier: apache-2.0change logs:date author notes2023-03-01 dyc the first version
/
#include dht11.h
#include
#include hal_data.h
#include
#define dbg_tag sensor.asair.dhtxx
#ifdef pkg_using_dhtxx_debug
#define dbg_lvl dbg_log
#else
#define dbg_lvl dbg_error
#endif
#include
/ timing /
#define dht1x_begin_time 20 / ms /
#define dht2x_begin_time 1 / ms /
#define dhtxx_pull_time 30 / us /
#define dhtxx_reply_time 100 / us /
#define measure_time 40 / us /
rt_weak void rt_hw_us_delay(rt_uint32_t us)
{
rt_uint32_t delta;
us = us * (systick->load / (1000000 / rt_tick_per_second));
delta = systick->val;
while (delta - systick->val < us) continue;
}
/ *this function will split a number into two part according to times.@param num the number will be split@param integer the integer part@param decimal the decimal part@param times how many times of the real number (you should use 10 in this case)@return 0 if num is positive, 1 if num is negative
*/
int split_int(const int num, int *integer, int *decimal, const rt_uint32_t times)
{
int flag = 0;
if (num < 0) flag = 1;
int anum = num<0 ? -num : num;
integer = anum / times;
decimal = anum % times;
return flag;
}
/this function will convert temperature in degree celsius to kelvin.@param c the temperature indicated by degree celsius@return the result
/
float convert_c2k(float c)
{
return c + 273.15;
}
/ *this function will convert temperature in degree celsius to fahrenheit.@param c the temperature indicated by degree celsius@return the result
/
float convert_c2f(float c)
{
return c * 1.8 + 32;
}
/ *this function will convert temperature in degree fahrenheit to celsius.@param f the temperature indicated by degree fahrenheit@return the result
/
float convert_f2c(float f)
{
return (f - 32) * 0.55555;
}
/ *this function will read a bit from sensor.@param pin the pin of dout@return the bit value
/
static uint8_t dht_read_bit(const rt_base_t pin)
{
uint8_t retry = 0;
while(rt_pin_read(pin) && retry < dhtxx_reply_time)
{
retry++;
rt_hw_us_delay(1);
}
retry = 0;
while(!rt_pin_read(pin) && retry < dhtxx_reply_time)
{
retry++;
rt_hw_us_delay(1);
}
rt_hw_us_delay(measure_time);
return rt_pin_read(pin);
}
/ *this function will read a byte from sensor.@param pin the pin of dout@return the byte
*/
static uint8_t dht_read_byte(const rt_base_t pin)
{
uint8_t i, byte = 0;
for(i=0; i<8; i++)
{
byte / mcu request sampling /
rt_pin_mode(dev->pin, pin_mode_output);
rt_pin_write(dev->pin, pin_low);
if (dev->type == dht11) {
rt_thread_mdelay(dht1x_begin_time); / tbe /
} else {
rt_thread_mdelay(dht2x_begin_time);
}
#ifdef pkg_using_dhtxx_interrupt_disable
level = rt_hw_interrupt_disable();
#endif
rt_pin_mode(dev->pin, pin_mode_input_pullup);
rt_hw_us_delay(dhtxx_pull_time); / tgo /
/ waiting for sensor reply /
while (rt_pin_read(dev->pin) && retry = dhtxx_reply_time) return rt_false;
retry = 0;
while (!rt_pin_read(dev->pin) && retry = dhtxx_reply_time) return rt_false;
/ read data /
for(i=0; i {
dev->data[i] = dht_read_byte(dev->pin);
}
#ifdef pkg_using_dhtxx_interrupt_disable
rt_hw_interrupt_enable(level);
#endif
/ checksum */
for(i=0; i {
sum += dev->data[i];
}
if(sum != dev->data[4]) return rt_false;
return rt_true;
}
/**this function will get the humidity from dhtxx sensor.@param dev the device to be operated@return the humidity value
/
rt_int32_t dht_get_humidity(dht_device_t const dev)
{
rt_assert(dev);
rt_int32_t humi = 0;
switch(dev->type)
{
case dht11:
humi = dev->data[0] * 10 + dev->data[1];
break;
default:
break;
}
return humi;
}
/ *this function will get the temperature from dhtxx sensor.@param dev the device to be operated@return the temperature value
/
rt_int32_t dht_get_temperature(dht_device_t const dev)
{
rt_assert(dev);
rt_int32_t temp = 0;
switch(dev->type)
{
case dht11:
temp = dev->data[2] * 10 + (dev->data[3] & 0x7f);
if(dev->data[3] & 0x80) {
temp = -temp;
}
break;
default:
break;
}
return temp;
}
/ *this function will init dhtxx sensor device.@param dev the device to init@param pin the pin of dout@return the device handler
*/
rt_err_t dht_init(struct dht_device dev, const rt_base_t pin)
{
if(dev == null)
return -rt_error;
dev->type = dht_type;
dev->pin = pin;
rt_memset(dev->data, 0, dht_data_size);
rt_pin_mode(dev->pin, pin_mode_input_pullup);
return rt_eok;
}
// 1、初始化类型
dht_device_t dht_create(const rt_base_t pin)
{
dht_device_t dev;
dev = rt_calloc(1, sizeof(struct dht_device));
if (dev == rt_null)
{
log_e(can't allocate memory for dhtxx device);
return rt_null;
}
dev->type = dht_type;
dev->pin = pin;
rt_memset(dev->data, 0, dht_data_size);
rt_pin_mode(dev->pin, pin_mode_input_pullup);
return dev;
}
void dht_delete(dht_device_t dev)
{
if (dev)
rt_free(dev);
}
/
copyright (c) 2006-2021, rt-thread development teamspdx-license-identifier: apache-2.0
change logs:
date author notes
2023-03-01 dyc the first version
/
#ifndef src_dht11_h_
#define src_dht11_h_
#include
#include
#include
#include
#include
#define dhtlib_version 0.9.0
#define dht_data_size 5
/ sensor model type */
#define dht11 0
#define dht_type dht11
struct dht_device
{
rt_base_t pin;
rt_uint8_t type;
rt_uint8_t data[dht_data_size];
rt_mutex_t lock;
};
typedef struct dht_device *dht_device_t;
dht_device_t dht_create(const rt_base_t pin);
void dht_delete(dht_device_t dev);
rt_err_t dht_init(struct dht_device *dev, const rt_base_t pin);
rt_bool_t dht_read(dht_device_t dev);
rt_int32_t dht_get_humidity(dht_device_t dev);
rt_int32_t dht_get_temperature(dht_device_t dev);
float convert_c2k(float c);//将摄氏温度转为开氏温度
float convert_c2f(float c);//将摄氏温度转为华氏温度
float convert_f2c(float f);//将华氏温度转为摄氏温度
rt_int32_t split_int(const rt_int32_t num, rt_int32_t *integer,
rt_int32_t *decimal, const rt_uint32_t times);
rt_err_t rt_hw_dht_init(const char *name, struct rt_sensor_config cfg);
#endif / src_dht11_h_ */
这里dht11 使用的是 gpio 0208,所以需要把这个引脚配置为输入模式
五、烧录验证

华为聚焦6大通用终端能力,形成“3+6+X”的5G应用体系2.0版本
镭神智能完成数亿元D轮融资,进一步加大激光雷达技术研发
技术攻关:区块链技术创新应用在继电保护工作中
积木机器人系列开启了业内实物编程的新时代?
下一代Arm服务器CPU内核:Neoverse V2和E2
使用RT-Thread和CPK-RA2L1采集DHT11温湿度
华为FusionPlant工业互联网平台打造有竞争力的解决方案
光纤微裂纹检测仪测量手机玻璃镜底厚度
振动传感器YTS9512A构成的车辆防盗报警电路
ATA-2161高压放大器的电子实验案例(案例合集)
太阳热水器水位控制的EDA实现
中科大研制出自主知识产权量子计算机控制系统
台积电:美国亚利桑那州第二座晶圆厂投产时间推迟至2027年
独石电容生产工艺
中国工程院院士许祖彦:激光技术为什么是重要战略支撑技术
智慧照明企业华体科技发布2022第一季度报告
在水泥工程机械搅拌行业中称重传感器有哪些广泛应用?
状态机概述 如何理解状态机
多个WS2812灯珠的点亮实验
小米6怎么样?小米6最新消息:三大猜想,小米6或将再一次惊艳世人