Giter Club home page Giter Club logo

arduino-mcp2515's Introduction

Arduino MCP2515 CAN interface library

Build Status


CAN-BUS is a common industrial bus because of its long travel distance, medium communication speed and high reliability. It is commonly found on modern machine tools and as an automotive diagnostic bus. This CAN-BUS Shield gives your Arduino/Seeeduino CAN-BUS capability. With an OBD-II converter cable added on and the OBD-II library imported, you are ready to build an onboard diagnostic device or data logger.
  • Implements CAN V2.0B at up to 1 Mb/s
  • SPI Interface up to 10 MHz
  • Standard (11 bit) and extended (29 bit) data and remote frames
  • Two receive buffers with prioritized message storage

Contents:

Hardware:

CAN Shield

The following code samples uses the CAN-BUS Shield, wired up as shown:

MCP2515 CAN-Shield wiring

Do It Yourself

If you want to make your own CAN board for under $10, you can achieve that with something like this:

MCP2515 with MCP2551 wiring

Component References:

  • MCP2515 Stand-Alone CAN Controller with SPI Interface
  • MCP2551 High-speed CAN Transceiver - pictured above, however "not recommended for new designs"
  • MCP2562 High-speed CAN Transceiver with Standby Mode and VIO Pin - an updated tranceiver since the MCP2551 (requires different wiring, read datasheet for example, also here)
  • TJA1055 Fault-tolerant low speed CAN Transceiver. Mostly used in vehicles.

Software Usage:

Library Installation

  1. Download the ZIP file from https://github.com/autowp/arduino-mcp2515/archive/master.zip
  2. From the Arduino IDE: Sketch -> Include Library... -> Add .ZIP Library...
  3. Restart the Arduino IDE to see the new "mcp2515" library with examples

Initialization

To create connection with MCP2515 provide pin number where SPI CS is connected (10 by default), baudrate and mode

The available modes are listed as follows:

mcp2515.setNormalMode();
mcp2515.setLoopbackMode();
mcp2515.setListenOnlyMode();

The available baudrates are listed as follows:

enum CAN_SPEED {
    CAN_5KBPS,
    CAN_10KBPS,
    CAN_20KBPS,
    CAN_31K25BPS,
    CAN_33KBPS,
    CAN_40KBPS,
    CAN_50KBPS,
    CAN_80KBPS,
    CAN_83K3BPS,
    CAN_95KBPS,
    CAN_100KBPS,
    CAN_125KBPS,
    CAN_200KBPS,
    CAN_250KBPS,
    CAN_500KBPS,
    CAN_1000KBPS
};

Example of initialization

MCP2515 mcp2515(10);
mcp2515.reset();
mcp2515.setBitrate(CAN_125KBPS);
mcp2515.setLoopbackMode();


You can also set oscillator frequency for module when setting bitrate:
mcp2515.setBitrate(CAN_125KBPS, MCP_8MHZ);

The available clock speeds are listed as follows:
enum CAN_CLOCK {
    MCP_20MHZ,
    MCP_16MHZ,
    MCP_8MHZ
};

Default value is MCP_16MHZ

Note: To transfer data on high speed of CAN interface via UART dont forget to update UART baudrate as necessary.

Frame data format

Library uses Linux-like structure to store can frames;

struct can_frame {
    uint32_t can_id;  /* 32 bit CAN_ID + EFF/RTR/ERR flags */
    uint8_t  can_dlc;
    uint8_t  data[8];
};

For additional information see SocketCAN

Send Data

MCP2515::ERROR sendMessage(const MCP2515::TXBn txbn, const struct can_frame *frame);
MCP2515::ERROR sendMessage(const struct can_frame *frame);

This is a function to send data onto the bus.

For example, In the 'send' example, we have:

struct can_frame frame;
frame.can_id = 0x000;
frame.can_dlc = 4;
frame.data[0] = 0xFF;
frame.data[1] = 0xFF;
frame.data[2] = 0xFF;
frame.data[3] = 0xFF;

/* send out the message to the bus and
tell other devices this is a standard frame from 0x00. */
mcp2515.sendMessage(&frame);
struct can_frame frame;
frame.can_id = 0x12345678 | CAN_EFF_FLAG;
frame.can_dlc = 2;
frame.data[0] = 0xFF;
frame.data[1] = 0xFF;

/* send out the message to the bus using second TX buffer and
tell other devices this is a extended frame from 0x12345678. */
mcp2515.sendMessage(MCP2515::TXB1, &frame);

Receive Data

The following function is used to receive data on the 'receive' node:

MCP2515::ERROR readMessage(const MCP2515::RXBn rxbn, struct can_frame *frame);
MCP2515::ERROR readMessage(struct can_frame *frame);

In conditions that masks and filters have been set. This function can only get frames that meet the requirements of masks and filters.

You can choose one of two method to receive: interrupt-based and polling

Example of poll read

struct can_frame frame;

void loop() {
    if (mcp2515.readMessage(&frame) == MCP2515::ERROR_OK) {
        // frame contains received message
    }
}

Example of interrupt based read

volatile bool interrupt = false;
struct can_frame frame;

void irqHandler() {
    interrupt = true;
}

void setup() {
    ...
    attachInterrupt(0, irqHandler, FALLING);
}

void loop() {
    if (interrupt) {
        interrupt = false;

        uint8_t irq = mcp2515.getInterrupts();

        if (irq & MCP2515::CANINTF_RX0IF) {
            if (mcp2515.readMessage(MCP2515::RXB0, &frame) == MCP2515::ERROR_OK) {
                // frame contains received from RXB0 message
            }
        }

        if (irq & MCP2515::CANINTF_RX1IF) {
            if (mcp2515.readMessage(MCP2515::RXB1, &frame) == MCP2515::ERROR_OK) {
                // frame contains received from RXB1 message
            }
        }
    }
}

Set Receive Mask and Filter

There are 2 receive mask registers and 5 filter registers on the controller chip that guarantee you get data from the target device. They are useful, especially in a large network consisting of numerous nodes.

We provide two functions for you to utilize these mask and filter registers. They are:

MCP2515::ERROR setFilterMask(const MASK mask, const bool ext, const uint32_t ulData)
MCP2515::ERROR setFilter(const RXF num, const bool ext, const uint32_t ulData)

MASK mask represents one of two mask MCP2515::MASK0 or MCP2515::MASK1

RXF num represents one of six acceptance filters registers from MCP2515::RXF0 to MCP2515::RXF5

ext represents the status of the frame. false means it's a mask or filter for a standard frame. true means it's for a extended frame.

ulData represents the content of the mask of filter.

Examples

Example implementation of CanHacker (lawicel) protocol based device: https://github.com/autowp/can-usb

For more information, please refer to wiki page .


This software is written by loovee ([email protected]) for seeed studio,
Updated by Dmitry (https://github.com/autowp)
and is licensed under The MIT License. Check LICENSE.md for more information.

Contributing to this software is warmly welcomed. You can do this basically by
forking, committing modifications and then pulling requests (follow the links above
for operating guide). Adding change log and your contact into file header is encouraged.
Thanks for your contribution.

Seeed Studio is an open hardware facilitation company based in Shenzhen, China.
Benefiting from local manufacture power and convenient global logistic system,
we integrate resources to serve new era of innovation. Seeed also works with
global distributors and partners to push open hardware movement.

arduino-mcp2515's People

Contributors

autowp avatar bringert avatar carloshdezm avatar coddingtonbear avatar designer2k2 avatar fragmuffin avatar jxltom avatar kolabse avatar leres avatar lordware avatar meganukebmp avatar olegel avatar pl125 avatar samc1213 avatar thero0t avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

arduino-mcp2515's Issues

How to change the frame data format?

May I ask how to remove the "can_dlc" from the frame data format. The reason is that the BLDC driver has a different protocal which doesn't have "can_dlc" in the data.

Read Extend Frame Format

I try to read extended format with the example file.
My arduino receive the SFF, but never the EFF is displayed on serial console.
It's necessary a particular coniguration ?

Use different pins on Adafruit Metro M0?

Hi,

I want to learn a bit more about CAN-Bus and therefore use an Adafruit Metro M0 to receive some can-messages. On all the wiring diagrams and instructions I find, the MCP2515 module is connected to D2 and D10-13. However D2-D10 are already used on my Adafruit Metro M0 Express. Only D0(RX), D1(TX), A4, A5 and D11-13 are still free. Can I also use these pins instead? And how to change it? In the initialization I only see a way to set the CSI pin:

MCP2515 mcp2515(10);

What about the other pins? How do I change them?

not entering sleep mode properly

Hi, as the SleepMode is not entered most of the times with the current setMode void i suggest the following changes:

put the modifyRegister: https://github.com/autowp/arduino-mcp2515/blob/master/mcp2515.cpp#L167

inside the while loop so it will rewrite the register.

also set the timeout from 10ms to 200ms as the sleep mode entering can take up to 100ms

i also suggest to add the sleepWakeup bit (CANINTF_WAKIF) into the reset void: https://github.com/autowp/arduino-mcp2515/blob/master/mcp2515.cpp#L50

so it will wake up on can messages, or maybe make an extra function to enable or disable that bit so everyone can set if needed.

Extended frame returning wrong MSB ID value

I'm filtering EXT ID's and while the filters are working the returned value is not correct.
Here is the code (from the example receive sketch);

#include <SPI.h>
#include <mcp2515.h>

struct can_frame canMsg;
MCP2515 mcp2515(10);


void setup() {
  Serial.begin(115200);
  SPI.begin();
  
  mcp2515.reset();
  mcp2515.setBitrate(CAN_125KBPS, MCP_8MHZ);
  mcp2515.setConfigMode();
  mcp2515.setFilterMask(0,false, 0x1FF);
  mcp2515.setFilter(0, false, 0x1BA);
  mcp2515.setFilter(1, false, 0x1BA);
  mcp2515.setFilterMask(1,true,0x1FFFFFFF);   //Set mask to compare all bits in EXT ID
  mcp2515.setFilter(2, true, 0x0E345678);     //Set filter for ID 0E345678, returns 8E345678.
  mcp2515.setFilter(3, true, 0x00000001);     //Set filter for ID 00000001, returns 80000001.
  mcp2515.setFilter(4, true, 0x12345678);     //Set filter for ID 12345678, returns 92345678.
  mcp2515.setFilter(5, true, 0x1F00000F);     //Set filter for ID 1F00000F, returns 9F00000F.
  mcp2515.setNormalMode();
  
  Serial.println("------- CAN Read ----------");
  Serial.println("ID  DLC   DATA");
}

void loop() {
  
  if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) {
      
    Serial.print(canMsg.can_id, HEX); // print ID
    Serial.print(" "); 
    Serial.print(canMsg.can_dlc, HEX); // print DLC
    Serial.print(" ");
    
    for (int i = 0; i<canMsg.can_dlc; i++)  {  // print the data
        
      Serial.print(canMsg.data[i],HEX);
      Serial.print(" ");

    }

    Serial.println();      
  }

}

The following is giving me the wrong results;

mcp2515.setFilter(2, true, 0x0E345678); //Set filter for ID 0E345678, returns 8E345678.
mcp2515.setFilter(3, true, 0x00000001); //Set filter for ID 00000001, returns 80000001.
mcp2515.setFilter(4, true, 0x12345678); //Set filter for ID 12345678, returns 92345678.
mcp2515.setFilter(5, true, 0x1F00000F); //Set filter for ID 1F00000F, returns 9F00000F.

The most significant nibble is returning the ID + 8.

Is this a function? Is this documented?

Thanks and regards,

Issue Arduino MEGA 2560 and MCP2515

I am trying to connect a Arduino MEGA 2560 to a MCP2515 can controller with a TJA1050 transceiver and SPI interface. The board with the can controller and transceiver has a 8.000 mhz crystal. The problem is when calling mcp2515.setBitrate(CAN_125KBPS, MCP_8MHZ);
the return value is 1. Tested this on a Arduino UNO where it works. Tried calling setBitrate() with different parameters too but it seems that it fail every time.

EDIT:
I have managed to make it work by chaging the wiring in the following way:
Name----Mega2560---MCP2515
INT-------2----------INT
MISO------50---------SO
MOSI------51---------SI
SCK-------52---------SCK
SS--------53---------CS
VCC-------5V---------VCC
GND-------GND--------GND

Now i can call successfully mcp2515.setBitrate(CAN_125KBPS, MCP_8MHZ);

This issue can be closed. :D

Adding RTR ID

The library is working great, however I am struggling with adding an RTR tag to the CAN ID.

Is anyone able to provide a quick pointer? I've been searching the interwebs for the past 3 hours or so

Thank you!

Can't get filters to work

I am trying to get filters to work. I have read up on the special handling that the mcp2515 needs. Here is a simple sketch that should filter out ALL can messages that don't have an id of 0x0000 (which should be all).

When I run this on my CAN bus I still get the data that is being sent from other devices. I have tried every combination I can think of.

I have a adafruit huzzah32 which uses the esp32. I also have this CAN featherboard:

https://easyeda.com/armin.von_collrepp/Adafruit_CAN_FeatherWing-0YRL3lfxP

The CAN board works fine as I am getting the CAN traffic. I just can get it to filter correctly.

Can someone please tell me why filters don't seem to be working?

#include <SPI.h>
#include <mcp2515.h>

struct can_frame canMsg;
MCP2515 mcp2515(A5);

void setup()
{
  Serial.begin(115200);
  SPI.begin();

  mcp2515.reset();
  mcp2515.setBitrate(CAN_500KBPS);
  mcp2515.setConfigMode();
  mcp2515.setFilterMask(MCP2515::MASK0, false, 0x07FF0000);
  mcp2515.setFilter(MCP2515::RXF0, false, 0x00000000);
  mcp2515.setFilter(MCP2515::RXF1, false, 0x00000000);
  mcp2515.setFilterMask(MCP2515::MASK1, false, 0x07FF0000);
  mcp2515.setFilter(MCP2515::RXF2, false, 0x00000000);
  mcp2515.setFilter(MCP2515::RXF3, false, 0x00000000);
  mcp2515.setFilter(MCP2515::RXF4, false, 0x00000000);
  mcp2515.setFilter(MCP2515::RXF5, false, 0x00000000);
  mcp2515.setNormalMode();
  
  Serial.println("------- CAN Read ----------");
  Serial.println("ID  DLC   DATA");
}

void loop()
{
  if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK)
  {
    Serial.print(canMsg.can_id, HEX); // print ID
    Serial.print(" "); 
    Serial.print(canMsg.can_dlc, HEX); // print DLC
    Serial.print(" ");
    
    for (int i = 0; i<canMsg.can_dlc; i++) 
    {
      Serial.print(canMsg.data[i],HEX);
      Serial.print(" ");
    }

    Serial.println();
  }
      
  delay(10);
}

using the entire frame structure in the arduino code for mcp2515

Hey i'm trying to interface arduino and mcp2515 with busmaster through a Peak USB device.
But since there is some problem with the usage of the frame structure, bus master is neither able to transmit or receive data. So the question is how do I code the entire frame for transmitting or receiving. Examples maybe Start of frame, Arbitration field and the control field. The code needs to follow the same format.
canMsg.can_id = 0x036; //CAN id as 0x036
canMsg.can_dlc = 8; //CAN data length as 8
canMsg.data[0] = h; //Update humidity value in [0]
canMsg.data[1] = t; //Update temperature value in [1]
canMsg.data[2] = 0x00; //Rest all with 0
canMsg.data[3] = 0x00;
canMsg.data[4] = 0x00;
canMsg.data[5] = 0x00;
canMsg.data[6] = 0x00;
canMsg.data[7] = 0x00;
mcp2515.sendMessage(&canMsg); //Sends the CAN message

add travis for autotest

Hi every one,
I propose adding an autotest with Travis (CI) to make this library more stable.
I am really happy with our work and I want to participate in this project. If you want, I can write the .travis.yml script.

can you add an oscillator part number to the parts list?

I am building the DIY circuit and see in the datasheet for the MCP2515 that there is a dependency on selecting the correct type of oscillator so is it possible to add a specific oscillator to the parts list as a reference of what works with this circuit?

Thanks,
dave

A default for oscillator frequency can be determined by looking at F_CPU

I'm using something similar to:

#if F_CPU == 20000000UL
#define MCP_SPEED MCP_20MHZ
#elif F_CPU == 16000000UL
#define MCP_SPEED MCP_16MHZ
#elif F_CPU == 8000000UL
#define MCP_SPEED MCP_8MHZ
#else
#error "Unsupported F_CPU value"
#endif
[...]
mcp2515.setBitrate(CAN_500KBPS, MCP_SPEED);

But it wouldn't be difficult to change the default speed from 16MHz to a speed based on the arduino clock. I'm happy to make the changes and issue a pull request if this is interesting.

Also you could free up some program memory by changing setBitrate() to conditionally compiling the canClock cases.

Allowing to pass a custom SPI object

Hi,

at the moment it is not possible to pass a custom SPI instance to the MCP2515 constructor.
I had this need in a project, so I have made the change on a local copy.
If you think this could be useful to others, I can provide a pull request.

Regards.

Not compatible with Adafruit Metro M0?

Hi,

I sucessfully used the mcp2515 with an Arduino Nano Every. Now I tried to upload the same code to the Adafruit Metro M0 Express, unfortunately this causes the Metro to "crash".
After uploading the code, the Metro stops the program (I added a blinking LED to the Code, so you can see if it is running). Also it loses the connection to the PC, since the Port does not show up anymore and I can't see the serial monitor. The only thing working is the "ON" LED on the Metro. Everything else seems to stop working after uploading the code.

I need to enable verbose mode for uploading and double press reset while uploading, so the Metro resets and the Port shows up again.

Down below is the code I am using. It is very similar to the "read" example.
I uncommented a lot of lines to see when the problem occurs. Already at the top there is a command to set a pin: MCP2515 mcp2515(10)
At this point the Metro already seems to have problems. I also tried using different pins instead of D10, but without any luck. Why would the Metro already have problems with this part? Could this be a problem with the missing EEPROM of the Metro M0?

`#include <SPI.h>
#include <mcp2515.h>

struct can_frame canMsg;
// MCP2515 mcp2515(10); // this line causes problems? After uncommenting this line, the Metro loses connection

void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
// mcp2515.reset();
// mcp2515.setBitrate(CAN_125KBPS);
// mcp2515.setNormalMode();
//
// Serial.println("------- CAN Read ----------");
// Serial.println("ID DLC DATA");
}

void loop() {
digitalWrite(LED_BUILTIN, HIGH); // aktiviere LED
delay(500);
digitalWrite(LED_BUILTIN, LOW); // deaktiviere LED
delay(500);

// if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) {
// Serial.print(canMsg.can_id, HEX); // print ID
// Serial.print(" ");
// Serial.print(canMsg.can_dlc, HEX); // print DLC
// Serial.print(" ");
//
// for (int i = 0; i<canMsg.can_dlc; i++) { // print the data
// Serial.print(canMsg.data[i],HEX);
// Serial.print(" ");
// }
//
//
// if(canMsg.data[0]==0x8E){ // Check if first Data of Message is correct
// Serial.print("\nFirst Data of Msg1 correct: ");
// Serial.print(canMsg.data[0], HEX);
// }
//
// Serial.println();
// }
}`

sends 2 messages at once when I add multiple values to a frame

Hey,

I run in an issue with a custom board I made (uses an Atmega328PB), but it feels like this is a general issue. If I send a message adding only 1 value everything works fine.
For example this is ok:

    canMsg1.can_id  = CANID;
    canMsg1.can_dlc = 8;
    canMsg1.data[1] = A0_Value;
    canMsg1.data[2] = 0x00;
    canMsg1.data[3] = 0x00;
    canMsg1.data[4] = 0x00;
    canMsg1.data[5] = 0x00;
    canMsg1.data[6] = 0x00;
    canMsg1.data[7] = 0x00;

    mcp2515.sendMessage(&canMsg1);

but once I populate the full message it always bursts the message out twice when calling sendMessage.
Does someone have an idea whats wrong here?:

    canMsg1.can_id  = CANID;
    canMsg1.can_dlc = 8;
    canMsg1.data[0] = highByte(A0_Value);
    canMsg1.data[1] = lowByte(A0_Value);
    canMsg1.data[2] = highByte(A1_Value);
    canMsg1.data[3] = lowByte(A1_Value);
    canMsg1.data[4] = highByte(A2_Value);
    canMsg1.data[5] = lowByte(A2_Value);
    canMsg1.data[6] = highByte(A3_Value);
    canMsg1.data[7] = lowByte(A3_Value);
    
    mcp2515.sendMessage(&canMsg1);

Thanks for the help!
Andre

Multiple Nodes, 1 Receiver, 2 Transmitter

Hello, i need to communicate with 3 arduinos, 2 transmitter data and one receiver.. But how can i coordinate them? im wondering what can happen when both trasnmitter put on the can line different data with different ID. Maybe they coordinate by theyself and keep the data message in the buffer? Or i need to write a function for this? Thanks a lot

How to know if received frame is standard frame or extended frame?

I want to know if my received frame is the standard or extended frame.

I have the following code to receive a can frame.
`
while ((mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK)) {
debug_log("I got some data!");

    Serial.print(canMsg.can_id, HEX); // print ID
    Serial.print(" "); 
    Serial.print(canMsg.can_dlc, HEX); // print DLC
    Serial.print(" ");
    
    for (int i = 0; i < 8; i++)  {  // print the data
        Serial.print(canMsg.data[i],HEX);
        Serial.print(" ");
    }
    Serial.println(" ");
    //debug_log(msg_string);
}

`

I also want to display the frame format of the above CAN message.
How to do that?

Interrupt based reads hang with high throughput writes

If the delay in the loop of the example CAN_write is removed, the proposed interrupt based solution hangs after a few messages. Even a delay of 1 millisecond is not enough to prevent this behaviour. The polling based solution works in any case.

Conflict with CCS811

Hello.
I cant init ccs sensor if mcp2515 initialized. Minimal code is:

#include <SPI.h>
#include <mcp2515.h>
#include "Adafruit_CCS811.h"

Adafruit_CCS811 ccs;
MCP2515 CAN0(10);

void setup() {
  Serial.begin(115200);
  if(!ccs.begin()){
    Serial.println("Failed to start sensor! Please check your wiring.");
    while(1);
  }
}

void loop() {
  delay(5000);
}

if i comment out line "MCP2515 CAN0(10);", sensor initializing properly.
Sensor connected to i2c bus.
Arduino pro mini, atmega 328p 5v 16MHz.

Mcp2515 does not send in normal mode

I am running this driver on esp8266 devices. When I have the mcp2515 in loopbackmode I can send and receive data. This indicates that my spi connections are ok. When I use the example read and write programs on two separate esp8266 with mcp2515. The sender fills up the 3 message queues in the mcp2515 but the receiver shows no data.
Is this driver suited for esp devices? And what can I check to see what is wrong?

Wiring

Could you include a wiring setup including SPI connections? Thanks

No reading on lower speeds

I use your library and it works fine at 500 and 1000KBPS. It sends and reads CAN data.
But as soon as I drive it in lower speeds as 500 (250, or even 50) it is only sending data out on CAN, not reading/receiving it anymore.

Is that a known thing? Or do I miss something?

I am running with internal Clock on an Atmega 328P at 8Mhz. Thanks

No data receive

Hi

I have made appropriate changes to use 8Mhz crystal but unfortunately, I don't see any data in receiver.

I am not sure what's wrong with my code. Can you please guide me to debug the issue or any ways to figure out the problem.

Just to add: I am using Arduino uno

How does the library handle error messages?

Hey guys. The library has been working amazing so far. I just couldn't find a reference in the code or documentation that tells how would the code handle error frames that comes from CAN. Could you direct me to a resource and specific cod in the library that handles these errors?

Cheers

Problem with Arduino Due

I'm trying to use your library with Arduino Due and it won`t work.
I spent some time to make it working and I found solution.
Two line were commented in the file mcp2515.cpp

MCP2515::MCP2515(const uint8_t _CS)
{
//SPI.begin(); //<==Commented
SPICS = _CS;
pinMode(SPICS, OUTPUT);
//endSPI(); //<==Commented
}
After this change Due successfully works with your samples and Can Hacker works too.
Are you sure SPI.begin() is needed here? All your examples also contains SPI.begin() in Setup.

Connecting two MCP2515 shields at 500KBPS

Using the example code, I'm able to get two arduino nanos + mcp2515 shields (using 8mhz crystals) to communicate @ 125kbps, and default frequency (16mhz). When I set both 'sender' and 'receiver' to 500kbps, the receiver doesn't receive anything, regardless of the frequency I set.

I can write and read @ 500kbps to an arduino uno with an mcp2515 shield (16mhz), but the nano must be set @ 8mhz for communication to work.

So my question is, is there some limitation to using nanos to communicate in this way, or is there something I'm missing?

Use of RTR and others

When I examine the details, the following statement goes!

/ * 32 bit CAN_ID + EFF / RTR / ERR flags * /

So how do we express this in code?

Checking Free Buffer

Hello! Thanks a lot for this project, it is incredible. I'm having a problem when I try to send a lot of messages one after the other. I only receive a few of them. If i use a delay between messages, the problem is solved, but I wanted to know if there is a function to check if the buffer is free to send the next one instead the delay.
I hope you can help me. Thank you a lot!

can communication between esp32 and arduino not working

Hi

I am trying to make an ESP32 (running arduino) communicate with an arduino uno. Both have an MCP2515 board wired up as the following picture shows.

Group 9

To enable the can communication via the board I am using the arduino-mcp2515 library. I uploaded the send example to the arduino uno, and the receive example to the ESP32 (changing the chip select to 5 wich is the standard chip select for the SPI bus on the ESP32), yet it is not working.

Does anyone know what I might be doing wrong?

Sending code on the Arduino Uno
`#include <SPI.h>
#include <mcp2515.h>

struct can_frame canMsg1;
struct can_frame canMsg2;
struct can_frame canMsg3;
MCP2515 mcp2515(10);

void setup() {
canMsg1.can_id = 0x0F6;
canMsg1.can_dlc = 8;
canMsg1.data[0] = 0x8E;
canMsg1.data[1] = 0x87;
canMsg1.data[2] = 0x32;
canMsg1.data[3] = 0xFA;
canMsg1.data[4] = 0x26;
canMsg1.data[5] = 0x8E;
canMsg1.data[6] = 0xBE;
canMsg1.data[7] = 0x86;

canMsg2.can_id = 0x036;
canMsg2.can_dlc = 8;
canMsg2.data[0] = 0x0E;
canMsg2.data[1] = 0x00;
canMsg2.data[2] = 0x00;
canMsg2.data[3] = 0x08;
canMsg2.data[4] = 0x01;
canMsg2.data[5] = 0x00;
canMsg2.data[6] = 0x00;
canMsg2.data[7] = 0xA0;

while (!Serial);
Serial.begin(115200);

mcp2515.reset();
mcp2515.setBitrate(CAN_125KBPS);
mcp2515.setNormalMode();

Serial.println("Example: Write to CAN");
}

void loop() {
mcp2515.sendMessage(&canMsg1);
Serial.print("sent ");
Serial.print(canMsg1.can_id, HEX);
Serial.println();
delay(5000);
mcp2515.sendMessage(&canMsg2);
Serial.print("sent ");
Serial.print(canMsg2.can_id, HEX);
Serial.println();
delay(5000);
}`

Receiving code on the ESP32
`#include <SPI.h>
#include <mcp2515.h>

struct can_frame canMsg;
MCP2515 mcp2515(5);

void setup() {
Serial.begin(115200);

mcp2515.reset();
mcp2515.setBitrate(CAN_125KBPS);
mcp2515.setNormalMode();

Serial.println("------- CAN Read ----------");
Serial.println("ID DLC DATA");
}

void loop() {
if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK)
{
Serial.print("CAN ID: ");
Serial.println(canMsg.can_id);
Serial.print("data: ");
for (int i = 0; i<canMsg.can_dlc; i++) { // print the data
Serial.print(canMsg.data[i],HEX);
Serial.print(" ");
}
Serial.println();
}
}`

Thanks in advance!

Send high speed

I'm trying to send frames with a 1ms delay, but here is the monitor print, are frames phisically sent with 1ms intervall or are they really sent all in a row?

14:56:33.001 -> Messages sent
14:56:33.001 -> Messages sent
14:56:33.001 -> Messages sent
14:56:33.001 -> Messages sent
14:56:33.001 -> Messages sent
14:56:33.001 -> Messages sent
14:56:33.001 -> Messages sent
14:56:33.001 -> Messages sent
14:56:33.001 -> Messages sent
14:56:33.001 -> Messages sent
14:56:33.001 -> Messages sent
14:56:33.001 -> Messages sent
14:56:33.001 -> Messages sent
14:56:33.001 -> Messages sent
14:56:33.001 -> Messages sent
14:56:33.001 -> Messages sent
14:56:33.001 -> Messages sent
14:56:33.001 -> Messages sent
14:56:33.001 -> Messages sent
14:56:33.001 -> Messages sent
14:56:33.001 -> Messages sent
14:56:33.001 -> Messages sent
14:56:33.001 -> Messages sent
14:56:33.001 -> Messages sent
14:56:33.001 -> Messages sent
14:56:33.001 -> Messages sent
14:56:33.001 -> Messages sent
14:56:33.001 -> Messages sent
14:56:33.036 -> Messages sent
14:56:33.036 -> Messages sent
14:56:33.036 -> Messages sent

Connecting two mcp2515 shields

Hello. Is there a way to connect two mcp2515 shields to one Arduino Nano? I can only see how to configure SPI CS pin and what about wiring of the other pins?

Weird message behavior

I am experiencing some weird message behavior.
I am using your library since i like its API and the ability to run 20 MHZ crystals on the mcp2515 which is what I am using on my custom boards.
On the bus I have 4 cm of wire terminated on both boards with 120R resistors. The boards have common grounds.
Running the example can_write and can_read on each board respectively, I am getting only one of the two messages on the receiver side, and some times, especially when i reset the sender, I get the messages but as seen in the screenshot. Any idea why this might happen? Thanks in advance.
Imgur

83,333

Здравствуйте, у меня ничего не принимает и не отправляет в мерседес w220 по 83.333... Дело точно не в подключении, с библиотекой mcp_can.h все прекрасно работает, а вот ваша библиотека молчит, то есть если одно устройство с mcp_can.h будет отправлять то другое устройство с вашей библиотекой на 83,333 не услышет ничего а на 250 и выше все слышит и передает... В машине соответственно работает только mcp_can.h ... Подскажите пожалуйста, как можно их подружить? Очень хочется канхакер, но ему если принудительно в canhacker.cpp задать скорость 83.333, то в эфире тоже тишина...

Bit stuffing error is coming while trying to send data

Hi I'm trying to send data frames through the arduino board and read it using a tool. Its showing a bit stuffing error for me. Pls give a solution. I'm using a TJA1050 transciever along with an MCP2515 controller.

Did not wake-up after in sleep mode

Hi,

when I put the MCP2515 in sleep mode - setSleepMode() - it don't wake up when the CAN bus is back active.

Do I have to add something in the .ino or the lib itself?

Thank you in advance

Receive not working

Hello,

I have two arduinos with a MCP2515 module on each. I installed your library and ran your CAN_write code on one, and your CAN_read code on the other. The sender says it is sending messages and the LED seems to confirm that.

The receiver never gets anything, though. It seems to initialize and prints the header (------- CAN Read ----------
ID DLC DATA), but never gets any data. If I put a print where the else of the if would be, it goes there every time. So maybe there is an error? If so, how do I figure out what the error is?

I tried both as senders and both as receivers, and they both seem to send fine, but neither receives. I've also tried with and without terminators and it doesn't seem to matter.

Compatible with other MCP chips/ CAN FD?

Hi!
Great library, thanks!
I wonder if this library will also work with the mcp25625 - this chip has the transceiver integrated and will safe some room on the pcb.

Also interesting if it will work with the CAN FD chip from microchip.

faulty function can_id

Hi!
I am sending a CAN message with id 0x11 (17 dec, 10001 bin) and reading the received CAN id with this command:
Serial.print("Message read: "); Serial.print(canMsg.can_id); Serial.print(" = "); Serial.println(CAN_receive);

The output is:
Message read: 2147483665 = 17

which is 80000011 in hex, or a 32bit binary. So it is making an output of 32bits on a 29 bit identifier. Maybe it is returning some other parts of the identifier/frame?

Or do I use this function wrong?

Error Frames sending direction

When transmitting data with this Library I get randomly errorframes RX (so the device holding the library sending to other nodes). It occurs randomly and go onb for several minutes. The device still reads on CAN, just sending seems to be distorted.

I can´t identify hardware problems.
Error frames are the following:

ECC 10 10 1000 CRC Sequence
ECC 10 10 10 10 Data
ECC 01 11 1000 CRC Delimiter
ECC 1111 00 11 Tolerate Dominant Bits

Thankfull for any hints

INT pin of the MCP2515

Hello.

I can not find any reference in the libraries to the INT pin from the MCP2515. Can I assume that the INT pin is not necessary to connect ?

Thank you.

How to define a Extended Frame Format ?

I have try to send a message within Extended Frame Format.
When I use a USB-CAN Analyst to read the message, it show me wrong ID, And I was told it is Stranded Frame Format.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.