I have a custom PCB, that uses a ATMEGA2560 with an MCP2515 and a MCP2551 in order to recieve CAN. Board is able to read CAN using the following code:
#include <SPI.h>
#include <mcp2515.h>
// LEDs debug
#define LED_DEBUG0 49
#define LED_DEBUG1 48
#define LED_DEBUG2 47
#define LED_DEBUG3 46
#define LED_DEBUG4 45
#define LED_DEBUG5 44
#define LED_DEBUG6 43
#define LED_DEBUG7 42
struct can_frame canMsg_recieved;
struct can_frame canMsg_send1;
struct can_frame canMsg_send2;
MCP2515 mcp2515(53);
unsigned long last_time_send_1_2 = 0;
void send_canMsg_1_2(void) {
digitalWrite(LED_DEBUG4, true);
mcp2515.sendMessage(&canMsg_send1);
mcp2515.sendMessage(&canMsg_send2);
Serial.println("B");
digitalWrite(LED_DEBUG4, false);
}
void setup() {
// ########################################################################################
// I/0
// ########################################################################################
// LEDs debug
pinMode(LED_DEBUG0, OUTPUT);
pinMode(LED_DEBUG1, OUTPUT);
pinMode(LED_DEBUG2, OUTPUT);
pinMode(LED_DEBUG3, OUTPUT);
pinMode(LED_DEBUG4, OUTPUT);
pinMode(LED_DEBUG5, OUTPUT);
pinMode(LED_DEBUG6, OUTPUT);
pinMode(LED_DEBUG7, OUTPUT);
// ########################################################################################
// CANBUS
// ########################################################################################
mcp2515.reset();
mcp2515.setBitrate(CAN_500KBPS);
mcp2515.setNormalMode();
canMsg_send1.can_id = 750;
canMsg_send1.can_dlc = 8;
canMsg_send2.can_id = 751;
canMsg_send2.can_dlc = 8;
canMsg_send1.data[0] = 0xFF;
canMsg_send1.data[1] = 0xFF;
canMsg_send1.data[2] = 0xFF;
canMsg_send1.data[3] = 0xFF;
canMsg_send1.data[4] = 0xFF;
canMsg_send1.data[5] = 0xFF;
canMsg_send1.data[6] = 0xFF;
canMsg_send1.data[7] = 0xFF;
canMsg_send2.data[0] = 0xFF;
canMsg_send2.data[1] = 0xFF;
canMsg_send2.data[2] = 0xFF;
canMsg_send2.data[3] = 0xFF;
canMsg_send2.data[4] = 0xFF;
canMsg_send2.data[5] = 0xFF;
canMsg_send2.data[6] = 0xFF;
canMsg_send2.data[7] = 0xFF;
Serial.begin(115200);
}
void loop() {
if (mcp2515.readMessage(&canMsg_recieved) == MCP2515::ERROR_OK) {
digitalWrite(LED_DEBUG2, true);
Serial.print("A ID: ");
Serial.println(canMsg_recieved.can_id);
digitalWrite(LED_DEBUG2, false);
}
else if (millis() - last_time_send_1_2 > 1000) {
last_time_send_1_2 = millis();
//send_canMsg_1_2();
Serial.println("C");
}
}
Notice that I am not sending nothing, since send_canMsg_1_2() is commented in the last lines. With this script, I get the following output:
12:36:51.357 -> C
12:36:51.779 -> A ID: 1000
12:36:51.779 -> A ID: 1001
12:36:51.779 -> A ID: 1003
12:36:51.779 -> A ID: 1002
12:36:52.377 -> C
12:36:52.747 -> A ID: 1000
12:36:52.747 -> A ID: 1001
12:36:52.747 -> A ID: 1003
12:36:52.747 -> A ID: 1002
12:36:53.344 -> C
12:36:53.765 -> A ID: 1000
12:36:53.765 -> A ID: 1001
12:36:53.765 -> A ID: 1003
12:36:53.765 -> A ID: 1002
Seems to work propperly (Is printing "C" at 1Hz (every 1 second) and is recieving all the messages from my ECU (4 different frames at 1Hz, 1s)). But if I try to send any package (uncommenting the last send_canMsg_1_2() line), I get the following:
12:38:56.997 -> B
12:38:56.997 -> C
12:38:57.781 -> A ID: 1001
12:38:57.781 -> A ID: 1003
12:38:58.012 -> B
12:38:58.012 -> C
12:38:59.010 -> B
12:38:59.010 -> C
12:39:00.028 -> B
12:39:00.028 -> C
12:39:00.770 -> A ID: 1001
12:39:00.770 -> A ID: 1003
Am I missing something? I guess HW must be ok, since I am recieving correct data from the ECU... The only thing that comes to my mind is that they overlay messages, but I guess MCP2515 has an integrated CSMA protocol.
BTW: I am using the following library: https://github.com/autowp/arduino-mcp2515?tab=readme-ov-file#software-usage
Thanks!