I use an Atmel 2560 with MCP2551.
Here the example sketch
// Copyright (c) Sandeep Mistry. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#include <CAN.h>
unsigned long synchMillis = millis();
unsigned long dataMillis = millis();
const uint8_t NODE_ID = 0x12;
const uint8_t PINIO_CS = 31;
const uint8_t PINIO_IRQ = 21;
/*
Send all 100mSec the SYNCH message
*/
void sendSynch() {
uint8_t msg[] = {0x7F, 0xFF};
sendCanMessage(0x80, 2, msg);
}
void askSDO() {
uint8_t ask[8] = {0x40, 0x0A, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00};
sendCanMessage(0x612, 8, ask);
}
bool detectBattery() {
return true;
}
void sendCanMessage(uint16_t canId, uint8_t sendLen, uint8_t *msg) {
uint8_t status = 1;
status = CAN.beginPacket(canId);
Serial.print("Status begin packet: ");
Serial.println(status);
status = CAN.write(msg, sendLen);
Serial.print("Status write: ");
Serial.println(status);
status = CAN.endPacket();
Serial.print("Status end packet: ");
Serial.println(status);
Serial.println();
}
void setup() {
Serial.begin(115200);
while (!Serial);
CAN.setPins(PINIO_CS, PINIO_IRQ);
CAN.setClockFrequency(8E6);
while (!CAN.begin(250E3)) {
Serial.println("Init CAN: Error");
delay(100);
}
CAN.onReceive(onReceive);
}
void loop() {
// if battery is detected then send SYNCH
if (millis() - synchMillis >= 100) {
sendSynch();
synchMillis = millis();
}
if (millis() - dataMillis >= 1000) {
askSDO();
dataMillis = millis();
}
}
void onReceive(int packetSize) {
// received a packet
Serial.print("Received ");
if (CAN.packetExtended()) {
Serial.print("extended ");
}
if (CAN.packetRtr()) {
// Remote transmission request, packet contains no data
Serial.print("RTR ");
}
Serial.print("packet with id 0x");
Serial.print(CAN.packetId(), HEX);
if (CAN.packetRtr()) {
Serial.print(" and requested length ");
Serial.println(CAN.packetDlc());
} else {
Serial.print(" and length ");
Serial.println(packetSize);
// only print packet data for non-RTR packets
while (CAN.available()) {
Serial.print((char)CAN.read(), HEX);
}
Serial.println();
}
Serial.println();
}
and the response...
21:07:35.565 -> Status begin packet: 1
21:07:35.565 -> Status write: 2
21:07:35.565 -> Status end packet: 0
21:07:35.565 ->
21:07:35.673 -> Status begin packet: 1
21:07:35.673 -> Status write: 2
21:07:35.673 -> Status end packet: 0
21:07:35.673 ->
21:07:35.783 -> Status begin packet: 1
21:07:35.783 -> Status write: 2
21:07:35.783 -> Status end packet: 0
21:07:35.783 ->
21:07:35.891 -> Status begin packet: 1
21:07:35.891 -> Status write: 2
21:07:35.891 -> Status end packet: 0