mirror of
https://github.com/Seeed-Studio/Seeed_Arduino_CAN.git
synced 2026-07-28 04:06:45 +00:00
update examples
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
CAN BUS Shield
|
||||
---------------------------------------------------------
|
||||
[](http://www.seeedstudio.com/depot/wifi-bee-v20-p-1637.html)
|
||||
[](http://www.seeedstudio.com/depot/wifi-bee-v20-p-1637.html)
|
||||
|
||||
|
||||
|
||||
@@ -138,7 +138,7 @@ For more information, please refer to [wiki page](http://www.seeedstudio.com/wik
|
||||
|
||||
----
|
||||
|
||||
This software is written by loovee ([luweicong@seeedstudio.com](luweicong@seeedstudio.com "luweicong@seeedstudio.com")) for seeed studio<br>
|
||||
This software is written by loovee ([luweicong@seeed.cc](luweicong@seeed.cc "luweicong@seeed.cc")) for seeed studio<br>
|
||||
and is licensed under [The MIT License](http://opensource.org/licenses/mit-license.php). Check License.txt for more information.<br>
|
||||
|
||||
Contributing to this software is warmly welcomed. You can do this basically by<br>
|
||||
|
||||
@@ -7,13 +7,11 @@
|
||||
#include "mcp_can.h"
|
||||
|
||||
|
||||
unsigned char Flag_Recv = 0;
|
||||
unsigned char len = 0;
|
||||
unsigned char buf[8];
|
||||
char str[20];
|
||||
// the cs pin of the version after v1.1 is default to D9
|
||||
// v0.9b and v1.0 is default D10
|
||||
const int SPI_CS_PIN = 9;
|
||||
|
||||
|
||||
MCP_CAN CAN(10); // Set CS to pin 10
|
||||
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
|
||||
|
||||
void setup()
|
||||
{
|
||||
@@ -37,6 +35,9 @@ START_INIT:
|
||||
|
||||
void loop()
|
||||
{
|
||||
unsigned char len = 0;
|
||||
unsigned char buf[8];
|
||||
|
||||
if(CAN_MSGAVAIL == CAN.checkReceive()) // check if data coming
|
||||
{
|
||||
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
|
||||
|
||||
@@ -5,10 +5,14 @@
|
||||
#include <SPI.h>
|
||||
#include "mcp_can.h"
|
||||
|
||||
MCP_CAN CAN(10); // Set CS to pin 10
|
||||
// the cs pin of the version after v1.1 is default to D9
|
||||
// v0.9b and v1.0 is default D10
|
||||
const int SPI_CS_PIN = 9;
|
||||
|
||||
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
|
||||
|
||||
|
||||
unsigned char Flag_Recv = 0;
|
||||
unsigned char flagRecv = 0;
|
||||
unsigned char len = 0;
|
||||
unsigned char buf[8];
|
||||
char str[20];
|
||||
@@ -17,7 +21,7 @@ void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
START_INIT:
|
||||
START_INIT:
|
||||
|
||||
if(CAN_OK == CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
|
||||
{
|
||||
@@ -30,36 +34,39 @@ START_INIT:
|
||||
delay(100);
|
||||
goto START_INIT;
|
||||
}
|
||||
|
||||
|
||||
attachInterrupt(0, MCP2515_ISR, FALLING); // start interrupt
|
||||
}
|
||||
|
||||
void MCP2515_ISR()
|
||||
{
|
||||
Flag_Recv = 1;
|
||||
flagRecv = 1;
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
if(Flag_Recv) { // check if get data
|
||||
|
||||
Flag_Recv = 0; // clear flag
|
||||
|
||||
// iterate over all pending messages
|
||||
// If either the bus is saturated or the MCU is busy,
|
||||
// both RX buffers may be in use and reading a single
|
||||
// message does not clear the IRQ conditon.
|
||||
while (CAN_MSGAVAIL == CAN.checkReceive()) {
|
||||
// read data, len: data length, buf: data buf
|
||||
CAN.readMsgBuf(&len, buf);
|
||||
|
||||
// print the data
|
||||
for(int i = 0; i<len; i++) {
|
||||
Serial.print(buf[i]);Serial.print("\t");
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
}
|
||||
if(flagRecv)
|
||||
{ // check if get data
|
||||
|
||||
flagRecv = 0; // clear flag
|
||||
|
||||
// iterate over all pending messages
|
||||
// If either the bus is saturated or the MCU is busy,
|
||||
// both RX buffers may be in use and reading a single
|
||||
// message does not clear the IRQ conditon.
|
||||
while (CAN_MSGAVAIL == CAN.checkReceive())
|
||||
{
|
||||
// read data, len: data length, buf: data buf
|
||||
CAN.readMsgBuf(&len, buf);
|
||||
|
||||
// print the data
|
||||
for(int i = 0; i<len; i++)
|
||||
{
|
||||
Serial.print(buf[i]);Serial.print("\t");
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
|
||||
@@ -2,7 +2,11 @@
|
||||
#include <mcp_can.h>
|
||||
#include <SPI.h>
|
||||
|
||||
MCP_CAN CAN(10); // Set CS to pin 10
|
||||
// the cs pin of the version after v1.1 is default to D9
|
||||
// v0.9b and v1.0 is default D10
|
||||
const int SPI_CS_PIN = 9;
|
||||
|
||||
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
|
||||
|
||||
void setup()
|
||||
{
|
||||
|
||||
@@ -6,10 +6,14 @@
|
||||
#include <SPI.h>
|
||||
#include "mcp_can.h"
|
||||
|
||||
MCP_CAN CAN(9); // Set CS to pin 10
|
||||
// the cs pin of the version after v1.1 is default to D9
|
||||
// v0.9b and v1.0 is default D10
|
||||
const int SPI_CS_PIN = 9;
|
||||
|
||||
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
|
||||
|
||||
|
||||
unsigned char Flag_Recv = 0;
|
||||
unsigned char flagRecv = 0;
|
||||
unsigned char len = 0;
|
||||
unsigned char buf[8];
|
||||
char str[20];
|
||||
@@ -18,7 +22,7 @@ void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
START_INIT:
|
||||
START_INIT:
|
||||
|
||||
if(CAN_OK == CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
|
||||
{
|
||||
@@ -31,23 +35,23 @@ START_INIT:
|
||||
delay(100);
|
||||
goto START_INIT;
|
||||
}
|
||||
|
||||
|
||||
attachInterrupt(0, MCP2515_ISR, FALLING); // start interrupt
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* set mask, set both the mask to 0x3ff
|
||||
*/
|
||||
CAN.init_Mask(0, 0, 0x3ff); // there are 2 mask in mcp2515, you need to set both of them
|
||||
CAN.init_Mask(1, 0, 0x3ff);
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* set filter, we can receive id from 0x04 ~ 0x09
|
||||
*/
|
||||
CAN.init_Filt(0, 0, 0x04); // there are 6 filter in mcp2515
|
||||
CAN.init_Filt(1, 0, 0x05); // there are 6 filter in mcp2515
|
||||
|
||||
|
||||
CAN.init_Filt(2, 0, 0x06); // there are 6 filter in mcp2515
|
||||
CAN.init_Filt(3, 0, 0x07); // there are 6 filter in mcp2515
|
||||
CAN.init_Filt(4, 0, 0x08); // there are 6 filter in mcp2515
|
||||
@@ -57,15 +61,15 @@ START_INIT:
|
||||
|
||||
void MCP2515_ISR()
|
||||
{
|
||||
Flag_Recv = 1;
|
||||
flagRecv = 1;
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
if(Flag_Recv) // check if get data
|
||||
if(flagRecv) // check if get data
|
||||
{
|
||||
|
||||
Flag_Recv = 0; // clear flag
|
||||
|
||||
flagRecv = 0; // clear flag
|
||||
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
|
||||
|
||||
Serial.println("\r\n------------------------------------------------------------------");
|
||||
|
||||
@@ -4,7 +4,11 @@
|
||||
#include <mcp_can.h>
|
||||
#include <SPI.h>
|
||||
|
||||
MCP_CAN CAN(10); // Set CS to pin 10
|
||||
// the cs pin of the version after v1.1 is default to D9
|
||||
// v0.9b and v1.0 is default D10
|
||||
const int SPI_CS_PIN = 9;
|
||||
|
||||
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
|
||||
|
||||
void setup()
|
||||
{
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
Author:Loovee
|
||||
Contributor: Cory J. Fowler
|
||||
2014-1-16
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
|
||||
Reference in New Issue
Block a user