Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
瑞萨-RTT活动
RT-Thread一般讨论
CPK-RA2L1评估板PMS1003(4)
发布于 2023-03-12 20:26:32 浏览:319
订阅该版
[tocm] # 一、准备 本篇文章主要介绍使用RT-Thread Studio 和瑞萨 CPK-RA2L1评估板,使用攀藤PMS1003 作为pm2.5 ,pm10等粉尘颗粒信息采集传感器 ![1.png](https://oss-club.rt-thread.org/uploads/20230312/48f90a83e2e3a202262991d5e1ccf1b8.png.webp) # 二、模块信息 通过官网对PMS1003的介绍,可通过uart通信采集数据,以及该传感器的各项技术性能指标 ![2.png](https://oss-club.rt-thread.org/uploads/20230312/98124c9fae18cff947354377a40ea49f.png.webp) [pms1003.pdf](https://club.rt-thread.org/file_download/abcbed7595fe2699) # 三、新建工程 根据上面了解的模块信息,使用UART0 作为通信接口,注意波特率为:9600 ,数据包长32字节 ![3.png](https://oss-club.rt-thread.org/uploads/20230312/5670fedfc2f3cd5cc49beccc041feb74.png.webp) 在RT-Thread Settings种配置好 UART驱动程序,和打开硬件模块的UART0 开关 ![4.png](https://oss-club.rt-thread.org/uploads/20230312/5bf9bd5854d6b8dc2a160965ba8e1fef.png.webp) ![5.png](https://oss-club.rt-thread.org/uploads/20230312/ba588ebb3c51d463cd7d3526acf384e8.png.webp) 在 RA Smart Configurator 种添加UART0,配置好引脚和波特率 ![6.png](https://oss-club.rt-thread.org/uploads/20230312/d6a0c650d4372c5d99361d7767280816.png) # 四、代码实现 ```c /* * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2023-02-25 DYC the first version */ #define DBG_SECTION_NAME "pms_series" #define DBG_LEVEL DBG_LOG #define DBG_COLOR #include
#include
#include "pms1003.h" #define RT_SERIAL_RB_BUFSZ 64 #define COMM_START1 0x42 #define COMM_START2 0x4D #define FRAME_HEAD1 0x00 #define FRAME_HEAD2 0x01 #define FRAME_LENH 0x02 #define FRAME_LENL 0x03 #define FRAME_RECEIVE 0x04 #define FRAME_CHECK 0x05 rt_err_t frame_check(pms_device_t dev,rt_uint8_t *buf,rt_uint16_t len) { rt_uint16_t sum=0; RT_ASSERT(dev); for(uint8_t i=0;i<(len-2);i++) { sum += buf[i]; } if((buf[len-1] == (sum&0xFF)) && (buf[len-2] == (sum >> 8))) { dev->PM1_0_CF1 = ((rt_uint16_t)(buf[4])<<8) | buf[5]; dev->PM2_5_CF1 = ((rt_uint16_t)(buf[6])<<8) | buf[7]; dev->PM10_0_CF1 = ((rt_uint16_t)(buf[8])<<8) | buf[9]; dev->PM1_0_amb = ((rt_uint16_t)(buf[10])<<8) | buf[11]; dev->PM2_5_amb = ((rt_uint16_t)(buf[12])<<8) | buf[13]; dev->PM10_0_amb = ((rt_uint16_t)(buf[14])<<8) | buf[15]; dev->air_0_3um = ((rt_uint16_t)(buf[16])<<8) | buf[17]; dev->air_0_5um = ((rt_uint16_t)(buf[18])<<8) | buf[19]; dev->air_1_0um = ((rt_uint16_t)(buf[20])<<8) | buf[21]; dev->air_2_5um = ((rt_uint16_t)(buf[22])<<8) | buf[23]; dev->version = buf[len - 4]; dev->errorCode = buf[len - 3]; return RT_EOK; } return RT_ERROR; } rt_err_t pms_get_byte(pms_device_t dev, char data) { rt_err_t result; static uint8_t state = FRAME_HEAD1; static uint8_t cnt = 0; static rt_uint8_t buf[40] = {0}; RT_ASSERT(dev); if(state == FRAME_HEAD1 && data == COMM_START1) { buf[cnt++] = data; state = FRAME_HEAD2; } else if (state == FRAME_HEAD2 && data == COMM_START2) { buf[cnt++] = data; state = FRAME_LENH; } else if (state == FRAME_LENH) { buf[cnt++] = data; state = FRAME_LENL; } else if (state == FRAME_LENL) { buf[cnt++] = data; state = FRAME_RECEIVE; } else if (state == FRAME_RECEIVE) { buf[cnt++] = data; if(cnt >= COMM_LEN - 1) state = FRAME_CHECK; } else if (state == FRAME_CHECK) { buf[cnt++] = data; state = FRAME_HEAD1; cnt = 0; result = frame_check(dev, buf, COMM_LEN); if (result == RT_EOK) { LOG_D("check success"); return result; } else { LOG_E("check error"); } } else {} return result; } pms_device_t pms_init(const char *uart_name) { pms_device_t dev; RT_ASSERT(uart_name); dev = rt_calloc(1, sizeof(struct pms_device)); if (dev == RT_NULL) { LOG_E("Can't allocate memory for pms device %s",uart_name); return RT_NULL; } dev->serial = rt_device_find(uart_name); if (!dev->serial) { rt_free(dev); rt_kprintf("find %s failed!\n", uart_name); } else { dev->config.baud_rate = BAUD_RATE_9600; dev->config.data_bits = DATA_BITS_8; dev->config.stop_bits = STOP_BITS_1; dev->config.parity = PARITY_NONE; dev->config.bit_order = BIT_ORDER_LSB; dev->config.invert = NRZ_NORMAL; dev->config.rx_bufsz = RT_SERIAL_RB_BUFSZ; dev->config.reserved = 0; rt_device_control(dev->serial, RT_DEVICE_CTRL_CONFIG, &dev->config); } return dev; } void pms_deinit(pms_device_t dev) { RT_ASSERT(dev); rt_free(dev); } ``` ```c /* * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2023-02-25 DYC the first version */ #ifndef SRC_PMS1003_H_ #define SRC_PMS1003_H_ #define DBG_TAG "pms_series" #define DBG_LVL DBG_INFO #include
#include
#include
#define COMM_LEN 32 struct pms_device { rt_device_t serial; struct serial_configure config; rt_uint16_t len; rt_uint16_t PM1_0_CF1; rt_uint16_t PM2_5_CF1; rt_uint16_t PM10_0_CF1; rt_uint16_t PM1_0_amb; rt_uint16_t PM2_5_amb; rt_uint16_t PM10_0_amb; rt_uint16_t air_0_3um; rt_uint16_t air_0_5um; rt_uint16_t air_1_0um; rt_uint16_t air_2_5um; rt_uint16_t air_5_0um; rt_uint16_t air_10_0um; rt_uint8_t version; rt_uint8_t errorCode; rt_uint16_t checksum; }; typedef struct pms_device *pms_device_t; pms_device_t pms_init(const char *uart_name); rt_err_t frame_check(pms_device_t dev,rt_uint8_t *buf,rt_uint16_t len); rt_err_t pms_get_byte(pms_device_t dev, char data); void pms_deinit(pms_device_t dev); #endif /* SRC_PMS1003_H_ */ ``` ```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 LED pins */ #define DBG_ENABLE #define DBG_SECTION_NAME "pms_series" #define DBG_LEVEL DBG_LOG #define DBG_COLOR #include
#include
#define PMS_SERIES_UART "uart0" #define DBG_ENABLE struct rx_msg { rt_device_t dev; rt_size_t size; }; #ifdef PMS_SERIES_SAMPLE_USING_DMA struct rt_messagequeue pms_mq; #else struct rt_semaphore pms_sem; #endif void pms_series_debug(pms_device_t dev) { LOG_D("*********************************begin***********************************"); LOG_D("PM1_0_CF1 = %5d\tPM2_5_CF1 = %5d\tPM10_0_CF1 = %5d",dev->PM1_0_CF1,dev->PM2_5_CF1,dev->PM10_0_CF1); LOG_D("PM1_0_amb = %5d\tPM2_5_amb = %5d\tPM10_0_amb = %5d",dev->PM1_0_amb,dev->PM2_5_amb,dev->PM10_0_amb); LOG_D("air_0_3um = %5d\tair_0_5um = %5d\tair_1_0um = %5d",dev->air_0_3um,dev->air_0_5um,dev->air_1_0um); LOG_D("air_2_5um = %5d\t",dev->air_2_5um); LOG_D("version = %5d errorCode = %5d",dev->version,dev->errorCode); LOG_D("********************************over*************************************"); } static void serial_thread_entry(void *parameter) { #ifndef PMS_SERIES_SAMPLE_USING_DMA rt_err_t result; char ch; pms_device_t dev = parameter; while (1) { while (rt_device_read(dev->serial, 0, &ch, 1) == 0) { rt_sem_control(&pms_sem, RT_IPC_CMD_RESET, RT_NULL); rt_sem_take(&pms_sem, RT_WAITING_FOREVER); } result = pms_get_byte(dev,ch); if (result == RT_EOK) { pms_series_debug(dev); } } #endif #ifdef PMS_SERIES_SAMPLE_USING_DMA struct rx_msg msg; rt_err_t result; rt_uint32_t rx_length; static rt_uint8_t rx_buffer[RT_SERIAL_RB_BUFSZ + 1]; pms_device_t dev = parameter; while (1) { rt_memset(&msg, 0, sizeof(msg)); result = rt_mq_recv(&pms_mq, &msg, sizeof(msg), RT_WAITING_FOREVER); if (result == RT_EOK) { rx_length = rt_device_read(msg.dev, 0, rx_buffer, msg.size); rx_buffer[rx_length] = '\0'; result = frame_check(dev,rx_buffer,rx_length); if (result == RT_EOK) { pms_series_debug(dev); LOG_D("rx buff success"); } else { LOG_E("rx buff error"); } } } #endif } static rt_err_t uart_input(rt_device_t dev, rt_size_t size) { RT_ASSERT(dev); #ifndef PMS_SERIES_SAMPLE_USING_DMA if (size > 0) { rt_sem_release(&pms_sem); } return RT_EOK; #endif #ifdef PMS_SERIES_SAMPLE_USING_DMA rt_err_t result; struct rx_msg msg; msg.dev = dev; msg.size = size; result = rt_mq_send(&pms_mq, &msg, sizeof(msg)); if ( result == -RT_EFULL) { rt_kprintf("message queue full!\n"); } return result; #endif } //int pms_sample(int argc, char *argv[]) int pms_sample() { static pms_device_t dev = NULL; rt_err_t ret = RT_EOK; dev = pms_init(PMS_SERIES_UART); #ifndef PMS_SERIES_SAMPLE_USING_DMA rt_sem_init(&pms_sem, "pms_sem", 0, RT_IPC_FLAG_FIFO); rt_device_open(dev->serial, RT_DEVICE_FLAG_INT_RX); rt_device_set_rx_indicate(dev->serial, uart_input); #endif #ifdef PMS_SERIES_SAMPLE_USING_DMA static char msg_pool[256]; rt_err_t result; result = rt_mq_init(&pms_mq, "pms_mq", msg_pool, sizeof(struct rx_msg), sizeof(msg_pool), RT_IPC_FLAG_FIFO); if (result != RT_EOK) { LOG_E("init message queue failed.\n"); } result = rt_device_open(dev->serial, RT_DEVICE_FLAG_DMA_RX); if (result != RT_EOK) { LOG_E("open device failed.\n"); } result = rt_device_set_rx_indicate(dev->serial, uart_input); if (result != RT_EOK) { LOG_E("set rx indicate failed.\n"); } #endif rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, dev, 1024, 3, 10); if (thread != RT_NULL) { rt_thread_startup(thread); } else { ret = RT_ERROR; } return ret; } INIT_APP_EXPORT(pms_sample); //MSH_CMD_EXPORT(pms_sample, pms_series drive sample); void hal_entry(void) { rt_kprintf("\nHello RT-Thread!\n"); rt_uint32_t led1_pin = rt_pin_get(LED1_PIN); while (1) { rt_pin_write(led1_pin, PIN_HIGH); rt_thread_mdelay(500); rt_pin_write(led1_pin, PIN_LOW); rt_thread_mdelay(500); } } ``` # 五、 烧录验证 ![7.png](https://oss-club.rt-thread.org/uploads/20230312/21010c4aa4c0fc753b2a3e8079232da4.png)
0
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
快乐小鸟
这家伙很懒,什么也没写!
文章
5
回答
0
被采纳
0
关注TA
发私信
相关文章
1
有关动态模块加载的一篇论文
2
最近的调程序总结
3
晕掉了,这么久都不见layer2的踪影啊
4
继续K9ii的历程
5
[GUI相关] FreeType 2
6
[GUI相关]嵌入式系统中文输入法的设计
7
20081101 RT-Thread开发者聚会总结
8
嵌入式系统基础
9
linux2.4.19在at91rm9200 上的寄存器设置
10
[转]基于嵌入式Linux的通用触摸屏校准程序
推荐文章
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总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
I2C_IIC
ESP8266
UART
WIZnet_W5500
ota在线升级
PWM
cubemx
flash
freemodbus
BSP
packages_软件包
潘多拉开发板_Pandora
定时器
ADC
flashDB
GD32
socket
编译报错
中断
Debug
rt_mq_消息队列_msg_queue
SFUD
msh
keil_MDK
ulog
C++_cpp
MicroPython
本月问答贡献
xusiwei1236
5
个答案
2
次被采纳
踩姑娘的小蘑菇
1
个答案
2
次被采纳
用户名由3_15位
7
个答案
1
次被采纳
bernard
4
个答案
1
次被采纳
张世争
1
个答案
1
次被采纳
本月文章贡献
聚散无由
2
篇文章
15
次点赞
catcatbing
2
篇文章
5
次点赞
Wade
2
篇文章
2
次点赞
Ghost_Girls
1
篇文章
6
次点赞
YZRD
1
篇文章
2
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部