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

Sub development #10

Open
wants to merge 40 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
9a0c458
Work In Progess!
coryjfowler Jan 22, 2014
c2a6a98
Work In Progress!!
coryjfowler Jan 22, 2014
02a9245
Work In Progress!
coryjfowler Jan 22, 2014
5ab7ffc
Work In Progress!!
coryjfowler Jan 22, 2014
c5547a3
Update mcp_can_dfs.h
coryjfowler Jan 22, 2014
4861aee
Update README.md
coryjfowler Jan 22, 2014
e9aab99
Update keywords.txt
coryjfowler Jan 22, 2014
ad87ed0
Update receive.ino
coryjfowler Jan 22, 2014
188144e
Update send.ino
coryjfowler Jan 22, 2014
16aca1e
Update mcp_can.cpp
coryjfowler Jan 22, 2014
2f57078
Update README.md
coryjfowler Jan 22, 2014
997f128
Update mcp_can.cpp
coryjfowler Jan 22, 2014
ab38b31
Update README.md
coryjfowler Jan 23, 2014
284d058
New Example!
coryjfowler Jan 28, 2014
b4bd377
Companion script for example sketch.
coryjfowler Jan 28, 2014
adc5881
Added Dual CAN Example Sketch
coryjfowler Jan 31, 2014
2a9e0bc
Depreciated function, Testing for bug fix.
coryjfowler Jun 4, 2014
806ca5b
Depreciated function
coryjfowler Jun 4, 2014
e3b1de8
Fixed errors...
coryjfowler Jun 16, 2014
93c5312
Update! (Read Description)
coryjfowler Jul 17, 2014
a5c0e5a
Update! (Read Description)
coryjfowler Jul 17, 2014
66eaf32
Mask and Filter Demo for Standard IDs
coryjfowler Jul 17, 2014
4737bc1
Mask and Filter Demo for Extended IDs
coryjfowler Jul 17, 2014
fd4f81b
Rename examples/dual_CAN/dual_CAN.ino to examples/Dual_CAN/Dual_CAN.ino
coryjfowler Jul 17, 2014
47505d8
Update and rename examples/send/send.ino to examples/CAN_send/CAN_sen…
coryjfowler Jul 18, 2014
b3c88aa
Update and rename examples/receive/receive.ino to examples/CAN_receiv…
coryjfowler Jul 18, 2014
831bbcd
Updated, Read Description...
coryjfowler Sep 16, 2014
a9754fd
Updated, Read Description...
coryjfowler Sep 16, 2014
43da2b4
Updated, Read Description...
coryjfowler Sep 16, 2014
92b151a
Updated, Read Description...
coryjfowler Sep 16, 2014
46bc9e0
Updated, Read Description...
coryjfowler Sep 16, 2014
c9452a9
Updated Bit Rate Definitions
coryjfowler Sep 23, 2014
b227e2f
Updated to reflect recent changes.
coryjfowler Sep 23, 2014
3dd9b41
Updated to reflect recent changes.
coryjfowler Sep 23, 2014
8c71eac
Removed references to second person.
coryjfowler Sep 23, 2014
38e1af5
Update README.md
coryjfowler Jan 29, 2015
33666c3
Functions Added
coryjfowler Jun 30, 2016
1ff174b
Added functions
coryjfowler Jun 30, 2016
a00f46f
Update README.md
coryjfowler Jul 1, 2016
48a40c5
Sub-Development Branch NOT Maintained
coryjfowler Mar 24, 2017
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
12 changes: 1 addition & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
MCP2515 Library for Arduino
Sub-Development branch was copied to Master on June 30th, 2016 and is not being maintained and will be deleted soon. The master is current.
==============
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.

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

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.


*Happy Coding!*
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// demo: CAN-BUS Shield, receive data
// CAN Receive Example
//

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

Expand All @@ -12,29 +14,38 @@ MCP_CAN CAN0(10); // Set CS to pin 10
void setup()
{
Serial.begin(115200);
CAN0.begin(CAN_500KBPS); // init can bus : baudrate = 500k

// 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(2, INPUT); // Setting pin 2 for /INT input

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

void loop()
{
if(!digitalRead(2)) // If pin 2 is low, read receive buffer
{
CAN0.readMsgBuf(&len, rxBuf); // Read data: len = data length, buf = data byte(s)
rxId = CAN0.getCanId(); // Get message ID
Serial.print("ID: ");
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)

Serial.print("ID: "); // Print the message ID.
Serial.print(rxId, HEX);

Serial.print(" Data: ");
for(int i = 0; i<len; i++) // Print each byte of the data
for(int i = 0; i<len; i++) // Print each byte of the data.
{
if(rxBuf[i] < 0x10) // If data byte is less than 0x10, add a leading zero
if(rxBuf[i] < 0x10) // If data byte is less than 0x10, add a leading zero.
{
Serial.print("0");
}
Serial.print(rxBuf[i], HEX);
Serial.print(" ");
}

Serial.println();
}
}
Expand Down
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
*********************************************************************************************************/
59 changes: 59 additions & 0 deletions examples/CAN_to_Ethernet/CAN_to_Ethernet.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// CAN to Ethernet
// Jan 28th, 2014
// Written by: Cory J. Fowler

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

// Change these for your network!
byte mac[] = {0x00, 0x55, 0x66, 0xEE, 0xFF, 0xFF};
IPAddress ip(10, 100, 50, 233);
IPAddress gateway(10, 100, 50, 254);
IPAddress dest(10, 100, 50, 210);

unsigned int localPort = 8888;
unsigned int remPort = 54321;

unsigned long rxId;
byte len = 0;
byte rxBuf[8];
char buffer[50];

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

EthernetUDP UDP;
void setup()
{
Serial.begin(115200);
// CAN0.begin(CAN_250KBPS); // init CAN Bus with 250kb/s baudrate
CAN0.begin(MCP_ANY, CAN_250KBPS, MCP_16MHZ); // init CAN Bus with 250kb/s baudrate at 16MHz with Mask & Filters Disabled
CAN0.setMode(MCP_NORMAL); // Set operation mode to normal so the MCP2515 sends acks to received data.
pinMode(2, INPUT); // Setting pin 2, MCP2515 /INT, to input mode
Ethernet.begin(mac,ip); // Initialize Ethernet
UDP.begin(localPort); // Initialize the UDP listen port that is currently unused!

Serial.println("CAN to Ethernet...");
}

void loop()
{
if(!digitalRead(2)) // If pin 2 is low, read receive buffer
{
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read Data: rxID = Message ID, len = Data Length, buf = Data Byte(s)
// CAN0.readMsgBuf(&len, rxBuf); // Read Data: len = Data Length, buf = Data Byte(s)
// rxId = CAN0.getCanId(); // Function will be depreciated soon due to readMsgBuf now returning ID

sprintf(buffer, "ID: %.8lX Data: %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X\n\r",
rxId, rxBuf[0], rxBuf[1], rxBuf[2], rxBuf[3], rxBuf[4], rxBuf[5], rxBuf[6], rxBuf[7]);

UDP.beginPacket(dest, remPort);
UDP.write(buffer);
UDP.endPacket();
}
}

/*********************************************************************************************************
END FILE
*********************************************************************************************************/
19 changes: 19 additions & 0 deletions examples/CAN_to_Ethernet/udp_listen.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/perl -w
#This works on Debian Linux
#I have not tested it on a Windows system running Perl, but it should work if all dependencies are met.
#Also, FIREWALL. <--- I gaurantee that will be the issue.

use IO::Socket;

### Create UDP Listen Socket
my $udpsocket = new IO::Socket::INET (
LocalPort => '54321',
Proto => 'udp',
);
die "Could not create socket: $!\n" unless $udpsocket;

### Data Manipulation and Display
while(1) {
$udpsocket->recv(my $data,512);
print $data;
}
55 changes: 55 additions & 0 deletions examples/Dual_CAN/Dual_CAN.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Demo: Dual CAN-BUS Shields, Data Pass-through
// Written by: Cory J. Fowler
// January 31st 2014
// This examples the ability of this library to support more than one MCP2515 based CAN interface.


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

unsigned long rxId;
byte len;
byte rxBuf[8];

byte txBuf0[] = {AA,55,AA,55,AA,55,AA,55};
byte txBuf1[] = {55,AA,55,AA,55,AA,55,AA};

MCP_CAN CAN0(10); // CAN0 interface usins CS on digital pin 10
MCP_CAN CAN1(9); // CAN1 interface using CS on digital pin 9

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

// init CAN0 bus, baudrate: 250k@16MHz
if(CAN0.begin(MCP_EXT, CAN_250KBPS, MCP_16MHZ) == CAN_OK){
Serial.print("CAN0: Init OK!\r\n");
CAN0.setMode(MCP_NORMAL);
} else Serial.print("CAN0: Init Fail!!!\r\n");

// init CAN1 bus, baudrate: 250k@16MHz
if(CAN1.begin(MCP_EXT, CAN_250KBPS, MCP_16MHZ) == CAN_OK){
Serial.print("CAN1: Init OK!\r\n");
CAN1.setMode(MCP_NORMAL);
} else Serial.print("CAN1: Init Fail!!!\r\n");

SPI.setClockDivider(SPI_CLOCK_DIV2); // Set SPI to run at 8MHz (16MHz / 2 = 8 MHz)

CAN0.sendMsgBuf(0x1000000, 1, 8, tx0Buf);
CAN1.sendMsgBuf(0x1000001, 1, 8, tx1Buf);
}

void loop(){
if(!digitalRead(2)){ // If pin 2 is low, read CAN0 receive buffer
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
CAN1.sendMsgBuf(rxId, 1, len, rxBuf); // Immediately send message out CAN1 interface
}
if(!digitalRead(3)){ // If pin 3 is low, read CAN1 receive buffer
CAN1.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
CAN0.sendMsgBuf(rxId, 1, len, rxBuf); // Immediately send message out CAN0 interface
}
}

/*********************************************************************************************************
END FILE
*********************************************************************************************************/
87 changes: 87 additions & 0 deletions examples/Extended_MaskFilter/Extended_MaskFilter.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@

// MCP2515 Mask and Filter example for extended CAN message frames.
// Written by Cory J. Fowler (20140717)

/***********************************************************************************
If you send the following extended IDs below to an Arduino loaded with this sketch
you will find that 0x00FFCC00 and 0x00FF9900 will not get in.

ID in Hex is the same as the Filter in Hex.
0x00FFEE00
0x00FFDD00
0x00FFCC00 This example will NOT be receiving this ID
0x00FFBB00
0x00FFAA00
0x00FF9900 This example will NOT be receiving this ID
0x00FF8800
0x00FF7700

This mask will check the filters against ID bits 23 through 8.
(Those familiar with J1939 might see why I used this mask.)
MASK = 0x00FFFF00
If there is an explicit filter match to those bits, the message will be passed to the
receive buffer and the interrupt pin will be set.

This example will NOT be exclusive to ONLY the above message IDs, for that a mask such
as the below would be used:
MASK = 0x1FFFFFFF

At the moment, to disable a filter or mask, copy the value of a used filter or mask.

***********************************************************************************/


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

long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];

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

void setup()
{
Serial.begin(115200);
if(CAN0.begin(MCP_STDEXT, CAN_500KBPS, MCP_16MHZ) == CAN_OK) Serial.print("MCP2515 Init Okay!!\r\n");
else Serial.print("MCP2515 Init Failed!!\r\n");
pinMode(2, INPUT); // Setting pin 2 for /INT input

CAN0.init_Mask(0,1,0x00FFFF00); // Init first mask...
CAN0.init_Filt(0,1,0x00FFEE00); // Init first filter...
CAN0.init_Filt(1,1,0x00FFDD00); // Init second filter...

CAN0.init_Mask(1,1,0x00FFFF00); // Init second mask...
CAN0.init_Filt(2,1,0x00FFBB00); // Init third filter...
CAN0.init_Filt(3,1,0x00FFAA00); // Init fouth filter...
CAN0.init_Filt(4,1,0x00FF8800); // Init fifth filter...
CAN0.init_Filt(5,1,0x00FF7700); // Init sixth filter...

Serial.println("MCP2515 Library Mask & Filter Example...");
CAN0.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted
}

void loop()
{
if(!digitalRead(2)) // If pin 2 is low, read receive buffer
{
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
Serial.print("ID: ");
Serial.print(rxId, HEX);
Serial.print(" Data: ");
for(int i = 0; i<len; i++) // Print each byte of the data
{
if(rxBuf[i] < 0x10) // If data byte is less than 0x10, add a leading zero
{
Serial.print("0");
}
Serial.print(rxBuf[i], HEX);
Serial.print(" ");
}
Serial.println();
}
}

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