0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

RA-RTT RA2L1 PM2.5采集顯示器

冬至子 ? 來源:kun222 ? 作者:kun222 ? 2023-10-12 14:08 ? 次閱讀

二、工作原理

通過串口讀取傳感器數(shù)值解釋完后,顯示到數(shù)碼管,數(shù)碼管顯示方式為動態(tài)掃描
程序分為2個線程,一個數(shù)據(jù)解釋線程,一個數(shù)碼管掃描顯示線程

三、硬件連接

1.jpg

四、rt-thread studio驅(qū)動配置

打開UART0

1.jpg

五、RA Smart Configurator配置

添加g_uart0 UART驅(qū)動

1.jpg

配置SC0為異步串口,選擇101 100引腳

1.jpg

六、代碼編寫

PM2.5 接收及解釋

#include
#include
#include
#include "display.h"
#define DBG_TAG "app.pm25"
#define DBG_LVL DBG_LOG
#include
static rt_device_t serial;
static pm25_state_t pm25_state;
uint16_t pm25_data_convert(uint8_t h, uint8_t l)
{
return h<<8|l;
}
void pm25_thread_entry(void parameter)
{
uint8_t ch;
serial = rt_device_find("uart0");
if (!serial)
{
LOG_E("find %s failed!n", "uart1");
return;
}
else{
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; /
初始化配置參數(shù) /
/
step2:修改串口配置參數(shù) /
config.baud_rate = BAUD_RATE_9600; //修改波特率為 9600
config.data_bits = DATA_BITS_8; //數(shù)據(jù)位 8
config.stop_bits = STOP_BITS_1; //停止位 1
config.tx_bufsz = 0; //修改緩沖區(qū) buff size 為 128
config.rx_bufsz = 128; //修改緩沖區(qū) buff size 為 128
config.parity = PARITY_NONE; //無奇偶校驗位
/
step3:控制串口設備。通過控制接口傳入命令控制字,與控制參數(shù) */
rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);
rt_device_open(serial, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING);
}
while(1){
// rt_thread_delay(10);
if( rt_device_read(serial, -1, &ch, 1) > 0 ){
pm25_state.recv_time = 10;
switch( pm25_state.recv_state )
{
case RECV_START1:
if( ch==0x42 ){
pm25_state.recv_state++;
pm25_state.recv_buf[pm25_state.recv_index++] = ch;
}
break;
case RECV_START2:
if( ch==0x4d ){
pm25_state.recv_state++;
pm25_state.recv_buf[pm25_state.recv_index++] = ch;
}
break;
case RECV_LEN1:
pm25_state.recv_state++;
pm25_state.recv_buf[pm25_state.recv_index++] = ch;
pm25_state.recv_len = ch<<8;
break;
case RECV_LEN2:
pm25_state.recv_state++;
pm25_state.recv_buf[pm25_state.recv_index++] = ch;
pm25_state.recv_len |= ch;
break;
case RECV_DATA:
if( pm25_state.recv_index < pm25_state.recv_len ){
pm25_state.recv_buf[pm25_state.recv_index++] = ch;
if( pm25_state.recv_index == pm25_state.recv_len ){
pm25_state.recv_state++;
}
}
break;
case RECV_END:
// LOG_HEX("REPLY", 16, pm25_state.recv_buf, 32);
pm25_state.recv_state = 0;
pm25_state.recv_index = 0;
pm25_decode_t *pm25_decode_ptr = (pm25_decode_t *)pm25_state.recv_buf;
pm25_state.pm25_data.pm1_0 = pm25_data_convert(pm25_decode_ptr->pm1_0_h,pm25_decode_ptr->pm1_0_l);
pm25_state.pm25_data.pm2_5 = pm25_data_convert(pm25_decode_ptr->pm2_5_h,pm25_decode_ptr->pm2_5_l);
pm25_state.pm25_data.pm10 = pm25_data_convert(pm25_decode_ptr->pm10_h,pm25_decode_ptr->pm10_l);
pm25_state.pm25_data.pm1_0_air = pm25_data_convert(pm25_decode_ptr->pm1_0_air_h,pm25_decode_ptr->pm1_0_air_l);
pm25_state.pm25_data.pm2_5_air = pm25_data_convert(pm25_decode_ptr->pm2_5_air_h,pm25_decode_ptr->pm2_5_air_l);
pm25_state.pm25_data.pm10_air = pm25_data_convert(pm25_decode_ptr->pm10_air_h,pm25_decode_ptr->pm10_air_l);
pm25_state.pm25_data.quantity_0_3 = pm25_data_convert(pm25_decode_ptr->quantity_0_3_h,pm25_decode_ptr->quantity_0_3_l);
pm25_state.pm25_data.quantity_0_5 = pm25_data_convert(pm25_decode_ptr->quantity_0_5_h,pm25_decode_ptr->quantity_0_5_l);
pm25_state.pm25_data.quantity_1_0 = pm25_data_convert(pm25_decode_ptr->quantity_1_0_h,pm25_decode_ptr->quantity_1_0_l);
pm25_state.pm25_data.quantity_2_5 = pm25_data_convert(pm25_decode_ptr->quantity_2_5_h,pm25_decode_ptr->quantity_2_5_l);
pm25_state.pm25_data.quantity_5_0 = pm25_data_convert(pm25_decode_ptr->quantity_5_0_h,pm25_decode_ptr->quantity_5_0_l);
pm25_state.pm25_data.quantity_10 = pm25_data_convert(pm25_decode_ptr->quantity_10_h,pm25_decode_ptr->quantity_10_l);
pm25_state.pm25_data.version = pm25_decode_ptr->version;
pm25_state.pm25_data.err_code = pm25_decode_ptr->err_code;
display_dig(pm25_state.pm25_data.pm2_5_air);
//
// LOG_D("pm1_0 = %d",pm25_state.pm25_data.pm1_0);
// LOG_D("pm2_5 = %d",pm25_state.pm25_data.pm2_5);
// LOG_D("pm10 = %d",pm25_state.pm25_data.pm10);
//
// LOG_D("pm1_0_air = %d",pm25_state.pm25_data.pm1_0_air);
// LOG_D("pm2_5_air = %d",pm25_state.pm25_data.pm2_5_air);
// LOG_D("pm10_air = %d",pm25_state.pm25_data.pm10_air);
//
// LOG_D("quantity_0_3 = %d",pm25_state.pm25_data.quantity_0_3);
// LOG_D("quantity_0_5 = %d",pm25_state.pm25_data.quantity_0_5);
// LOG_D("quantity_1_3 = %d",pm25_state.pm25_data.quantity_1_0);
// LOG_D("quantity_2_5 = %d",pm25_state.pm25_data.quantity_2_5);
// LOG_D("quantity_5_0 = %d",pm25_state.pm25_data.quantity_5_0);
// LOG_D("quantity_10 = %d",pm25_state.pm25_data.quantity_10);
// LOG_D("n");
break;
default:
break;
}
if( pm25_state.recv_time > 0 ){
pm25_state.recv_time --;
if( pm25_state.recv_time==0 ){
pm25_state.recv_state = RECV_START1;
pm25_state.recv_index = 0;
LOG_E("recv timeout");
}
}
}
// rt_device_write(serial, 0,"hello world",11);
rt_thread_delay(10);
}
}
void pm25_init(void)
{
rt_thread_t tid1 = RT_NULL;
tid1 = rt_thread_create("pm25",
pm25_thread_entry, RT_NULL,
512,
10, 10);
if (tid1 != RT_NULL)
rt_thread_startup(tid1);
}
INIT_APP_EXPORT(pm25_init);

595動態(tài)掃描顯示線程

/*

Copyright (c) 2006-2021, RT-Thread Development Team

SPDX-License-Identifier: Apache-2.0

Change Logs:
Date Author Notes
2023-02-18 dmjku the first version
*/
#include
#include
#include "display.h"
disp_buf_t disp_buf;
#define SERIAL_RCLK_PIN "P302"
#define SERIAL_SCLK_PIN "P303"
#define SERIAL_DIO_PIN "P301"
rt_uint32_t rclk_pin;
rt_uint32_t sclk_pin;
rt_uint32_t dio_pin;
#define serial_rclk_pin(sta) rt_pin_write(rclk_pin, sta)
#define serial_sclk_pin(sta) rt_pin_write(sclk_pin, sta)
#define serial_dio_pin(sta) rt_pin_write(dio_pin, sta)
const uint8_t dig_code[]={
/ 生成 0-f 的編碼 /
0xC0,////0
0xF9,////1
0xA4,////2
0xB0,////3
0x99,////4
0x92,////5
0x82,////6
0xF8,////7
0x80,////8
0x90,////9
0x88,////A
0x80,////B
0xC6,////C
0xC0,////D
0x86,////E
0x8E,////F
};
void display_dig(uint16_t dig)
{
uint8_t zero=0;
if( dig/1000 ){
disp_buf.dig[0] = dig_code[dig/1000];
dig %= 1000;
zero = 1;
}
else{
disp_buf.dig[0] = 0xff;
}
if( dig/100 ){
disp_buf.dig[1] = dig_code[dig/100];
dig %= 100;
zero = 1;
}
else if(zero){
disp_buf.dig[1] = dig_code[0];
}
else{
disp_buf.dig[1] = 0xff;
}
if( dig/10 ){
disp_buf.dig[2] = dig_code[dig/10];
dig %= 10;
zero = 1;
}
else if(zero){
disp_buf.dig[2] = dig_code[0];
}
else{
disp_buf.dig[2] = 0xff;
}
if( dig ){
disp_buf.dig[3] = dig_code[dig];
}
else if(zero){
disp_buf.dig[3] = dig_code[0];
}
else{
disp_buf.dig[3] = 0xff;
}
}
void display_led(void)
{
const uint8_t pos_v[] = {0x08,0x04,0x02,0x01};
static uint8_t pos = 0;
disp_buf.output[0] = pos_v[pos];
disp_buf.output[1] = disp_buf.dig[pos];
pos++;
if( pos>=4 )
pos = 0;
}
static void display_wr_byte(uint8_t data)
{
for( uint8_t i=0; i<8; i++ ){
serial_dio_pin(data&0x80?1:0);
// rt_hw_us_delay(1);
serial_sclk_pin(1);
// rt_hw_us_delay(1);
serial_sclk_pin(0);
data <<= 1;
}
}
static void display_refresh(void)
{
for( uint8_t i=DISP_BUF_LEN; i>0; i-- ){
display_wr_byte(disp_buf.output[i-1]);
}
serial_rclk_pin(1);
// rt_hw_us_delay(1);
serial_rclk_pin(0);
}
void display_thread_entry(void *paramter)
{
display_dig(1);
while( 1 ){
display_led();
display_refresh();
rt_thread_delay(5);
}
}
void display_init(void)
{
rclk_pin = rt_pin_get(SERIAL_RCLK_PIN);
sclk_pin = rt_pin_get(SERIAL_SCLK_PIN);
dio_pin = rt_pin_get(SERIAL_DIO_PIN);
// serial_rclk_pin(1);
// serial_rclk_pin(0);
// serial_sclk_pin(1);
// serial_sclk_pin(0);
// serial_dio_pin(1);
// serial_dio_pin(0);
rt_thread_t tid1 = RT_NULL;
tid1 = rt_thread_create("disp",
display_thread_entry, RT_NULL,
512,
10, 10);
if (tid1 != RT_NULL)
rt_thread_startup(tid1);
}
INIT_APP_EXPORT(display_init);

七、總結(jié)

第一次使用的瑞薩ARM系列芯片,在RT-thread studio的加持下結(jié)合RA Smart Configurator,不需看硬件手冊,可以快速上手開發(fā),真心好用,比keil+evn+stm32cubeMX還要順手

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學習之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • 傳感器
    +關(guān)注

    關(guān)注

    2545

    文章

    50446

    瀏覽量

    751076
  • 數(shù)碼管
    +關(guān)注

    關(guān)注

    32

    文章

    1873

    瀏覽量

    90865
  • ARM芯片
    +關(guān)注

    關(guān)注

    1

    文章

    125

    瀏覽量

    21420
  • stm32cubemx
    +關(guān)注

    關(guān)注

    5

    文章

    280

    瀏覽量

    14713
  • RTThread
    +關(guān)注

    關(guān)注

    7

    文章

    132

    瀏覽量

    40776
收藏 人收藏

    評論

    相關(guān)推薦

    RA-RTT體驗零代碼點亮LED燈

    很榮幸能參加RA-RTT瑞薩開發(fā)板試用活動。目的介紹如何零代碼體驗點亮LED燈。開發(fā)板RA2L1開發(fā)板實現(xiàn)步聚1、打開RT-Thread Studio SDK管理,安裝開發(fā)板支持包:
    發(fā)表于 02-22 15:13

    RA-RTT RA2L1 PM2.5采集顯示器設計

    25_decode_ptr-》pm1_0_l);  pm25_state.pm25_data.pm2_5 = pm25_data_convert(pm25_decode_ptr-》
    發(fā)表于 02-28 15:00

    RA2L1硬件用戶手冊(中英)

    RA2L1硬件用戶手冊(中英)
    發(fā)表于 11-07 19:49 ?6次下載
    <b class='flag-5'>RA2L1</b>硬件用戶手冊(中英)

    RA2L1開發(fā)板資料

    RA2L1開發(fā)板資料
    發(fā)表于 11-07 19:50 ?18次下載
    <b class='flag-5'>RA2L1</b>開發(fā)板資料

    基于RA2L1的串口及J-Link的燒寫說明

    本節(jié)介紹使用串口工具對RA2L1進行程序的燒寫。采用CPK-RA2L1開發(fā)板,硬件如下圖所示。
    的頭像 發(fā)表于 11-25 13:55 ?1676次閱讀

    RA2L1 組用戶手冊:硬件

    RA2L1 組用戶手冊:硬件
    發(fā)表于 01-09 19:06 ?5次下載
    <b class='flag-5'>RA2L1</b> 組用戶手冊:硬件

    RA2L1 組數(shù)據(jù)表

    RA2L1 組數(shù)據(jù)表
    發(fā)表于 01-09 19:06 ?0次下載
    <b class='flag-5'>RA2L1</b> 組數(shù)據(jù)表

    RA2L1 組電容式觸控評估系統(tǒng)快速入門指南

    RA2L1 組電容式觸控評估系統(tǒng)快速入門指南
    發(fā)表于 02-03 19:24 ?0次下載
    <b class='flag-5'>RA2L1</b> 組電容式觸控評估系統(tǒng)快速入門指南

    RA2L1 組用戶手冊:硬件

    RA2L1 組用戶手冊:硬件
    發(fā)表于 06-30 18:50 ?1次下載
    <b class='flag-5'>RA2L1</b> 組用戶手冊:硬件

    RA2L1 組數(shù)據(jù)表

    RA2L1 組數(shù)據(jù)表
    發(fā)表于 06-30 18:50 ?0次下載
    <b class='flag-5'>RA2L1</b> 組數(shù)據(jù)表

    RA2L1 組 Capacitive Touch Evaluation System 用戶手冊

    RA2L1 組 Capacitive Touch Evaluation System 用戶手冊
    發(fā)表于 07-04 20:38 ?0次下載
    <b class='flag-5'>RA2L1</b> 組 Capacitive Touch Evaluation System 用戶手冊

    CPK-RA2L1評估板PMS1003

    本篇文章主要介紹使用RT-Thread Studio 和瑞薩 CPK-RA2L1評估板,使用攀藤PMS1003 作為pm2.5 ,pm10等粉塵顆粒信息采集傳感
    的頭像 發(fā)表于 10-11 11:40 ?617次閱讀
    CPK-<b class='flag-5'>RA2L1</b>評估板PMS1003

    RA2L1上手優(yōu)化BSP占用

    在上手RA2L1開發(fā)板的時候,使用RTT Studio給的BSP進行開發(fā)時發(fā)現(xiàn)默認占用太大了,所以今天就來說一下如何優(yōu)化掉不是很必要的部分(當然芯片資源很大且不在乎的可以不做)。
    的頭像 發(fā)表于 10-12 11:18 ?589次閱讀
    <b class='flag-5'>RA2L1</b>上手優(yōu)化BSP占用

    基于RA2L1實現(xiàn)串口DTC數(shù)據(jù)接收

    基于RA2L1實現(xiàn)串口DTC數(shù)據(jù)接收
    的頭像 發(fā)表于 10-10 09:34 ?457次閱讀
    基于<b class='flag-5'>RA2L1</b>實現(xiàn)串口DTC數(shù)據(jù)接收

    瑞薩RA2L1系列CAN通信應用

    瑞薩RA2L1系列CAN通信應用
    的頭像 發(fā)表于 06-26 08:06 ?528次閱讀
    瑞薩<b class='flag-5'>RA2L1</b>系列CAN通信應用