Mcp2515.h works on MEGA but not on UNO

Hey guys,
I am using a mcp2515 module over the SPI-bus with the mcp2515.h library with the following code:

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


struct can_frame canMsg1;   //UQS-Message 1; CatalystReagentTemperature2, CatalystReagentConcentration  
struct can_frame canMsg2;   //UQS-Mesage 2; AT1SCRCatalystTankLevel, AT1SCRCatalystTankLevel2,  AT1SCRCatalystTankTemperature 
MCP2515 mcp2515(53);        //Chip-Select


//Values CAN-Messages 
int crt2, ctt;         //Dezimal
double crc, ctl, ctl2;  

byte crt2_b, ctt_b, crc_b, ctl_b, ctl2_b1, ctl2_b2;  //Binär
unsigned short ctl2_b;


void setup() {
   while (!Serial);
  Serial.begin(115200);
  
  mcp2515.reset();
  mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ);        //Set Baud-Rate corresponding to Engine subnet //WRONG BAUDRATE!
  mcp2515.setNormalMode();

  canMsg1.can_id  = 0x18FD9BA3 | CAN_EFF_FLAG;    //Extended Frame with 29-Bit ID (SAE J1939)
  canMsg1.can_dlc = 8;                            //8 Byte Data Length
  

  canMsg2.can_id  = 0x18FE56A3 | CAN_EFF_FLAG;
  canMsg2.can_dlc = 8;

  //Values
  crt2=-21; //Temperature in °C, tank and catalyst get same value
  crc=13;    //Catalyst Concentration in %
  ctl=93.7;    //Tank level in %
  ctl2=312.0;   //Tank level in mm
  
  //Message-Calculation
  crt2_b=ctt_b=(byte)(crt2+40);
  crc_b=(byte)(4*crc);
  ctl_b=(byte)(2.5*ctl);
  
  ctl2_b=(short)(10*ctl2);
  ctl2_b1=ctl2_b>>8;      //zweites Byte wegschieben
  ctl2_b2=ctl2_b&0x00FF; //erstes Byte löschen

  canMsg1.data[0] = crt2_b;     //0x41;
  canMsg1.data[1] = crc_b;      // 0x7B;
  canMsg1.data[2] = 0xFF;       //constant Standard-signals
  canMsg1.data[3] = 0xFF;
  canMsg1.data[4] = 0xFF;
  canMsg1.data[5] = 0xF3;
  canMsg1.data[6] = 0xFF;
  canMsg1.data[7] = 0xFF;


  canMsg2.data[0] = ctl_b;        // 0x4D;
  canMsg2.data[1] = ctt_b;        // 0x45;
  canMsg2.data[2] = ctl2_b2;      //0x10;
  canMsg2.data[3] = ctl2_b1;      //0x04; 
  canMsg2.data[4] = 0xFF;         //Standard-signals
  canMsg2.data[5] = 0xFF;
  canMsg2.data[6] = 0xFF;
  canMsg2.data[7] = 0xFF;
  
  
 
  
  Serial.println("Example: Write to CAN");
  Serial.println(ctt_b);
  Serial.println(crt2_b);
}

void loop() {
  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");
  

  Serial.println("Messages sent");
  
  delay(1000);
}

The code works completely fine with the Arduino UNO (CS for UNO is set to 10), but not with the with the MEGA. It has nothing to do with the module itself, since I get "Msg1 TX error" and "Msg2 TX error" in the Serial monitor although its exactly the same code for both.

Does the library not work with the MEGA or am I missing something?

Is the MCP2515 on a 'shield'? For an UNO-shaped 'shield' on a MEGA there is no easy way to connect to Pin 53 so the shield will often use Pin 10 for both UNO and MEGA.

That statement is the complete opposite of the title you've given this thread.

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