CAN controller MCP2515 not working with Arduino Mega

Hi All,
I was trying a small project with the CAN MCP2515 and two Arduino UNO, this worked very well and went without a glitch as I only used the <SPI.h> & <mcp2515.h> headers and just read and write examples of “arduino-mcp2515-master” library here

here is the sample code used while reading

#include <SPI.h>
#include <mcp2515.h>

struct can_frame canMsg;
MCP2515 mcp2515(10);
  

void setup() {
  Serial.begin(115200);
  SPI.begin();
  
  mcp2515.reset();
  mcp2515.setBitrate(CAN_1000KBPS);
  mcp2515.setNormalMode();
  
  Serial.println("------- CAN Read ----------");
  Serial.println("ID  DLC   DATA");
}

void loop() {
  
  if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) {
      
    Serial.print(canMsg.can_id, HEX); // print ID
    Serial.print(" "); 
    Serial.print(canMsg.can_dlc, HEX); // print DLC
    Serial.print(" ");
    
    for (int i = 0; i<canMsg.can_dlc; i++)  {  // print the data
        
      Serial.print(canMsg.data[i],HEX);
      Serial.print(" ");

    }

    Serial.println();      
  }

}

Now i am facing a problem when i want to Add a TFT-LCD display to read from can bus and display. For this I am using a LCD Shield like here

I am trying to use the same code with Arduino Mega because i assumed MEGA has 2 SPI pins so I can power both MCP2515 and the Display at the same time. For this i am using the pins as SO–> 50, SI–> 51, SCK–> 52 and CS–> 53 for the MCP2515 as only these are free after the Lcd shield is placed.
I am only changing one line of the above code MCP2515 mcp2515(10); to MCP2515 mcp2515(53);

Can anyone please tell me what I am doing wrong? even with SEND code the MEGA and MCP2515 doesn’t seem to talk at all even without any display connected and running just the example code.

confused_man:
I am trying to use the same code with Arduino Mega because i assumed MEGA has 2 SPI pins ...

the MEGA has only ONE SPI (Arduino - SPI).

also you never mentioned how you connected (what pins u used) for your LCD display.

Please share your code as well as your wiring with the MEGA if you want us to help you better.

Oh okay thank you. So does this mean I cannot use both my LCD display and MCP2515 at the same time? U cannot do 2 SPI communications at a time or on a single board?
If possible can u please help me how?
I have attached the LCD Display Shield pins and they occupy the following pins on the Arduino Mega
Analog Pins A0 to A5 and the entire PWM row from Pin 0 to 13 and including the GND and 1 more pin after it.
Thank You.

(I am reattaching the links as in the previous post they weren’t hyperlinked.) Library GitHub - autowp/arduino-mcp2515: Arduino MCP2515 CAN interface library
LCD Shield https://www.banggood.com/3_5-Inch-TFT-Color-Screen-Module-320-X-480-Support-Arduino-UNO-Mega2560-p-1022298.html?p=DQ30066511122014069J&utm_campaign=educ8stv&utm_content=3216&cur_warehouse=CN

I don't think you understood my previos post....

what you provided is absolutely USELESS.

I DO NOT KNOW HOW YOU HAVE WIRED YOUR CIRCUIT!

I CANNOT SEE YOUR CODE WITH THE MCP2515 AND LCD SHEILD

How do you expect us to help!

confused_man:
Oh okay thank you. So does this mean I cannot use both my LCD display and MCP2515 at the same time? U cannot do 2 SPI communications at a time or on a single board?

you can use the same SPI connections (MOSI, MISO, CLK) for both the LCD and MCP2515. Only the CS pins from the LCD and MCP2515 would be connected to DIFFERENT pins to the mega.

I am Sorry. Actually the lcd is like part 2, even before that I am struck. I haven’t connected the LCD shield, only MCP2515 to MEGA. I am only trying to get the Mega and MCP2515 to work together. I am unable to transmit any data via the MCP2515 and I know it is transmit problem because my CRO is also unable to pick any signals on the CAN bus. I am attaching a drawing of my connection between MCP2515 and MEGA and also the Library file. I am pasting the Transmit code I am using on MEGA. and the Receive code i am using on UNO. The UNO and the MCP2515 work together without a hitch for both transmit and receive via CAN bus.
Thank you.

Transmit code on MEGA

#include <SPI.h>
#include <mcp2515.h>

struct can_frame canMsg1;
struct can_frame canMsg2;
MCP2515 mcp2515(53);


void setup() {

  canMsg1.can_id  = 0x0F6;
  canMsg1.can_dlc = 8;
  canMsg1.data[0] = 0x8E;
  canMsg1.data[1] = 0x87;
  canMsg1.data[2] = 0x32;
  canMsg1.data[3] = 0xFA;
  canMsg1.data[4] = 0x26;
  canMsg1.data[5] = 0x8E;
  canMsg1.data[6] = 0xBE;
  canMsg1.data[7] = 0x86;

  canMsg2.can_id  = 0x036;
  canMsg2.can_dlc = 8;
  canMsg2.data[0] = 0x0E;
  canMsg2.data[1] = 0x00;
  canMsg2.data[2] = 0x00;
  canMsg2.data[3] = 0x08;
  canMsg2.data[4] = 0x01;
  canMsg2.data[5] = 0x00;
  canMsg2.data[6] = 0x00;
  canMsg2.data[7] = 0xA0;
  
  while (!Serial);
  Serial.begin(115200);
  SPI.begin();
  
  mcp2515.reset();
  mcp2515.setBitrate(CAN_500KBPS);
  mcp2515.setNormalMode();
  
  Serial.println("Example: Write to CAN");
}

void loop() {
  
  mcp2515.sendMessage(&canMsg1);
  mcp2515.sendMessage(&canMsg2);

  Serial.println("Messages sent");
  
  delay(100);

}

Receive Code on UNO

#include <SPI.h>
#include <mcp2515.h>

struct can_frame canMsg;
MCP2515 mcp2515(10);
  

void setup() {
  Serial.begin(115200);
  SPI.begin();
  
  mcp2515.reset();
  mcp2515.setBitrate(CAN_500KBPS);
  mcp2515.setNormalMode();
  
  Serial.println("------- CAN Read ----------");
  Serial.println("ID  DLC   DATA");
}

void loop() {
  
  if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) {
      
    Serial.print(canMsg.can_id, HEX); // print ID
    Serial.print(" "); 
    Serial.print(canMsg.can_dlc, HEX); // print DLC
    Serial.print(" ");
    
    for (int i = 0; i<canMsg.can_dlc; i++)  {  // print the data
        
      Serial.print(canMsg.data[i],HEX);
      Serial.print(" ");

    }

    Serial.println();      
  }

}

please tell me if you need me to provide more info.

mcp2515.h (13.4 KB)

ok. not sure right now what may be wrong but...

for degugging purposes, in your MEGA code, could you change "mcp2515.sendMessage(&canMsg1);" and "mcp2515.sendMessage(&canMsg2 );" to

if (mcp2515.sendMessage(&canMsg1) == MCP2515::ERROR_OK) Serial.println("Messages sent");
else Serial.println("Msg1 TX error");
         
if (mcp2515.sendMessage(&canMsg2) == MCP2515::ERROR_OK) Serial.println("Messages sent");
else Serial.println("Msg2 TX error");

that way you can see if it is returning an error on the code side as well

also what is the Xtral frequency on that MCP2515 sheild? it is 16MHz or 8MHz?

It is 8MHz. I will try your edit tomorrow when I get home and update here.

Also on the Part 2 side, do u think the approach will work? Read from the CAN Bus and Print on the LCD? Is there anything that I will have to take care before hand and make sure while writing my Code?

confused_man:
It is 8MHz. I will try your edit tomorrow when I get home and update here.

if its 8MHz, you should also change "mcp2515.setBitrate(CAN_500KBPS);" to
"mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ);"

confused_man:
Also on the Part 2 side, do u think the approach will work? Read from the CAN Bus and Print on the LCD? Is there anything that I will have to take care before hand and make sure while writing my Code?

I don't see why not.

What I would do to start with is what you are (I assume) doing already ie

  1. get a working code for the MCP2515
  2. then, I would get a code working for the LCD e.g try printing "Hello World!" to the LCD.
  3. then try combining the 2 together

Okay, I got it to work by setting the “mcp2515.setBitrate(CAN_500KBPS);” to “mcp2515.setBitrate(CAN_250KBPS);” it worked like a charm. even with the LCD and the combined code.
But i then checked via a CAN Interface and CANoe, the bitrate was only 125kbps in the bus not 250kbps as specified. Same with “mcp2515.setBitrate(CAN_125KBPS);” it got reduced by a factor of 2! The bitrate was being set at half! = 62.5kbps!

I do step 2 by increasing it to “mcp2515.setBitrate(CAN_500KBPS);” now the bitrate has to be 250kbps but no! the communication halts!!

sherzaad:
if its 8MHz, you should also change “mcp2515.setBitrate(CAN_500KBPS);” to
mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ);

Okay I tried this and still the same! there is no communication at “500kbps” but happens if i enter “250kbps” but the actual commucation is happening at 125kbps only! My head hurts due to confusion! :confused: Is it some problem with my library file? can u recommend me a better library file if that’s the issue.

confused_man:
Okay, I got it to work by setting the "mcp2515.setBitrate(CAN_500KBPS);" to "mcp2515.setBitrate(CAN_250KBPS);" it worked like a charm. even with the LCD and the combined code.
But i then checked via a CAN Interface and CANoe, the bitrate was only 125kbps in the bus not 250kbps as specified. Same with "mcp2515.setBitrate(CAN_125KBPS);" it got reduced by a factor of 2! The bitrate was being set at half! = 62.5kbps!

I do step 2 by increasing it to "mcp2515.setBitrate(CAN_500KBPS);" now the bitrate has to be 250kbps but no! the communication halts!!

Okay I tried this and still the same! there is no communication at "500kbps" but happens if i enter "250kbps" but the actual commucation is happening at 125kbps only! My head hurts due to confusion! :confused: Is it some problem with my library file? can u recommend me a better library file if that's the issue.

Now I'm the confused_man! I could not make sense of what achieve from what you wrote!:confused:

to monitor the CAN bus, you connected CANoe to what? the MEGA, the uno or did you splice into the UNO-MEGA CAN bus? have you included the CAN terminators in your circuit?

What was your Config setup in CANoe to monitor the CAN bus ie what did you set the CAN_speed to in CANoe?

when using "mcp2515.setBitrate(CAN_500KBPS);" it assumes your shield has a 16MHz Xtal. hence the alternative code I gave you.