极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 28460|回复: 2

两套arduino UNO 和MCP2515模块进行CAN通讯的问题

[复制链接]
发表于 2019-8-20 16:17:31 | 显示全部楼层 |阅读模式
本帖最后由 影丿真红 于 2019-8-20 18:55 编辑

新人想实现两个arduino进行CAN通讯,@上网找了很多程序和接线方法,搭了两个设备,一个用于发送CAN,一个用于接收CAN,两个arduino uno与CAN模块接线如下:
INT——D2
SCK——D13
SI——D11
SO——D12
CS——D9
GND——GND
VCC——5V

两个MCP2515模块之间
H——H
L——L

上网找的库,板子都是新买的,串口监视到两个arduino都初始化成功,发送端有发送成功的Can Send Ok出现,但是接收的一直没有任何响应,只有显示初始化成功,现在没有门路了,求各位帮忙不知道是哪里的问题。

程序如下:发送:

  1. #include <mcp_can.h>
  2. #include <mcp_can_dfs.h>

  3. const int SPI_CS_PIN = 9;
  4. MCP_CAN CAN(SPI_CS_PIN);     

  5. void setup()
  6. {
  7.   Serial.begin(115200);
  8.   // init can bus, baudrate: 500k
  9.   if(CAN.begin(CAN_1000KBPS) ==CAN_OK) Serial.print("can init ok!!\r\n");
  10.   else Serial.print("Can init fail!!\r\n");
  11. }

  12. unsigned char stmp[8] = {0, 1, 2, 3, 4, 5, 6, 7};
  13. void loop()
  14. {
  15.   // send data:  id = 0x00, standrad flame, data len = 8, stmp: data buf
  16.   CAN.sendMsgBuf(0x00, 0, 8, stmp);  
  17.   Serial.print("Can Send Ok\r\n");
  18.   delay(1000);
  19. }
复制代码


接收:

  1. #include <mcp_can.h>
  2. #include <mcp_can_dfs.h>

  3. // the cs pin of the version after v1.1 is default to D9
  4. // v0.9b and v1.0 is default D10
  5. const int SPI_CS_PIN = 9;
  6. MCP_CAN CAN(SPI_CS_PIN);                                    // Set CS pin

  7. unsigned char flagRecv = 0;
  8. unsigned char len = 0;
  9. unsigned char buf[8];
  10. char str[20];

  11. void setup()
  12. {
  13.     Serial.begin(115200);

  14.     while (CAN_OK != CAN.begin(CAN_1000KBPS))              // init can bus : baudrate = 500k
  15.     {
  16.         Serial.println("CAN BUS Shield init fail");
  17.         Serial.println(" Init CAN BUS Shield again");
  18.         delay(100);
  19.     }
  20.     Serial.println("CAN BUS Shield init ok!");

  21.     attachInterrupt(0, MCP2515_ISR, FALLING); // start interrupt
  22. }

  23. void MCP2515_ISR()
  24. {
  25.     flagRecv = 1;
  26. }

  27. void loop()
  28. {
  29.     if(flagRecv)
  30.     {                                   // check if get data

  31.         flagRecv = 0;                   // clear flag

  32.             // read data,  len: data length, buf: data buf
  33.             CAN.readMsgBuf(&len, buf);

  34.             // print the data
  35.             for(int i = 0; i<len; i++)
  36.             {
  37.                 Serial.print(buf);Serial.print("\t");
  38.             }
  39.             Serial.println();

  40.     }
  41. }
复制代码


实物图:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2020-11-29 14:07:38 | 显示全部楼层
发送和接受后来研究成功了吗?大兄弟
成功了可以上来分享分享
回复 支持 反对

使用道具 举报

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

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-29 14:07 , Processed in 0.046810 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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