Instantly share code, notes, and snippets.

What would you like to do?
MCP2515 Library example
MCP2515 CAN Interface Using SPI
Author: David Harding
Created: 11/08/2010
Modified: 6/26/12 by RechargeCar Inc.
For further information see:
The MCP2515 Library files also contain important information.
This sketch is configured to work with the 'Macchina' Automotive Interface board
manufactured by RechargeCar Inc. CS_PIN and INT_PIN are specific to this board.
This sketch shows the most basic of steps to send and receive CAN messages.
NOTE!!! If you use this sketch to test on a live system I suggest that you comment out the
send messages lines unless you are certain that they will have no detrimental effect!
This example code is in the public domain.
#include <SPI.h> // Arduino SPI Library
#include <MCP2515.h>
// Pin definitions specific to how the MCP2515 is wired up.
#define CS_PIN 85
#define RESET_PIN 7
#define INT_PIN 84
// Create CAN object with pins as defined
void setup() {
Serial.println("Initializing ...");
// Set up SPI Communication
// dataMode can be SPI_MODE0 or SPI_MODE3 only for MCP2515
// Initialise MCP2515 CAN controller at the specified speed and clock frequency
// In this case 125kbps with a 16MHz oscillator
// (Note: This is the oscillator attached to the MCP2515, not the Arduino oscillaltor)
Serial.println("MCP2515 Init OK ...");
} else {
Serial.println("MCP2515 Init Failed ...");
Serial.println("Ready ...");
byte i=0;
// CAN message frame (actually just the parts that are exposed by the MCP2515 RX/TX buffers)
Frame message;
void loop() { = 0;
// This implementation utilizes the MCP2515 INT pin to flag received messages or other events
if(CAN.Interrupt()) {
// determine which interrupt flags have been set
byte interruptFlags = CAN.Read(CANINTF);
if(interruptFlags & RX0IF) {
// read from RX buffer 0
message = CAN.ReadBuffer(RXB0);
if(interruptFlags & RX1IF) {
// read from RX buffer 1
message = CAN.ReadBuffer(RXB1);
// (this is poor code as clearly if two messages are received then the second will overwrite the first)
if(interruptFlags & TX0IF) {
// TX buffer 0 sent
if(interruptFlags & TX1IF) {
// TX buffer 1 sent
if(interruptFlags & TX2IF) {
// TX buffer 2 sent
if(interruptFlags & ERRIF) {
// error handling code
if(interruptFlags & MERRF) {
// error handling code
// if TXBnCTRL.TXERR set then transmission error
// if message is lost TXBnCTRL.MLOA will be set
if(>0) {
// Print message
Serial.print("ID: ");
Serial.print("Extended: ");
if(message.ide) {
} else {
Serial.print("DLC: ");
for(i=0;i<message.dlc;i++) {
Serial.print(" ");
// Send out a return message for each one received
// Simply increment message id and data bytes to show proper transmission
// Note: Please see explanation at top of sketch. You might want to comment this out!
// for(i=0;i<message.dlc;i++) {
// }
// CAN.LoadBuffer(TXB0, message);
// CAN.SendBuffer(TXB0);

This comment has been minimized.

sagaralchem commented Jan 6, 2017


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment