the MCP2515 has a 8MHz crystal - note I have two MCP2515 modules one works OK with 3.3V the other requires 5V
RPi pico code
EDIT: updated code, results and photo
// RP2040 RPi Pico CAN Receive Example - note added INPUT_PULLUP to CAN0_INT
//
// RP2040 RPi PICO connections
// MCP2515 INT to RP2040 GP20
// MCP2515 SCK to RP2040 GP18 SPI0_SCK
// MCP2515 SI to RP2040 GP19 SPI0_TX
// MCP2515 SO to RP2040 GP16 SPI0_RX
// MCP2515 CS to RP2040 GP17 SPI0_CSn
// MCP2515 GND to RP2040 GND
// MCP2515 VCC to RP2040 GP 3.3V
#include <mcp_can.h>
#include <SPI.h>
long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
char msgString[128]; // Array to store serial string
#define CAN0_INT 20 // for RP2040
MCP_CAN CAN0(17); //
void setup() {
Serial.begin(115200);
delay(2000);
Serial.println("\n\nRP2040 CAN MCP2515 shield Send/Receive test - MCP2515 Initialize");
// Initialize MCP2515 baudrate of 250kb/s and the masks and filters disabled.
// check crystal frequency!! e.g. Canbus shield is 16MHz MCP2515 is 8MHz
if (CAN0.begin(MCP_ANY, CAN_250KBPS, MCP_8MHZ) == CAN_OK)
Serial.println("CAN Receive - MCP2515 Initialized Successfully!");
else
Serial.println("Error Initializing MCP2515...");
CAN0.setMode(MCP_NORMAL); // Set operation mode to normal so the MCP2515 sends acks to received data.
pinMode(CAN0_INT, INPUT_PULLUP); // Configuring pin for /INT input *** added PULLUP ***
Serial.println("MCP2515 Library CAN Send/Receive Example\n enter space to send a frame");
}
void loop() {
// check for data received
if (!digitalRead(CAN0_INT)) // If CAN0_INT pin is low, read receive buffer
{
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
if ((rxId & 0x80000000) == 0x80000000) // Determine if ID is standard (11 bits) or extended (29 bits)
sprintf(msgString, "Extended ID: 0x%.8lX DLC: %1d Data:", (rxId & 0x1FFFFFFF), len);
else
sprintf(msgString, "Standard ID: 0x%.3lX DLC: %1d Data: ", rxId, len);
Serial.print(msgString);
if ((rxId & 0x40000000) == 0x40000000) { // Determine if message is a remote request frame.
sprintf(msgString, " REMOTE REQUEST FRAME");
Serial.print(msgString);
} else {
for (byte i = 0; i < len; i++) {
sprintf(msgString, "0x%.2X ", rxBuf[i]);
Serial.print(msgString);
}
Serial.print((char *)&rxBuf + 1);
}
Serial.println();
}
// transmit data when space entered on keyboard
if (Serial.available()) {
if (Serial.read() != ' ') return;
static byte data[8] = { 0x00, 'R', 'P', 'p', 'i', 'c', 'o', 0};
for (byte i = 0; i < 8; i++) {
sprintf(msgString, " 0x%.2X", data[i]);
Serial.print(msgString);
}
// send data: ID = 0x100, Standard CAN Frame, Data length = 8 bytes, 'data' = array of data bytes to send
byte sndStat = CAN0.sendMsgBuf(0x100, 0, 8, data);
if (sndStat == CAN_OK) {
Serial.println(" Message Sent Successfully!");
} else {
Serial.println(" Error Sending Message...");
}
data[0]++; // increment first byte of data
}
}
serial monitor output
RP2040 CAN MCP2515 shield Send/Receive test - MCP2515 Initialize
Entering Configuration Mode Successful!
Setting Baudrate Successful!
CAN Receive - MCP2515 Initialized Successfully!
MCP2515 Library CAN Send/Receive Example
enter space to send a frame
Standard ID: 0x100 DLC: 8 Data: 0x00 0x4D 0x45 0x47 0x41 0x00 0x07 0x08 MEGA
Standard ID: 0x100 DLC: 8 Data: 0x01 0x4D 0x45 0x47 0x41 0x00 0x07 0x08 MEGA
0x00 0x52 0x50 0x70 0x69 0x63 0x6F 0x00 Message Sent Successfully!
0x01 0x52 0x50 0x70 0x69 0x63 0x6F 0x00 Message Sent Successfully!
Standard ID: 0x7DF DLC: 8 Data: 0x02 0x54 0x57 0x41 0x49 0x00 0xAA 0xAA TWAI
Standard ID: 0x7DF DLC: 8 Data: 0x03 0x54 0x57 0x41 0x49 0x00 0xAA 0xAA TWAI
Standard ID: 0x077 DLC: 8 Data: 0x11 0x50 0x49 0x43 0x32 0x34 0x00 0x00 PIC24
Standard ID: 0x077 DLC: 8 Data: 0x11 0x50 0x49 0x43 0x32 0x34 0x00 0x00 PIC24
Extended ID: 0x00000077 DLC: 8 Data:0x11 0x50 0x49 0x43 0x32 0x34 0x00 0x00 PIC24
Extended ID: 0x00000077 DLC: 8 Data:0x11 0x50 0x49 0x43 0x32 0x34 0x00 0x00 PIC24
Extended ID: 0x00000077 DLC: 0 Data: REMOTE REQUEST FRAME
Standard ID: 0x40000077 DLC: 0 Data: REMOTE REQUEST FRAME
Standard ID: 0x100 DLC: 8 Data: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
Standard ID: 0x100 DLC: 8 Data: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
photo





