查看: 994|回复: 4

[原创] 【LPC55S69】基于rt-thread模拟接口CAN通信测试

[复制链接]
  • TA的每日心情
    开心
    2024-4-10 10:22
  • 签到天数: 1317 天

    [LV.10]以坛为家III

    124

    主题

    2831

    帖子

    31

    版主

    Rank: 7Rank: 7Rank: 7

    积分
    7570
    最后登录
    2024-4-10
    发表于 2023-2-16 11:19:55 | 显示全部楼层 |阅读模式
    本帖最后由 TLLED 于 2023-2-16 14:18 编辑

        这篇来测试下,使用模式方式驱动CAN收发芯片,实现CAN收发功能。
       
        一、硬件部分

        1.1、电路图部分
        接口部分使用上次设计的arduino转接板。部分电路图如下:
        20230216102614.png


        1.2、转接板实物图
        20230216102924.jpg
        20230216102912.jpg


        1.3、整体连接图
        20230216104151.jpg

       二、软件部分
       
        软件中使用了两个线程,一个邮箱。
        线程1,发送CAN数据
        线程2,接收的CAN数据通过邮箱传递给线程2
        can.c
    1. #include <rtthread.h>
    2. #include <rtdevice.h>
    3. #include "mcp2515.h"

    4. #define THREAD1_PRIORITY         25
    5. #define THREAD1_STACK_SIZE       512
    6. #define THREAD1_TIMESLICE        5

    7. #define THREAD2_PRIORITY         24
    8. #define THREAD2_STACK_SIZE       512
    9. #define THREAD2_TIMESLICE        5

    10. struct rt_mailbox mb;
    11. uint8_t mb_pool[128];
    12. uint8_t can_tx_buf[8];

    13. //thread1
    14. static void thread1_entry(void *parameter)
    15. {
    16.         rt_uint32_t count = 0;
    17.         
    18.         Init_MCP2515();
    19.         while (1)
    20.         {
    21.                 if(count>250)
    22.                 {
    23.                         count=0;
    24.                 }
    25.                 count++;
    26.                 can_tx_buf[0]=count;
    27.                 can_tx_buf[1]=count+1;
    28.                 can_tx_buf[2]=count+2;
    29.                 can_tx_buf[3]=count+3;
    30.                 CAN_Send(can_tx_buf);
    31.                 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]);
    32.                
    33.                 rt_thread_mdelay(500);
    34.         }
    35. }

    36. //thread2
    37. static void thread2_entry(void *param)
    38. {
    39.         rt_uint32_t count = 0;
    40.         uint8_t *str;
    41.         while(1)
    42.         {
    43.                 if (rt_mb_recv(&mb, (rt_uint32_t *)&str, RT_WAITING_FOREVER) == RT_EOK)
    44.                 {
    45.                                 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]);
    46.                                 rt_thread_mdelay(5);
    47.                 }
    48.         }  
    49. }

    50. int thread_can(void)
    51. {
    52.         rt_err_t result;
    53.         rt_thread_t tid1 = RT_NULL;
    54.         rt_thread_t tid2 = RT_NULL;
    55.         
    56.         //mailbox
    57.         result = rt_mb_init(&mb,
    58.                                                                                         "mbt",                       
    59.                                                                                         &mb_pool[0],                 
    60.                                                                                         sizeof(mb_pool) / 4,         
    61.                                                                                         RT_IPC_FLAG_FIFO);           
    62.         if (result != RT_EOK)
    63.         {
    64.                         rt_kprintf("init mailbox failed.\n");
    65.                         return -1;
    66.         }
    67.         //thread
    68.         tid1 = rt_thread_create("thread1",
    69.                                                                                                         thread1_entry,
    70.                                                                                                         RT_NULL,
    71.                                                                                                         THREAD1_STACK_SIZE,
    72.                                                                                                         THREAD1_PRIORITY,
    73.                                                                                                         THREAD1_TIMESLICE);
    74.         if (tid1 != RT_NULL)
    75.         rt_thread_startup(tid1);

    76.         tid2 = rt_thread_create("thread2",
    77.                                                                                                         thread2_entry,
    78.                                                                                                         RT_NULL,
    79.                                                                                                         THREAD2_STACK_SIZE,
    80.                                                                                                         THREAD2_PRIORITY,
    81.                                                                                                         THREAD2_TIMESLICE);

    82.         if (tid2 != RT_NULL)
    83.         rt_thread_startup(tid2);
    84.         return 0;
    85. }

    86. INIT_APP_EXPORT(thread_can);

    复制代码

       三、运行结果

       CAN通信收发的数据帧
        20230216102201.png
       

    哎...今天够累的,签到来了~
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2024-3-26 15:16
  • 签到天数: 266 天

    [LV.8]以坛为家I

    3298

    主题

    6545

    帖子

    0

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    32003
    最后登录
    2024-4-9
    发表于 2023-2-16 12:32:32 | 显示全部楼层
    板子这么快回来了,支持支持
    签到签到
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    慵懒
    2024-4-9 17:01
  • 签到天数: 1478 天

    [LV.10]以坛为家III

    203

    主题

    2万

    帖子

    64

    超级版主

    Rank: 8Rank: 8

    积分
    92609
    最后登录
    2024-4-9
    发表于 2023-2-16 12:49:07 | 显示全部楼层
    XL2515是不是和MCP2515有啥关系?
    该会员没有填写今日想说内容.
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2024-4-10 10:22
  • 签到天数: 1317 天

    [LV.10]以坛为家III

    124

    主题

    2831

    帖子

    31

    版主

    Rank: 7Rank: 7Rank: 7

    积分
    7570
    最后登录
    2024-4-10
     楼主| 发表于 2023-2-16 13:55:06 | 显示全部楼层
    stm1024 发表于 2023-2-16 12:49
    XL2515是不是和MCP2515有啥关系?

    一个是国产,一个是进口器件,直接替换
    哎...今天够累的,签到来了~
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2024-4-10 22:38
  • 签到天数: 1335 天

    [LV.10]以坛为家III

    88

    主题

    4292

    帖子

    12

    版主

    Rank: 7Rank: 7Rank: 7

    积分
    9049
    最后登录
    2024-4-13
    发表于 2023-2-16 14:04:49 | 显示全部楼层
    厉害了!
    楼主的效率太高了
    该会员没有填写今日想说内容.
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /4 下一条

    Archiver|手机版|小黑屋|恩智浦技术社区

    GMT+8, 2024-4-20 10:57 , Processed in 0.120508 second(s), 24 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表