Arduino Leonardo + CAN-BUS

With the example of your code does not work (github).

I need sample code to work for Arduino Leonardo + CanBusShield.

BaudRate: 1000KBPS

Thanks!

Receive code:

[code]// demo: CAN-BUS Shield, receive data
#include <mcp_can.h>
#include <SPI.h>

unsigned char Flag_Recv = 0;
unsigned char len = 0;
unsigned char buf[8];
char str[20];

void setup()
{
CAN.begin(CAN_500KBPS); // init can bus : baudrate = 500k
attachInterrupt(0, MCP2515_ISR, FALLING); // start interrupt
Serial.begin(115200);
}

void MCP2515_ISR()
{
Flag_Recv = 1;
}

void loop()
{
if(Flag_Recv) // check if get data
{
Flag_Recv = 0; // clear flag
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
Serial.println(“CAN_BUS GET DATA!”);
Serial.print("data len = ");
Serial.println(len);

  for(int i = 0; i<len; i++)            // print the data
  {
    Serial.print(buf[i]);Serial.print("\t");
  }
  Serial.println();
}

}

/*********************************************************************************************************
END FILE
*********************************************************************************************************/[/code]

Send code:[code]// demo: CAN-BUS Shield, send data
#include <mcp_can.h>
#include <SPI.h>

void setup()
{
Serial.begin(115200);
// init can bus, baudrate: 500k
if(CAN.begin(CAN_500KBPS) ==CAN_OK) Serial.print(“can init ok!!\r\n”);
else Serial.print(“Can init fail!!\r\n”);
}

unsigned char stmp[8] = {0, 1, 2, 3, 4, 5, 6, 7};
void loop()
{
// send data: id = 0x00, standrad flame, data len = 8, stmp: data buf
CAN.sendMsgBuf(0x00, 0, 8, stmp);
delay(100); // send data per 100ms
}

/*********************************************************************************************************
END FILE
*********************************************************************************************************/[/code]

hey, I am afraid canbus shield can’t work with leonardo easily, cu’z canbus shield use SPI interface, you know , SPI interface for an UNO is D10-13, but no for a leonardo. SPI interface for a leonardo was in ISCP pin, so you have to cut the origin connect and reconnect it to icsp pin. :smiley:

anyway , it’s really a hard thing~ but i’ll works when you connect the spi interface to the right pin.

more over, the library can work with 1000kbps baudrate now. :smiley: