Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
dht11
RT-Thread活动
瑞萨_RA6M4
基于RT-Thread+RA6M4的环境采集系统
发布于 2022-07-17 19:20:35 浏览:560
订阅该版
[tocm] [202207172031.mp4](https://club.rt-thread.org/file_download/be71cb6068a5c20a)很高兴能参加此次活动 https://club.rt-thread.org/ask/article/a07e2bf367df1195.html 瑞萨这款M4开发板功能比较强悍,官方支持也比较友好 https://gitee.com/rtthread/rt-thread/tree/gitee_master/bsp/renesas ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20220717/10b7c6edf2f91c58c9b95ab3ee6d72de.png) 官方已支持uart iic(软件模拟) gpio 等功能,并且通过两次直播也进行了外设使用的介绍 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20220717/fa1cd9a894a51f770879b92db1de1f0f.png) # 概要设计 本次主要基于此开发板完成温度、湿度、甲醛浓度的环境量采集功能,并通过iic 的OLED 12864进行显示,整体实现框图如下 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20220717/cd7bcc6fd630e62e5544e73a268b1f89.png) # 详细设计 ## oled显示部分 环境搭建参考https://blog.csdn.net/vor234/article/details/125634313 软件包添加u8g2 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20220717/2db0320a80d8a8d7d118939feb66ea77.png) u8g2设置 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20220717/86fe02a01e1c53ad7bd25227941b9759.png) 开启iic 软件模拟 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20220717/1802cc46aa732550cc2e1c5a6ef5a55c.png) 线程初始化过程 ```c rt_err_t ret = RT_EOK; // Initialization u8g2_Setup_ssd1306_i2c_128x64_noname_f( &u8g2, U8G2_R0, u8x8_byte_rtthread_hw_i2c, u8x8_gpio_and_delay_rtthread); u8g2_InitDisplay(&u8g2); u8g2_SetPowerSave(&u8g2, 0); rt_thread_t thread = rt_thread_create("ssd1306_u8g2", ssd1306_entry, RT_NULL, 1024, 14, 14); if (thread != RT_NULL) { rt_thread_startup(thread); } else { ret = RT_ERROR; } return ret;``` entry 部分 ```c static void ssd1306_entry(void* params) { while(1) { u8g2_ClearBuffer(&u8g2); u8g2_SetFont(&u8g2, u8g2_font_wqy12_t_gb2312a); u8g2_DrawUTF8(&u8g2, 24, 12, "RA6M4环境采集"); u8g2_DrawUTF8Line(&u8g2, 0, 20, 1, "----------------------------", 0, 0); snprintf(datatmp, sizeof(datatmp) - 1, "甲醛: %fmg/m^3", CHO_Data); u8g2_DrawUTF8(&u8g2, 4, 32, datatmp); snprintf(datatmp,sizeof(datatmp)-1,"温度:%d.%d 摄氏度",temp_data.data.temp/10,temp_data.data.temp%10); u8g2_DrawUTF8(&u8g2, 4, 44, datatmp); snprintf(datatmp,sizeof(datatmp)-1,"湿度:%d%%",humi_data.data.humi/10); u8g2_DrawUTF8(&u8g2, 4, 56, datatmp); u8g2_SendBuffer(&u8g2); //rt_hw_us_delay(500000); rt_thread_delay(rt_tick_from_millisecond(500)); } }``` 此线程使用全局变量,500ms 刷新界面显示,并设置中文显示字体及大小,注意此文件要以utf8格式编码保存,不然显示屏字会乱码或不显示 ## dht11 部分 ### 管脚指定 dht11 采用p102 管脚,管脚定义的内核值计算在bsp_io.h文件中有定义,可做参考。 针对此cpk 板子在bsp_pin_cfg.h也有相应的宏定义,方便进行查找,不用找原理图了 ,嘻嘻, ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20220717/39da771f58b88fc17b6dc1573a1f1967.png) 取消库里hw delay的实现,也可添加drv_comm 进行systick 引用 fsp 将相应管脚打开,如下图,不打开将无法检测到温度 湿度信息 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20220717/9b7a3bbb7f1f5491da6a31e105ce180e.png) 线程初始化 ```c static int dht11_thread(void) { humi_dev = rt_device_find("humi_dht"); if(!humi_dev) { rt_kprintf("can not find humi device\n"); return -1; } if (rt_device_open(humi_dev, RT_DEVICE_FLAG_RDWR)) { rt_kprintf("Open humi device failed.\n"); return -1; } temp_dev = rt_device_find("temp_dht"); if (!temp_dev) { rt_kprintf("Can't find temp device.\n"); return -1 ; } if (rt_device_open(temp_dev, RT_DEVICE_FLAG_RDWR)) { rt_kprintf("Open temp device failed.\n"); return -1; } rt_thread_t dht11_thread; dht11_thread = rt_thread_create("dht11_thread", dht11_entry, RT_NULL, 1024, 14, 20); if(dht11_thread) { rt_thread_startup(dht11_thread); } } ``` entry 部分 通过rt_device_read 读取 传感器数据,并存入到rt_sensor_data 中,方便oled进行数据显示 ```c static void dht11_entry(void* params) { while(1) { if (1 != rt_device_read(humi_dev, 0, &humi_data, 1)) { rt_kprintf("Read humi data failed.\n"); } else { //rt_kprintf("[%d] Humi: %d\n", humi_data.timestamp, humi_data.data.humi); } if (1 != rt_device_read(temp_dev, 0, &temp_data, 1)) { rt_kprintf("Read temp data failed.\n"); } else { //rt_kprintf("[%d] Temp: %d\n", temp_data.timestamp, temp_data.data.temp); } rt_thread_mdelay(3000); } } ``` ## 甲醛模块实现 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20220717/3d556e822f3b14567a86b35639692511.png.webp) ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20220717/7e832e28673b75202aa20ac548dccc4e.png.webp) 管脚定义 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20220717/7510af4386b7d8949416876fc89ab389.png.webp) 串口通信格式 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20220717/99cd73652fce8697c68a1f1f842e99c6.png) ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20220717/691c97b8308da1c23b35e316073c706e.png.webp) 注意事项 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20220717/e78ab17e75a1379d18718e4db62e337b.png.webp) 数据计算过程 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20220717/8d3bf55d6e6f517f224d2d5a8e911172.png) Byte5 +Byte4*256=ppb ppm=ppb/1000 ppm与浓度转换关系为 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20220717/43aadaf20c24dbb1165e5be3de9d0d1f.png) 由https://baike.baidu.com/item/%E7%94%B2%E9%86%9B/167033?fr=kg_qa#reference-[2]-4913-wrap 百科可知甲醛分子量为30.3 则 质量浓度= ppm*30.3/22.4 空气含量标准 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20220717/219d49fe87d4ad70c29d926d1d3c94f2.png.webp) 实现过程 fsp 开启uart1 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20220717/b69620cff0225f7718332f1e55b7bc3c.png.webp) rt-thread setting 中开启uart1 线程创建过程 ```c static int uart_CH0_thread(void) { rt_err_t ret = RT_EOK; char uart_name[RT_NAME_MAX]; rt_strncpy(uart_name, CHO_UART_NAME, RT_NAME_MAX); /* 查找串口设备 */ serial = rt_device_find(uart_name); if (!serial) { rt_kprintf("find %s failed!\n", uart_name); return RT_ERROR; } rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO); rt_device_open(serial, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX); rt_device_set_rx_indicate(serial, uart_in); rt_thread_t thread = rt_thread_create("serial_cho", uart_CHO_thread_entry, RT_NULL, 1024, 25, 9); if (thread != RT_NULL) { rt_thread_startup(thread); } else { ret = RT_ERROR; } return ret; } ``` 协议解析过程 并将解析完成数据放到CHO_Data 中,方便oled 动态刷新显示 ``` static void uart_CHO_thread_entry(void* parameter) { uint8_t data; uint8_t state = 0; uint32_t dataH,dataL; float tmpdata =0.0; char cho_data[40]; while(1) { while (rt_device_read(serial, -1, &data, 1) != 1) { /* 阻塞等待接收信号量,等到信号量后再次读取数据 */ rt_sem_take(&rx_sem, RT_WAITING_FOREVER); } switch(state) { case HeadByte: state = data==0xff?NameByte:HeadByte; break; case NameByte: state = data==0x17?UintByte:HeadByte; break; case UintByte: state = data==0x04?MinByte:HeadByte; break; case MinByte: state = data==0x00?DataHByte:HeadByte; break; case DataHByte: dataH = data; state = DataLByte; break; case DataLByte: dataL = data; state = FullHByte; break; case FullHByte: state = data==0x13?FullLByte:HeadByte; break; case FullLByte: state = data==0x88?CRCByte:HeadByte; break; case CRCByte: if((data+0x17+0x04+dataH+dataL+0x13+0x88)%256==0) { tmpdata = (float)(dataH*256+dataL)*0.001f; CHO_Data = tmpdata*1.352678571428f; //snprintf(cho_data, sizeof(cho_data) - 1, "CH0:%fmg/m^3", CHO_Data); //rt_kprintf(" %s\n",cho_data); } state =0; break; default: state =0; break; } } }``` 最终实现效果 ![screenshot_af2c6b2ab84e51e96c78560d566b8d6.jpg](https://oss-club.rt-thread.org/uploads/20220717/b8748f30e5414832289b425e4aa007dd.jpg.webp) 开源代码地址 https://gitee.com/xinshuwei/ra6m4-environment-collector 视频演示地址 「VID_20220717_193333.mp4」https://www.aliyundrive.com/s/ieyu9fJS1nQ
2
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
xinshuwei
这家伙很懒,什么也没写!
文章
8
回答
0
被采纳
0
关注TA
发私信
相关文章
1
(苏州站)RT-Thread物联网开发者沙龙【已结束】
2
(成都站)RT-Thread物联网开发者沙龙
3
(深圳站)RT-Thread物联网开发者沙龙
4
(西安站)RT-Thread物联网开发者沙龙
5
成都站2018 RT-Thread开发者沙龙回顾及PPT下载
6
2018 RT-Thread物联网开发者沙龙(北京站)
7
2018 RT-Thread物联网开发者沙龙(南京站)
8
第十三届研电赛RT-Thread企业专项奖发布通知
9
RT-Thread应用作品征集大赛开始啦!
10
你的投票将决定RT-Thread官网应该优先准备的文档是哪些
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
ART-Pi
FinSH
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
rt-smart
FAL
ESP8266
I2C_IIC
WIZnet_W5500
ota在线升级
UART
cubemx
PWM
flash
packages_软件包
freemodbus
BSP
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
中断
Debug
编译报错
msh
keil_MDK
SFUD
rt_mq_消息队列_msg_queue
C++_cpp
at_device
ulog
本月问答贡献
出出啊
1516
个答案
342
次被采纳
小小李sunny
1440
个答案
289
次被采纳
张世争
793
个答案
171
次被采纳
crystal266
547
个答案
161
次被采纳
whj467467222
1222
个答案
148
次被采纳
本月文章贡献
出出啊
1
篇文章
1
次点赞
小小李sunny
1
篇文章
1
次点赞
张世争
1
篇文章
4
次点赞
crystal266
2
篇文章
2
次点赞
whj467467222
2
篇文章
1
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部