CAN Bus Shield & Arduino Mega

Hi All,

I have an Arduino Mega, and a Sparkfun Canbus shield, and I’m trying to read data from my Syvecs ECU (not OBD-II per se).

I am using the Canbusv4 library.

I have found all the info regarding changing the pin assignments for the Arduino Mega, and think I have got that part working OK, but am still having issues reading/writing CAN data.

This is my modification to the defaults.h file:

#ifndef	DEFAULTS_H
#define	DEFAULTS_H

#define P_MOSI          B,2 // pin 51
#define P_MISO          B,3 // pin 50
#define P_SCK           B,1 // pin 52
#define MCP2515_CS      B,0 // pin 53
//#define MCP2515_INT     E,4 // pin 2
#define	MCP2515_INT			D,2 //Pin 19
#define LED2_HIGH       H,5 // pin 8
#define LED2_LOW        H,5 // pin 8


#endif	// DEFAULTS_H

With no CAN-H or CAN-L cables connected to the shield, I can get the ‘CAN Init OK’ message, but only when running with 1000kbps baud rate. I’m achieving this using the following line in Canbus.h:

#define CANSPEED_1000 0

I have tried connecting my DSO scope to the CAN lines whilst repeatedly sending CAN messages using the code below, but I see no change through the scope:

#include <Canbus.h>
#include <defaults.h>
#include <global.h>
#include <mcp2515.h>
#include <mcp2515_defs.h>
#include <SPI.h>

char buffer[512];  //Data will be temporarily stored to this buffer before being written to the file
int read_size=0;   //Used as an indicator for how many characters are read from the file
int count=0;       //Miscellaneous variable

void setup() {
 Serial.begin(115200);
  Serial.println("ECU Reader");  /* For debug use */
  
  if(Canbus.init(CANSPEED_1000))  /* Initialise MCP2515 CAN controller at the specified speed */
  {
    Serial.println("CAN Init ok");
  } else
  {
    Serial.println("Can't init CAN");
  } 

delay(2000);

}

void loop() 
{
tCAN message;

        message.id = 0x631; //formatted in HEX
        message.header.rtr = 0;
        message.header.length = 8; //formatted in DEC
        message.data[0] = 0x40;
	message.data[1] = 0x05;
	message.data[2] = 0x30;
	message.data[3] = 0xFF; //formatted in HEX
	message.data[4] = 0x00;
	message.data[5] = 0x40;
	message.data[6] = 0x00;
	message.data[7] = 0x00;

mcp2515_bit_modify(CANCTRL, (1<<REQOP2)|(1<<REQOP1)|(1<<REQOP0), 0);
mcp2515_send_message(&message);

      
}

Can anyone help me please? I’m tearing my hair out!!

Specific questions:

  1. When the CAN Bus initialises, is that simply reporting the SPI connection between the Arduino and the Shield had been successful?

  2. I’ve read lots about Baud rate, bit rate, and bit timing. I know my ECU CAN runs at 1Mbps, and I know the Frame identifiers. Is bit timing likely to affect messages being ‘scopeable’? I can’t see how it would?!

  3. Does the Sparkfun shield have a 120ohm terminating resistor built in?

Sorry for my first noob post. Hope someone can help!!

ShmedUK:
I have tried connecting my DSO scope to the CAN lines whilst repeatedly sending CAN messages using the code below, but I see no change through the scope:

While You are looking with Your DSO scope for You CAN messages, are You connected to the “alive” CAN network?

The MCP2551 does not “power up” the CAN network and if You were not connected to CAN network, You could send what You want, You will see nothing.

  1. When the CAN Bus initialises, is that simply reporting the SPI connection between the Arduino and the Shield had been successful?
Yes, You cont have to be on CAN network and You will see init ok.
  1. I’ve read lots about Baud rate, bit rate, and bit timing. I know my ECU CAN runs at 1Mbps, and I know the Frame identifiers. Is bit timing likely to affect messages being ‘scopeable’? I can’t see how it would?!
If You are not using scope for the first time, which i assume You are not, You should be able to find some signal. Most of the scopes have small memory to store the whole frame, but You could see, if the signal is there, or not.

Btw, what project it is? Some already running stuff?

  1. Does the Sparkfun shield have a 120ohm terminating resistor built in?
No, You could see it in the schematic diagram.

My project was something different.

I used UNO to just listen for some messages on my Peugeot 508 and to control electronic parking brake based on how strong i press the brake pedal while car is stopped and so.

I have CAN-BUS analyzer tool from Microchip, so i was able to actually “see” the messages and then check, if i can get them via CAN-BUS shield.