0
\$\begingroup\$

https://github.com/coryjfowler/MCP_CAN_lib/blob/master/examples/CAN_loopback/CAN_loopback.ino I'm using this library to implement CAN bus and I am using the loopback mode example using a single arduino connected to MCP2525 CAN transeiver. But I'm getting the error that MCP2515 is not initialized yet the message is being transmitted. How can I ressolve this error? enter image description here

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

// CAN TX Variables
unsigned long prevTX = 0;                                        // Variable to store last execution time
const unsigned int invlTX = 1000;                                // One second interval constant
byte data[] = {0xAA, 0x55, 0x01, 0x10, 0xFF, 0x12, 0x34, 0x56};  // Generic CAN data to send

// CAN RX Variables
long unsigned int rxId;
unsigned char len;
unsigned char rxBuf[8];

// Serial Output String Buffer
char msgString[128];

// CAN0 INT and CS
#define CAN0_INT 2                              // Set INT to pin 2
MCP_CAN CAN0(10);                               // Set CS to pin 10


void setup()
{
  Serial.begin(115200);  // CAN is running at 500,000BPS; 115,200BPS is SLOW, not FAST, thus 9600 is crippling.
  
  // 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...");
  
  // Since we do not set NORMAL mode, we are in loopback mode by default.
  //CAN0.setMode(MCP_NORMAL);

  pinMode(CAN0_INT, INPUT);                           // Configuring pin for /INT input
  
  Serial.println("MCP2515 Library Loopback 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();
  }
  
  if(millis() - prevTX >= invlTX){                    // Send this at a one second interval. 
    prevTX = millis();
    byte sndStat = CAN0.sendMsgBuf(0x100, 8, data);
    
    if(sndStat == CAN_OK)
      Serial.println("Message Sent Successfully!");
    else
      Serial.println("Error Sending Message...");

  }
}
\$\endgroup\$
0
\$\begingroup\$

I would suggest you use two units and use the send and receive examples included with the library. The library you are using works great and does it well. Mine came up on the first try. Just a bit of information, CAN messages must be acknowledged or you will get an error. A common misconception is that a CAN module can talk to itself, technically it has to to monitor the bus but the message is not saved. Until users get there first system or two up they forget that you need a minimum of two or more nodes (properly terminated) to establish a network, otherwise the CAN Bus controller will simply return error messages. Yes you may see data on the bus but without an acknowledgement it will not be complete. The MPC2515 in the loopback mode ignores the ACK and transfers the data internally from the send to receive buffers but it does not actually send the message on the bus. In summary, no messages will be transmitted on the bus while in the loopback mode (including error flags or Acknowledge signals). The TXCAN pin will be in a recessive state."

Your code shows a 16Mhz crystal, if it is a China clone it probably has a 8Mhz crystal. Also I would suggest dropping your baud rate to a lower value (CAN_250KBPS) until you get it working. It is important that CAN-H and CAN-L are connected properly, if not it will not work.

\$\endgroup\$
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.