We're using an Arduino Uno with the MCP2515 module and successfully tested message exchange with another Arduino via CANbus.
However, we're having trouble when using a single Arduino with a CANbus analyzer (Microchip CANbus Analyzer) or with a different microcontroller like the PIC18F46K80 for testing message transmit and recieve via CANbus. We're using the MCP2515 CAN library by Sandeep Mistry and standard receive code (attached).
Any guidance or advice would be greatly appreciated.
// Copyright (c) Sandeep Mistry. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#include <CAN.h>
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("CAN Receiver");
// start the CAN bus at 500 kbps
if (!CAN.begin(500E3)) {
Serial.println("Starting CAN failed!");
while (1);
}
}
void loop() {
// try to parse packet
int packetSize = CAN.parsePacket();
if (packetSize) {
// received a packet
Serial.print("Received ");
if (CAN.packetExtended()) {
Serial.print("extended ");
}
if (CAN.packetRtr()) {
// Remote transmission request, packet contains no data
Serial.print("RTR ");
}
Serial.print("packet with id 0x");
Serial.print(CAN.packetId(), HEX);
if (CAN.packetRtr()) {
Serial.print(" and requested length ");
Serial.println(CAN.packetDlc());
} else {
Serial.print(" and length ");
Serial.println(packetSize);
// only print packet data for non-RTR packets
while (CAN.available()) {
Serial.print((char)CAN.read());
}
Serial.println();
}
Serial.println();
}
}
I do not know if that analyzer will acknowledge a message but connect the two that worked. When working connect the analyzer. Use only 2 termination resistors one at each physical end of the bus.
We have used two termination resistors, one was setup externally and the other one is built internally with the module that we used. Do you have any ideas why we might have this issue, I am starting to think that Arduino CAN communication does not work with non-arduino controllers/peripherals.
We use an external CAN module (see the picture below). The number on the crystal is "8000". If you are interested, the number on the arduino crystal we used is "16000". Would the mismatch be the reason of failure?
That indicates a 8 MHz crystal, however the library assumes that you are using a 16 MHz crystal.
When using an 8 MHz crystal the documentation says you need to add
I have had different crystals on my CAN modules. I use Gary Fowler's mcp-can library and one of the first lines in setup is the crystal frequency. I also played around and would set it for 2x baud and it worked fine. If you cannot set the crystal frequency try 2x baud.
using mcp-can I set the CANbus baudrate and the crystal frequency
e.g. setting 250KBS on a Adafruit Feather RP2040 CAN (onboard MCP2515) which has a 16MHz crystal
That needs to be 8MHZ. CAN_250KBPS Needs to be the baud you want. You should get "
MCP2515 Initialized Successfully! when initializing, with or without a bus connected.