Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dual_CAN.ino error? #11

Open
wants to merge 79 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
d23d62b
Update README.md
coryjfowler Jan 29, 2015
5fda899
Bridge example created
coryjfowler Dec 9, 2015
eecf231
Comment wasn't commented...
coryjfowler Dec 9, 2015
175bec0
Update README.md
coryjfowler May 1, 2016
0901b7a
CNF Calc Tool
coryjfowler Jun 30, 2016
3b4c3a7
Sub-Development copied to master
Jul 1, 2016
80ed864
Update README.md
coryjfowler Jul 1, 2016
a2d28cd
Dual_CAN example replaces this.
coryjfowler Jul 1, 2016
b7fe05d
CAN_Receive replaces this.
coryjfowler Jul 1, 2016
8dd5dc8
CAN_send replaces this.
coryjfowler Jul 1, 2016
03b981f
Formatting and Remote Request
coryjfowler Jul 1, 2016
c353672
Remote Request
coryjfowler Jul 1, 2016
85a7938
Added function information
coryjfowler Jul 1, 2016
14db2dc
Update README.md
coryjfowler Jul 1, 2016
6853d17
Update CAN_receive.ino
coryjfowler Jul 5, 2016
cdd398a
Replaced !!! with !, since !!! is a special command to certain
kingcoyote Jul 15, 2016
4dc07ab
Merge pull request #8 from kingcoyote/master
coryjfowler Jul 21, 2016
f16b698
Redunant function...
coryjfowler Jul 21, 2016
e5d5dfc
Fixed typo...
coryjfowler Aug 15, 2016
1db096f
Update CAN_receive.ino
coryjfowler Aug 15, 2016
e2b6418
Added OSM
coryjfowler Sep 27, 2016
8d1a25c
Added OSM
coryjfowler Sep 27, 2016
9f12b4a
Added OSM
coryjfowler Sep 27, 2016
9b2c298
Fixed typo...
coryjfowler Sep 27, 2016
b6abeef
Added 33k3bps for 8MHz and 16MHz
coryjfowler Oct 4, 2016
5f8b598
Added 33k3bps for 8MHz and 16MHz
coryjfowler Oct 4, 2016
59e64f5
Added 33k3bps for 8MHz and 16MHz
coryjfowler Oct 4, 2016
9a4432f
Create CAN_loopback
coryjfowler Oct 6, 2016
ade38c1
Rename examples/CAN_loopback/CAN_loopback.ino to examples/CAN_loop/CA…
coryjfowler Oct 6, 2016
50c7559
Rename examples/CAN_loop/CAN_loopback.ino to examples/CAN_loopback/CA…
coryjfowler Oct 6, 2016
e76fe10
Uninitialized local variable ext
ichrysou Jan 17, 2017
0534a1e
Merge pull request #9 from ichrysou/patch-1
coryjfowler Jan 27, 2017
870c43e
Help and support info added
coryjfowler Mar 24, 2017
edb9d68
removed delays and rearranged chipselect init
coryjfowler Jul 11, 2017
cbcad4a
Keep serial print strings in flash
ZakKemble Aug 2, 2017
9139696
Improved sleep functionality
ZakKemble Aug 2, 2017
75d1231
Fixed an issue with sleeping when not connected to a bus
ZakKemble Aug 3, 2017
c5e512f
Added SPI Trans, GPIO, TX Abort
coryjfowler Sep 25, 2017
5c76961
Added SPI Trans, GPIO, TX Abort
coryjfowler Sep 25, 2017
a1d818d
Added SPI Trans, GPIO, TX Abort
coryjfowler Sep 25, 2017
4318f13
Added SPI Trans, GPIO, TX Abort
coryjfowler Sep 25, 2017
7008cba
Added SPI Trans, GPIO, TX Abort
coryjfowler Sep 25, 2017
ece730c
Modified CNF definition values
coryjfowler Oct 10, 2017
0fb5064
Mask & Filter Bug
coryjfowler Jan 12, 2018
dfd5449
Merge branch 'master' into master
coryjfowler Aug 15, 2018
280d6be
Update README.md
coryjfowler Sep 14, 2018
6485fdf
Use correct field separator in keywords.txt
per1234 May 30, 2019
2d6c458
Merge pull request #15 from per1234/keywords-separator
coryjfowler May 30, 2019
848dcc6
Update keywords.txt
coryjfowler Jul 17, 2019
85fc956
Update Dual_CAN.ino
Aug 5, 2019
9a9f9a4
Update Dual_CAN.ino
Aug 5, 2019
8a47a10
Merge pull request #1 from ilium007/ilium007-patch-1
Aug 5, 2019
7bf0431
Created OBD_Sim Example
coryjfowler Dec 11, 2019
c538b54
OBD2 PID Request
coryjfowler Nov 3, 2020
43dc655
Merge pull request #10 from zkemble/master
coryjfowler Mar 17, 2021
75e940c
Guard debug mode definition
VictorMirandaEE May 27, 2021
f657327
Fixed small spelling mistakes.
slav-at-attachix Dec 10, 2021
3d01d98
Review recommendations.
slav-at-attachix Dec 10, 2021
f44efa6
Merge pull request #24 from slaff/fix/spelling
coryjfowler Dec 10, 2021
00b536a
Manual spelling fixes.
slav-at-attachix Dec 10, 2021
1b7b722
Allow clkout to be selected in CNF3 register.
consp Dec 15, 2021
53fd798
Merge pull request #26 from consp/allow-clkout
coryjfowler Dec 15, 2021
42dd1e8
Merge pull request #25 from slaff/fix/spelling-manual-fixes
coryjfowler Dec 15, 2021
846bc60
Merge pull request #22 from VictorMirandaEE/master
coryjfowler Dec 15, 2021
5439f06
Create library.properties
coryjfowler Dec 15, 2021
074f7f4
Added missing license
coryjfowler Dec 16, 2021
f9df8f8
Update README.md
coryjfowler Aug 5, 2022
acc5eac
Changing Send CAN timeout
coryjfowler Aug 5, 2022
f0de98f
Changing Send CAN timeout
coryjfowler Aug 5, 2022
2eca0ac
SPI-Bus selectable in constructor
bitconnector Oct 12, 2022
c4b6fc0
Update mcp_can.cpp
coryjfowler Nov 24, 2022
4142e36
Update mcp_can.cpp
coryjfowler Nov 24, 2022
ce8c4b0
Merge pull request #27 from bitconnector/master
coryjfowler Nov 24, 2022
76e3e74
Merge pull request #17 from ilium007/development
coryjfowler Mar 19, 2023
c7ed2ef
Update mcp_can.cpp
coryjfowler Mar 19, 2023
4c50323
Update mcp_can.h
coryjfowler Mar 19, 2023
6f1f30c
Update library.properties
coryjfowler Jun 25, 2023
9235fa5
- fix clearMsg so buffer is actually cleard
Oct 19, 2023
f97ed43
Merge pull request #30 from domonoky/master
coryjfowler Aug 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added MCP2515Calc.xlsx
Binary file not shown.
40 changes: 33 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,41 @@
MCP2515 Library for Arduino
MCP_CAN Library for Arduino
==============
MCP2515 library v1.1
This library is compatible with any shield or CAN interface that uses the MCP2515 CAN protocol controller.
Baudrates 5k, 10k, 20k, 50k, 100k, 125k, 250k, 500k, & 1000k are confirmed to work using a Peak-System PCAN-USB dongle as a reference.
MCP_CAN library v1.5
This library is compatible with any shield or board that uses the MCP2515 or MCP25625 CAN protocol controller.

This version supports setting the ID filter mode of the protocol controller, the BAUD rate with clock speed with the begin() function. Baudrates 5k, 10k, 20k, 50k, 100k, 125k, 250k, 500k, & 1000k using 16MHz clock on the MCP2515 are confirmed to work using a Peak-System PCAN-USB dongle as a reference. Baudrates for 8MHz and 20MHz crystals are yet to be confirmed but were calculated appropriately.

**The readMsgBuf() functions bring in the message ID. The getCanId() function is obsolete and no longer exists, don't use it.**

The readMsgBuf(*ID, *DLC, *DATA) function will return the ID type (extended or standard) and it will bring back the remote request status bit.
If the ID AND 0x80000000 EQUALS 0x80000000, the ID is of the Extended type, otherwise it is standard.
If the ID AND 0x40000000 EQUALS 0x40000000, the message is a remote request.

The readMsgBuf(*ID, *EXT, *DLC, *DATA) function will return the ID unaltered and doesn't inform us of a remote request.
If EXT is true, the ID is extended.

The sendMsgBuf(ID, DLC, DATA) function can send extended or standard IDs.
To mark an ID as extended, OR the ID with 0x80000000.
To send a remote request, OR the ID with 0x40000000.

The sendMsgBuf(ID, EXT, DLC, DATA) has not changed other than fixing return values.

Using the setMode() function the sketch can now put the protocol controller into sleep, loop-back, or listen-only modes as well as normal operation. Right now the code defaults to loop-back mode after the begin() function runs. I have found this to increase the stability of filtering when the controller is initialized while connected to an active bus.

User can enable and disable (default) One-Shot transmission mode from the sketch using enOneShotTX() or disOneShotTX() respectively.

To wake up from CAN bus activity while in sleep mode enable the wake up interrupt with setSleepWakeup(1). Passing 0 will disable the wakeup interrupt (default).

Installation
==============
Copy this into your "[...]/MySketches/libraries/" folder and restart the Arduino editor.
Copy this into the "[.../MySketches/]libraries/" folder and restart the Arduino editor.

NOTE: If an older version of the library exists (e.g. CAN_BUS_Shield) be sure to remove it from the libraries folder or replace the files with those in this library to avoid conflicts.

NOTE: If you have an older version of the library (CAN_BUS_Shield) be sure to remove
it from the libraries folder or replace the files with those in this library to avoid conflicts.

Help and Support
==============
This is primarily for non-bug related issues: Please start a *new thread* in an appropriate area at Seeedstudio forums or Arduino.cc forums and then send me (coryjfowler) a link through the PM system, my user name is the same as it is here. I will receive an email about the PM. Keep in mind, I do this in my spare time.


*Happy Coding!*
88 changes: 88 additions & 0 deletions examples/CAN_loopback/CAN_loopback.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/* CAN Loopback Example
* This example sends a message once a second and receives that message
* no CAN bus is required. This example will test the functionality
* of the protocol controller, and connections to it.
*
* Written By: Cory J. Fowler - October 5th 2016
*/

#include <mcp_can.h>
#include <SPI.h>

// CAN TX Variables
unsigned long prevTX = 0; // Variable to store last execution time
const unsigned int invlTX = 1000; // One second interval constant
byte data[] = {0xAA, 0x55, 0x01, 0x10, 0xFF, 0x12, 0x34, 0x56}; // Generic CAN data to send

// CAN RX Variables
long unsigned int rxId;
unsigned char len;
unsigned char rxBuf[8];

// Serial Output String Buffer
char msgString[128];

// CAN0 INT and CS
#define CAN0_INT 2 // Set INT to pin 2
MCP_CAN CAN0(10); // Set CS to pin 10


void setup()
{
Serial.begin(115200); // CAN is running at 500,000BPS; 115,200BPS is SLOW, not FAST, thus 9600 is crippling.

// Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK)
Serial.println("MCP2515 Initialized Successfully!");
else
Serial.println("Error Initializing MCP2515...");

// Since we do not set NORMAL mode, we are in loopback mode by default.
//CAN0.setMode(MCP_NORMAL);

pinMode(CAN0_INT, INPUT); // Configuring pin for /INT input

Serial.println("MCP2515 Library Loopback Example...");
}

void loop()
{
if(!digitalRead(CAN0_INT)) // If CAN0_INT pin is low, read receive buffer
{
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)

if((rxId & 0x80000000) == 0x80000000) // Determine if ID is standard (11 bits) or extended (29 bits)
sprintf(msgString, "Extended ID: 0x%.8lX DLC: %1d Data:", (rxId & 0x1FFFFFFF), len);
else
sprintf(msgString, "Standard ID: 0x%.3lX DLC: %1d Data:", rxId, len);

Serial.print(msgString);

if((rxId & 0x40000000) == 0x40000000){ // Determine if message is a remote request frame.
sprintf(msgString, " REMOTE REQUEST FRAME");
Serial.print(msgString);
} else {
for(byte i = 0; i<len; i++){
sprintf(msgString, " 0x%.2X", rxBuf[i]);
Serial.print(msgString);
}
}

Serial.println();
}

if(millis() - prevTX >= invlTX){ // Send this at a one second interval.
prevTX = millis();
byte sndStat = CAN0.sendMsgBuf(0x100, 8, data);

if(sndStat == CAN_OK)
Serial.println("Message Sent Successfully!");
else
Serial.println("Error Sending Message...");

}
}

/*********************************************************************************************************
END FILE
*********************************************************************************************************/
62 changes: 62 additions & 0 deletions examples/CAN_receive/CAN_receive.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// CAN Receive Example
//

#include <mcp_can.h>
#include <SPI.h>

long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
char msgString[128]; // Array to store serial string

#define CAN0_INT 2 // Set INT to pin 2
MCP_CAN CAN0(10); // Set CS to pin 10


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

// Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK)
Serial.println("MCP2515 Initialized Successfully!");
else
Serial.println("Error Initializing MCP2515...");

CAN0.setMode(MCP_NORMAL); // Set operation mode to normal so the MCP2515 sends acks to received data.

pinMode(CAN0_INT, INPUT); // Configuring pin for /INT input

Serial.println("MCP2515 Library Receive Example...");
}

void loop()
{
if(!digitalRead(CAN0_INT)) // If CAN0_INT pin is low, read receive buffer
{
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)

if((rxId & 0x80000000) == 0x80000000) // Determine if ID is standard (11 bits) or extended (29 bits)
sprintf(msgString, "Extended ID: 0x%.8lX DLC: %1d Data:", (rxId & 0x1FFFFFFF), len);
else
sprintf(msgString, "Standard ID: 0x%.3lX DLC: %1d Data:", rxId, len);

Serial.print(msgString);

if((rxId & 0x40000000) == 0x40000000){ // Determine if message is a remote request frame.
sprintf(msgString, " REMOTE REQUEST FRAME");
Serial.print(msgString);
} else {
for(byte i = 0; i<len; i++){
sprintf(msgString, " 0x%.2X", rxBuf[i]);
Serial.print(msgString);
}
}

Serial.println();
}
}

/*********************************************************************************************************
END FILE
*********************************************************************************************************/
36 changes: 36 additions & 0 deletions examples/CAN_send/CAN_send.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// CAN Send Example
//

#include <mcp_can.h>
#include <SPI.h>

MCP_CAN CAN0(10); // Set CS to pin 10

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

// Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK) Serial.println("MCP2515 Initialized Successfully!");
else Serial.println("Error Initializing MCP2515...");

CAN0.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted
}

byte data[8] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};

void loop()
{
// send data: ID = 0x100, Standard CAN Frame, Data length = 8 bytes, 'data' = array of data bytes to send
byte sndStat = CAN0.sendMsgBuf(0x100, 0, 8, data);
if(sndStat == CAN_OK){
Serial.println("Message Sent Successfully!");
} else {
Serial.println("Error Sending Message...");
}
delay(100); // send data per 100ms
}

/*********************************************************************************************************
END FILE
*********************************************************************************************************/
107 changes: 107 additions & 0 deletions examples/CAN_sleep/CAN_sleep.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// CAN Sleep Example
// By Zak Kemble, based on the CAN receive example

// If you only have 2 devices on the CAN bus (the device running this sketch and some other device sending messages), then you may find that duplicate messages are received when waking up.
// This is because when the MCP2515 wakes up it enters LISTENONLY mode where it does not send ACKs to messages, so the transmitter will retransmit the same message a few times.

#include <mcp_can.h>
#include <SPI.h>
#include <avr/sleep.h>

#define CAN0_INT 2 // Set INT to pin 2
MCP_CAN CAN0(10); // Set CS to pin 10

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

// Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK)
Serial.println(F("MCP2515 Initialized Successfully!"));
else
Serial.println(F("Error Initializing MCP2515..."));

CAN0.setMode(MCP_NORMAL); // Set operation mode to normal so the MCP2515 sends acks to received data.
CAN0.setSleepWakeup(1); // Enable wake up interrupt when in sleep mode

pinMode(CAN0_INT, INPUT); // Configuring pin for /INT input

// Enable interrupts for the CAN0_INT pin (should be pin 2 or 3 for Uno and other ATmega328P based boards)
attachInterrupt(digitalPinToInterrupt(CAN0_INT), ISR_CAN, FALLING);

set_sleep_mode(SLEEP_MODE_PWR_DOWN);

Serial.println(F("MCP2515 Library Sleep Example..."));
}

void loop()
{
if(!digitalRead(CAN0_INT)) // If CAN0_INT pin is low, read receive buffer
{
unsigned long rxId;
byte len;
byte rxBuf[8];

if(CAN0.readMsgBuf(&rxId, &len, rxBuf) == CAN_OK) // Read data: len = data length, buf = data byte(s)
{
char msgString[128]; // Array to store serial string
if(rxId & CAN_IS_EXTENDED) // Determine if ID is standard (11 bits) or extended (29 bits)
sprintf_P(msgString, PSTR("Extended ID: 0x%.8lX DLC: %1d Data:"), (rxId & CAN_EXTENDED_ID), len);
else
sprintf_P(msgString, PSTR("Standard ID: 0x%.3lX DLC: %1d Data:"), rxId, len);

Serial.print(msgString);

if(rxId & CAN_IS_REMOTE_REQUEST) // Determine if message is a remote request frame.
Serial.print(F(" REMOTE REQUEST FRAME"));
else
{
for(byte i=0;i<len;i++)
{
sprintf_P(msgString, PSTR(" 0x%.2X"), rxBuf[i]);
Serial.print(msgString);
}
}

Serial.println();
}
else
Serial.println(F("Interrupt is asserted, but there were no messages to process..."));
}

Serial.println(F("SLEEPING..."));

// Put MCP2515 into sleep mode
// This can sometimes take up to around 100ms depending on what the chip is currently doing
CAN0.setMode(MCP_SLEEP);

Serial.println(F("SLEEP"));

// Clear serial buffers before sleeping
Serial.flush();

// Put the microcontroller to sleep
cli(); // Disable interrupts
if(digitalRead(CAN0_INT)) // Make sure we haven't missed an interrupt between the digitalRead() above and now. If an interrupt happens between now and sei()/sleep_cpu() then sleep_cpu() will immediately wake up again
{
sleep_enable();
sleep_bod_disable();
sei();
sleep_cpu();
sleep_disable();
}
sei();

Serial.println(F("WAKE"));

CAN0.setMode(MCP_NORMAL); // When the MCP2515 wakes up it will be in LISTENONLY mode, here we put it into NORMAL mode
}

static void ISR_CAN()
{
// We don't do anything here, this is just for waking up the microcontroller
}

/*********************************************************************************************************
END FILE
*********************************************************************************************************/
Loading