MCP2515 Interfaced with Arduino Mega using

I am currently using the mcp_2515 library provided by cory fowler as posted. GitHub - coryjfowler/MCP_CAN_lib: MCP_CAN Library I am running into issues sending an extended CANBUS message. I have a CANBUS shield with a 16MHz Crystal with a MCP2515 and TJA1050. I have attached how I have the MCP2515 connected to the Mega as well as my code. Any help on how to get this working would be helpful!

Serial.println(sndStat,DEC); is returning a Timeout message

`// CAN Send Example
//

#include <mcp_can.h>
#include <SPI.h>

 MCP_CAN CAN0(53);     // Set CS to pin 10

 void setup()
 {
  Serial.begin(115200);

 // Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters 
  disabled.
 while(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) != CAN_OK) 
Serial.println("MCP2515 Initializing");

Serial.println("Initialized MCP2515");

 CAN0.setMode(MCP_NORMAL);   // Change to normal mode to allow messages to be 
  transmitted
}

byte data[8] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};

void loop()
{
   // send data:  ID = 0x100, Standard CAN Frame, Data length = 8 bytes, 'data' = array of data 
   bytes to send
   byte sndStat = CAN0.sendMsgBuf(0xFFA1, 1, 8, data);
    if(sndStat == CAN_OK){
   Serial.println("Message Sent Successfully!");
    } else {
    Serial.println(sndStat,DEC);
   }
  delay(3000);   // send data per 100ms

 }







  /******************
 END FILE

 *******/`

What 'issues' are you haing?

It is initializing fine. When it enters the main loop, it fails sending the message with an extended ID of "FFA1" with a code of 7 which is #define CAN_SENDMSGTIMEOUT (7). No idea why the message wont send

Are you testing in isolation or is it connected to a second MCP2515 or a CAN bus with active devices?
To test a sender you also need a receiver otherwise ACKS don't get set.

Ah it was in isolation. Will set up a receive device and get back

Wired up a second unit connected to an UNO and am running a receive example and still getting the error code...

// CAN Receive Example
//

#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 2                              // Set INT to pin 2
MCP_CAN CAN0(10);                               // Set CS to pin 10


void setup()
{
  Serial.begin(115200);
  
  // Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
  if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK)
    Serial.println("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);                            // Configuring pin for /INT input
  
  Serial.println("MCP2515 Library Receive Example...");
}

void loop()
{
  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.println();
  }
}

/*********************************************************************************************************
  END FILE
*********************************************************************************************************/

Post a photo of the MCP2515 board.
Try to get everything in focus so you can see the writing on the crystal.

It is a 16Mhz crystal I am using fyi.

The outline of the board you posted looks like a Niren board and they ship with a 8MHz crystal. Have you made your own version of a Niren board with a 16MHz crystal?

Yes I soldered one on. I have the CAN communication working now. Did not jumper the terminator resistor. Onto the next step

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.