Example Code for Arduino-Specified ID Data Frame (Interrupt Mode)

This experiment is designed to only receive data frames with the specified ID configured during the initialization of the CAN module, and reception is implemented in interrupt mode.

Hardware Preparation

Item SKU / Model Qty Link
Arduino UNO Arduino UNO 2 Purchase
CAN-BUS Shield V2 DFR0370 2 Purchase
Dupont Cable 4

Software Preparation

Item Version / Description Link
Arduino IDE v1.6.5 Download
CAN-BUS Shield Library MCP2515 (v2.0) GitHub
Installation Tutorial Arduino IDE Library Installation View Guide

Wiring Diagram

DFR0370 WIRING DIAGRAM

Other Preparation Work

  1. Install the CAN-BUS Shield v2.0 Library in Arduino IDE.
  2. Ensure the power switch on the shield is set correctly (OFF for Arduino power, ON for DB9 power).
  3. Connect the CAN-H and CAN-L lines between the two shields using Dupont cables.

Sample Code

Receiver Code

/**************************************************************************************************************
*demo: CAN-BUS Shield, receive data with interrupt mode, and set mask and filter
* when in interrupt mode, the data coming can't be too fast, must >20ms, or else you can use check mode
* Jansion, 2015-5-27
****************************************************************************************************************/
#include <SPI.h>
#include "df_can.h"
const int SPI_CS_PIN = 10;
MCPCAN CAN(SPI_CS_PIN);                                    // Set CS pin
unsigned char flagRecv = 0;
unsigned char len = 0;
unsigned char buf[8];
char str[20];
void setup()
{
    Serial.begin(115200);
    int count = 50;                                     // the max numbers of initializint the CAN-BUS, if initialize failed first!.
    do {
        CAN.init();   //must initialize the Can interface here!
        CAN.init_Mask(MCP_RXM0, 0, 0x3ff);
        CAN.init_Mask(MCP_RXM1, 0, 0x3ff);
        CAN.init_Filter(MCP_RXF0, 0, 0x04);
        CAN.init_Filter(MCP_RXF1, 0, 0x05);
        CAN.init_Filter(MCP_RXF3, 0, 0x07);
        CAN.init_Filter(MCP_RXF4, 0, 0x08);
        CAN.init_Filter(MCP_RXF5, 0, 0x09);
        if(CAN_OK == CAN.begin(CAN_500KBPS))
        {
            Serial.println("DFROBOT's CAN BUS Shield init ok!");
            break;
        }
        else
        {
            Serial.println("DFROBOT's CAN BUS Shield init fail");
            Serial.println("Please Init CAN BUS Shield again");
            delay(100);
            if (count <=1) Serial.println("Please give up trying!, trying is useless!");
        }
    }while(count--);
    attachInterrupt(0, MCP2515_ISR, FALLING);
}

void MCP2515_ISR()
{
    flagRecv =1;
}

void loop()
{
    if(flagRecv)
    {
        flagRecv=0;
        CAN.readMsgBuf(&len, buf);
        Serial.println("\r\n------------------------------------------------------------------");
        Serial.print("Get Data From id: ");
        Serial.println(CAN.getCanId());
        for(int i=0; i<len; i++)
        {
            Serial.print("0x");
            Serial.print(buf[i], HEX);
            Serial.print("\t");
        }
        Serial.println();
    }
}

Sender Code

/***************************************************************
* demo: set_mask_filter_send
* this demo will show you how to use mask and filter
* Jansion, 2015-5-27
*****************************************************************/
#include <df_can.h>
#include <SPI.h>
const int SPI_CS_PIN =10;
MCPCAN CAN(SPI_CS_PIN);
void setup()
{
    Serial.begin(115200);
    int count=50;
    do {
        CAN.init();
        if(CAN_OK == CAN.begin(CAN_500KBPS))
        {
            Serial.println("DFROBOT's CAN BUS Shield init ok!");
            break;
        }
        else
        {
            Serial.println("DFROBOT's CAN BUS Shield init fail");
            Serial.println("Please Init CAN BUS Shield again");
            delay(100);
            if(count<=1) Serial.println("Please give up trying!, trying is useless!");
        }
    }while(count--);
}

unsigned char data[8] = {'D','F','R','O','B','O','T','!'};
void loop()
{
    for(int id=0; id<10; id++)
    {
        memset(data, id, sizeof(data));
        CAN.sendMsgBuf(id,0,sizeof(data),data);
        delay(100);
    }
}

Result

Receiver serial port output shows data frames only from allowed IDs (0x04,0x05,0x07-0x09):

dfr0370

Was this article helpful?

TOP