本帖最后由 影丿真红 于 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出现,但是接收的一直没有任何响应,只有显示初始化成功,现在没有门路了,求各位帮忙不知道是哪里的问题。
程序如下:发送:
- #include <mcp_can.h>
- #include <mcp_can_dfs.h>
-
- const int SPI_CS_PIN = 9;
- MCP_CAN CAN(SPI_CS_PIN);
-
- void setup()
- {
- Serial.begin(115200);
- // init can bus, baudrate: 500k
- if(CAN.begin(CAN_1000KBPS) ==CAN_OK) Serial.print("can init ok!!\r\n");
- else Serial.print("Can init fail!!\r\n");
- }
-
- unsigned char stmp[8] = {0, 1, 2, 3, 4, 5, 6, 7};
- void loop()
- {
- // send data: id = 0x00, standrad flame, data len = 8, stmp: data buf
- CAN.sendMsgBuf(0x00, 0, 8, stmp);
- Serial.print("Can Send Ok\r\n");
- delay(1000);
- }
复制代码
接收:
- #include <mcp_can.h>
- #include <mcp_can_dfs.h>
-
- // the cs pin of the version after v1.1 is default to D9
- // v0.9b and v1.0 is default D10
- const int SPI_CS_PIN = 9;
- MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
-
- unsigned char flagRecv = 0;
- unsigned char len = 0;
- unsigned char buf[8];
- char str[20];
-
- void setup()
- {
- Serial.begin(115200);
-
- while (CAN_OK != CAN.begin(CAN_1000KBPS)) // init can bus : baudrate = 500k
- {
- Serial.println("CAN BUS Shield init fail");
- Serial.println(" Init CAN BUS Shield again");
- delay(100);
- }
- Serial.println("CAN BUS Shield init ok!");
-
- attachInterrupt(0, MCP2515_ISR, FALLING); // start interrupt
- }
-
- void MCP2515_ISR()
- {
- flagRecv = 1;
- }
-
- void loop()
- {
- if(flagRecv)
- { // check if get data
-
- flagRecv = 0; // clear flag
-
- // read data, len: data length, buf: data buf
- CAN.readMsgBuf(&len, buf);
-
- // print the data
- for(int i = 0; i<len; i++)
- {
- Serial.print(buf);Serial.print("\t");
- }
- Serial.println();
-
- }
- }
复制代码
实物图:
|