【RA2L1开发实践】- 温湿度检测平台

主要驱动代码
dht11.c
/*
copyright (c) 2006-2021, rt-thread development teamspdx-license-identifier: apache-2.0change logs:date author notes2023-03-15 xiaodai the first version
/
#ifndef src_dht11_c_
#define src_dht11_c_
#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);
}
#endif / src_dht11_c_ /
/copyright (c) 2006-2021, rt-thread development teamspdx-license-identifier: apache-2.0change logs:date author notes2023-03-15 xiaodai the first version
*/
#ifndef src_dht11_h_
#define src_dht11_h_
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_ */
0.96oled代码
void lcd_thread_handler(void *parameter)
{
lcd_init();
lcd_fill(0,0,lcd_w,lcd_h,white);
lcd_show();
rt_thread_mdelay(1000);
lcd_fill(0,0,lcd_w,lcd_h,white);
while(1)
{
lcd_show_pic1();
rt_thread_mdelay(500);
}
}
if(key_num ==1)
{
// u8g2_clearbuffer(&u8g2);
u8g2_drawstr(&u8g2, 1, 40, led2 is on);
u8g2_sendbuffer(&u8g2);
}
else {
// u8g2_clearbuffer(&u8g2);
u8g2_drawstr(&u8g2, 1, 40, led2 is off);
u8g2_sendbuffer(&u8g2);
}
hal_entry.c
/*
copyright (c) 2006-2021, rt-thread development team
spdx-license-identifier: apache-2.0
change logs:
date author notes
2021-10-10 sherman first version
/
#include
#include hal_data.h
#include
#define led1_pin p502 / onboard led1 pins /
#define led2_pin p501 / onboard led2 pins */
#define user_input p004
rt_uint32_t pin ;
int key_num ;
void hal_entry(void)
{
rt_kprintf(nhello rt-thread!n);
while (1)
{
if( pin == 1)
{
rt_kprintf(nuser_input push !n);
rt_pin_write(led2,pin_high);
}
rt_thread_mdelay(500);
}
}
void irq_callback_test(void args)
{
static int out ,out2 ;
rt_kprintf(n irq03 triggered n);
rt_uint32_t led1_pin = rt_pin_get(led1_pin);
rt_uint32_t led2_pin = rt_pin_get(led2_pin);
out = rt_pin_read(led1_pin)?pin_low:pin_high;
out2 = rt_pin_read(led2_pin)?pin_low:pin_high;
rt_pin_write(led1_pin,out);
rt_pin_write(led2_pin,out2);
key_num++;
if( pin == 0 && key_num ==2)
{
rt_kprintf(n led2 is on !n);
rt_pin_write(led2,pin_high);
}
else {
rt_kprintf(n led2 is off !n);
rt_pin_write(led2,pin_low);
}
if(key_num ==2 )
key_num =0;
}
void push_btn(void)
{
/ init */
rt_uint32_t pin = rt_pin_get(user_input);
rt_kprintf(n pin number : 0x%04x n, pin);
rt_err_t err = rt_pin_attach_irq(pin, pin_irq_mode_rising, irq_callback_test, rt_null);
if (rt_eok != err)
{
rt_kprintf(n attach irq failed. n);
}
err = rt_pin_irq_enable(pin, pin_irq_enable);
if (rt_eok != err)
{
rt_kprintf(n enable irq failed. n);
}
}
msh_cmd_export(push_btn, push_btn);
偶然之中在源代码中发现官方已经帮我们定义好了有些引脚,所以就没必要重复定义了

元萝卜对弈围棋世界冠军见分晓,棋圣聂卫平解说妙语连珠
解密人脸识别安全性,汉柏科技引领行业正向发展
口算训练机/VK1056B 14X4 LCD段码液晶驱动芯片
中国空间站预计2022年前后建成
中国可穿戴设备市场二季度出现回暖,同比增长17.3%
【RA2L1开发实践】- 温湿度检测平台
新型机器人学会写字 还会画简版《蒙娜丽莎》
UTC推出5.5寸光学感应器,兼容军用民用无人机
工业平板定制_基于MT6762核心板的三防工业平板电脑方案
ios10.3最新消息:苹果ios10.3支持ios10.4操作系统,ios10.3自曝神秘新功能,还不赶快升级?
中国哪座城市是智慧城市的先行者
物联网时代之ZigBee到来
Facebook收购计算机视觉公司Zurich Eye
利用智能人工气候室来调整农作物种植的环境
TicWatchPro4G评测 为可穿戴产品在4G道路上提供新的方向
新能源电驱系统高压绝缘零件设计
美升级版宽带卫星通信系统将升空 带宽多了45%
Jupyter Notebook自动补全工具的使用
EMI的含义以及电磁干扰要素的解析
为什么智能音箱没有内置电池呢?