CAN Library - mcp_can [or] MCP2515 ?

I have a requirement to read an Angle information from a CAN bus.

I am trying to use the mcp_can library but its not very reliable. It mostly fails at the Init stage. ( Hardware is a Nano + MCP2515 module )

I also came across another library MCP2515 which seems to be simple but yet to try out. Just wanted to know which is better for a simple 11 bit CAN _ID Read application ?

Thanks

What is the baud rate and how many packets per second do you need to receive?
Is the angle information the only data on the bus, or are there packets for other data as well?

I ask because the Nano is a slow device and therefore you may be running into hardware limitations rather than library issues.

I use the mcp_can library and have never had any problems with it. If the hardware is not correct it will give you errors. Try the examples that came with the library first to check it.

Sorry about my vague question.

I just need to read a 2 byte angle information once every 100 ms from two encoders. There are two encoders connected to the controller and it also has to control 2 regulating valves. Its a headless system - no display. Hence the need to tap into the bus and grab the angle information for some other purpose.

The operating baud is 250 kbps. And I am using AltSoft serial for displaying values on Serial Monitor. Would that be an issue ?

I agree and I also have been using it for simple demo applications.

But now my problem is it fails in in the Init stage and hence unable to get into the loop. I am trying this example :

// 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(9600);
  
  // 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();
  }
}

You said that baud rate is 250k, but your code says 500k.

Also do you really have a 16MHz crystal? Check the markings on the metal can.

Typical mismatches ... when the modules was repeatedly failing to Init I have tried both options of 500 k and also 8MHz ( marking on metal can ) ... and the surprise is both 500 kbps and 16MHz are working in another code. I know this sounds crazy .. reason why I started searching for an alternate library . ( Not that mcp_can is bad - just that its a bit advanced for me and maybe I am making mistakes )

It was a Hook wire issue !!

It took a few hours of wasted time before I found out that one of the hook up wire was open ( and that too not fully - it was connecting in one position :hot_face:)

Now mcp_can is working fine. Anyway while i was trying various things came across a site which had this Init routine while using mcp_can.h

if(CAN.begin(CAN_500KBPS) ==CAN_OK) Serial.print("WOPR 0x01. CAN init ok!!\r\n");
// init can bus, baudrate: 250k Note the command here is twice as fast as the baud you want,
// because the version of the MCP2515 board you have is using an 8MHz chip, instead of the standard
// 16MHz chip. This is the easiest fix to that problem.

And I tried this and promptly got a no supporting call for CAN.begin(const int ) . So are there different versions of mcp_can library ?? ( That site had no link to the library they were using )

1 Like