在线时间795 小时
UID3067179
注册时间2014-10-23
NXP金币1047
TA的每日心情 | 开心 2024-4-10 10:22 |
---|
签到天数: 1317 天 [LV.10]以坛为家III
版主
- 积分
- 7570
- 最后登录
- 2024-4-10
|
本帖最后由 TLLED 于 2023-2-16 14:18 编辑
这篇来测试下,使用模式方式驱动CAN收发芯片,实现CAN收发功能。
一、硬件部分
1.1、电路图部分
接口部分使用上次设计的arduino转接板。部分电路图如下:
1.2、转接板实物图
1.3、整体连接图
二、软件部分
软件中使用了两个线程,一个邮箱。
线程1,发送CAN数据
线程2,接收的CAN数据通过邮箱传递给线程2
can.c
- #include <rtthread.h>
- #include <rtdevice.h>
- #include "mcp2515.h"
- #define THREAD1_PRIORITY 25
- #define THREAD1_STACK_SIZE 512
- #define THREAD1_TIMESLICE 5
- #define THREAD2_PRIORITY 24
- #define THREAD2_STACK_SIZE 512
- #define THREAD2_TIMESLICE 5
- struct rt_mailbox mb;
- uint8_t mb_pool[128];
- uint8_t can_tx_buf[8];
- //thread1
- static void thread1_entry(void *parameter)
- {
- rt_uint32_t count = 0;
-
- Init_MCP2515();
- while (1)
- {
- if(count>250)
- {
- count=0;
- }
- count++;
- can_tx_buf[0]=count;
- can_tx_buf[1]=count+1;
- can_tx_buf[2]=count+2;
- can_tx_buf[3]=count+3;
- CAN_Send(can_tx_buf);
- rt_kprintf("can tx data: %02x %02x %02x %02x %02x %02x %02x %02x\n", can_tx_buf[0],can_tx_buf[1],can_tx_buf[2],can_tx_buf[3],can_tx_buf[4],can_tx_buf[5],can_tx_buf[6],can_tx_buf[7]);
-
- rt_thread_mdelay(500);
- }
- }
- //thread2
- static void thread2_entry(void *param)
- {
- rt_uint32_t count = 0;
- uint8_t *str;
- while(1)
- {
- if (rt_mb_recv(&mb, (rt_uint32_t *)&str, RT_WAITING_FOREVER) == RT_EOK)
- {
- rt_kprintf("can rx data: %02x %02x %02x %02x %02x %02x %02x %02x\n", str[0],str[1],str[2],str[3],str[4],str[5],str[6],str[7]);
- rt_thread_mdelay(5);
- }
- }
- }
- int thread_can(void)
- {
- rt_err_t result;
- rt_thread_t tid1 = RT_NULL;
- rt_thread_t tid2 = RT_NULL;
-
- //mailbox
- result = rt_mb_init(&mb,
- "mbt",
- &mb_pool[0],
- sizeof(mb_pool) / 4,
- RT_IPC_FLAG_FIFO);
- if (result != RT_EOK)
- {
- rt_kprintf("init mailbox failed.\n");
- return -1;
- }
- //thread
- tid1 = rt_thread_create("thread1",
- thread1_entry,
- RT_NULL,
- THREAD1_STACK_SIZE,
- THREAD1_PRIORITY,
- THREAD1_TIMESLICE);
- if (tid1 != RT_NULL)
- rt_thread_startup(tid1);
- tid2 = rt_thread_create("thread2",
- thread2_entry,
- RT_NULL,
- THREAD2_STACK_SIZE,
- THREAD2_PRIORITY,
- THREAD2_TIMESLICE);
- if (tid2 != RT_NULL)
- rt_thread_startup(tid2);
- return 0;
- }
- INIT_APP_EXPORT(thread_can);
复制代码
三、运行结果
CAN通信收发的数据帧
|
|