项目简介
作为数据展示,通常有LCD屏、OLED屏等,这次采用迪文屏来做为显示形式,展示开发板的CAN通信能力。
移植目的
1、使用RT-Thread Sensor模块驱动HS3003温湿度计。
2、使用RT-Thread CAN通信模块将开发板与迪文屏连通
3、将温湿度显示在迪文屏上(非串口通信)
实验用材料
1、N32G457XVL-STB开发板
2、迪文屏EKT-04bB开发板
3、CAN转TTL收发模块
4、HS3003温湿模块
5、CAN总线分析仪等辅助工具。
原理图
接线图
新建RT-Thread Studio工程
按下图示建立H3003_CAN的工程
加入HS3003软件包
点击添加软件包,在搜索框里输入hs300点查询,然后点添加
开启 i2c1:
开启CAN1
在硬件上找到CAN,打开CAN及can1的开关
然后关闭setting,保存更改,等待下载生成代码:
生成工程结束后,激活工程,并编译显示编译通过:
在packages/hs300x-last文件夹下,有hs300x的readme.md文件,大家可以看到使用说明。
添加hs3003初化代码以及读取函数
在readme下面给了初始化的函数:
#include "sensor_renesas_hs300x.h"
#define HS300X_I2C_BUS "i2c2"
int rt_hw_hs300x_port(void)
{
struct rt_sensor_config cfg;
cfg.intf.dev_name = HS300X_I2C_BUS;
cfg.intf.user_data = (void *)HS300X_I2C_ADDR;
rt_hw_hs300x_init("hs300x", &cfg);
return RT_EOK;
}
INIT_ENV_EXPORT(rt_hw_hs300x_port);
把这段加入main.c下面
同时添加读取函数:
添加can初始化以及发送函数
在官方文档里有示例CAN设备 (rt-thread.org)
我们在applications文件夹下面新建can_sample.c。添加代码如下:
#include <rtthread.h>
#include "rtdevice.h"
#define CAN_DEV_NAME "bxcan1"
static struct rt_semaphore rx_sem;
static rt_device_t can_dev;
static rt_err_t can_rx_call(rt_device_t dev, rt_size_t size)
{
rt_sem_release(&rx_sem);
return RT_EOK;
}
static void can_rx_thread(void *parameter)
{
int i;
rt_err_t res;
struct rt_can_msg rxmsg = {0};
rt_device_set_rx_indicate(can_dev, can_rx_call);
#ifdef RT_CAN_USING_HDR
struct rt_can_filter_item items[5] =
{
RT_CAN_FILTER_ITEM_INIT(0x100, 0, 0, 0, 0x700, RT_NULL, RT_NULL),
RT_CAN_FILTER_ITEM_INIT(0x300, 0, 0, 0, 0x700, RT_NULL, RT_NULL),
RT_CAN_FILTER_ITEM_INIT(0x211, 0, 0, 0, 0x7ff, RT_NULL, RT_NULL),
RT_CAN_FILTER_STD_INIT(0x486, RT_NULL, RT_NULL),
{0x555, 0, 0, 0, 0x7ff, 7,}
};
struct rt_can_filter_config cfg = {5, 1, items};
res = rt_device_control(can_dev, RT_CAN_CMD_SET_FILTER, &cfg);
RT_ASSERT(res == RT_EOK);
#endif
while (1)
{
rxmsg.hdr = -1;
rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
rt_device_read(can_dev, 0, &rxmsg, sizeof(rxmsg));
rt_kprintf("ID:%x", rxmsg.id);
for (i = 0; i < 8; i++)
{
rt_kprintf("%2x", rxmsg.data[i]);
}
rt_kprintf("\n");
}
}
int can_send(uint32_t humi,uint32_t temp)
{
struct rt_can_msg msg = {0};
rt_err_t res;
rt_size_t size;
msg.id = 0x78;
msg.ide = RT_CAN_STDID;
msg.rtr = RT_CAN_DTR;
msg.len = 8;
msg.data[0] = (uint8_t)(temp/100);
msg.data[1] = (uint8_t)(temp%100);
msg.data[2] = (uint8_t)(humi/100);
msg.data[3] = (uint8_t)(humi%100);
msg.data[4] = 0x44;
msg.data[5] = 0x55;
msg.data[6] = 0x66;
msg.data[7] = 0x77;
size = rt_device_write(can_dev, 0, &msg, sizeof(msg));
if (size == 0)
{
rt_kprintf("can dev write data failed!\n");
}
return res;
}
int can_sample(int argc, char *argv[])
{
struct rt_can_msg msg = {0};
rt_err_t res;
rt_size_t size;
rt_thread_t thread;
char can_name[RT_NAME_MAX];
if (argc == 2)
{
rt_strncpy(can_name, argv[1], RT_NAME_MAX);
}
else
{
rt_strncpy(can_name, CAN_DEV_NAME, RT_NAME_MAX);
}
can_dev = rt_device_find(can_name);
if (!can_dev)
{
rt_kprintf("find %s failed!\n", can_name);
return RT_ERROR;
}
rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
res = rt_device_open(can_dev, RT_DEVICE_FLAG_INT_TX | RT_DEVICE_FLAG_INT_RX);
RT_ASSERT(res == RT_EOK);
rt_device_control(can_dev, RT_CAN_CMD_SET_BAUD, (void *)CAN500kBaud);
rt_device_control(can_dev, RT_CAN_CMD_SET_MODE, (void *)RT_CAN_MODE_NORMAL);
thread = rt_thread_create("can_rx", can_rx_thread, RT_NULL, 1024, 25, 10);
if (thread != RT_NULL)
{
rt_kprintf("create can_rx thread OK!\n");
rt_thread_startup(thread);
}
else
{
rt_kprintf("create can_rx thread failed!\n");
}
rt_kprintf("start can_snd\n");
msg.id = 0x78;
msg.ide = RT_CAN_STDID;
msg.rtr = RT_CAN_DTR;
msg.len = 8;
msg.data[0] = 0x00;
msg.data[1] = 0x11;
msg.data[2] = 0x22;
msg.data[3] = 0x33;
msg.data[4] = 0x44;
msg.data[5] = 0x55;
msg.data[6] = 0x66;
msg.data[7] = 0x77;
size = rt_device_write(can_dev, 0, &msg, sizeof(msg));
if (size == 0)
{
rt_kprintf("can dev write data failed!\n");
}
return res;
}
MSH_CMD_EXPORT(can_sample, can device sample);
然后在main主函数中添加如下代码:
到些为此,我们的代码就编写完成。
下载后工程后就可以看到迪文屏上收到温、湿度数据了。
【移植项目心得】
其实CAN通讯,我在2022年就用库实现过,但这次的是RT—Thread Studio来实现的,相比写代码,要快速很多。
HS3003我也在瑞萨单片机试用活动中用库的代码实现过。这次首次使用RT-Thread Studiog来实现,花了三天的时间,是前面一直调试不过,后面把RT-Thread Studio升级到最新版,就没有问题了,前面的版本一直提示sensor.h找不到,手工添加也不行。
最后实现的效果非常好,CAN的数据收发相比串口要简单一些,接收与判断也不用写复杂的代码。
当然这里只是实现基础的功能,如果要在多发多收上面,还要加入ID的判断才行。