Skip to content

Commit 31438c4

Browse files
committed
add send and receive extended frames examples
1 parent ad48756 commit 31438c4

File tree

2 files changed

+217
-0
lines changed

2 files changed

+217
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
// This is an example of receiving both standard frames and extended frames simultaneously.
2+
// I have used two arrays, STANDARD_CAN_IDS and EXTENDED_CAN_IDS, to receive CAN messages with specific IDs.
3+
// You need to set filters and masks for these IDs,
4+
// and make sure to refer to and coordinate with the example for sending extended frames
5+
6+
7+
#include <SPI.h>
8+
9+
#define CAN_2515
10+
// #define CAN_2518FD
11+
12+
// Set SPI CS Pin according to your hardware
13+
14+
#if defined(SEEED_WIO_TERMINAL) && defined(CAN_2518FD)
15+
// For Wio Terminal w/ MCP2518FD RPi Hat:
16+
// Channel 0 SPI_CS Pin: BCM 8
17+
// Channel 1 SPI_CS Pin: BCM 7
18+
// Interupt Pin: BCM25
19+
const int SPI_CS_PIN = BCM8;
20+
const int CAN_INT_PIN = BCM25;
21+
#else
22+
23+
// For Arduino MCP2515 Hat:
24+
// the cs pin of the version after v1.1 is default to D9
25+
// v0.9b and v1.0 is default D10
26+
const int SPI_CS_PIN = 9;
27+
const int CAN_INT_PIN = 2;
28+
#endif
29+
30+
31+
#ifdef CAN_2518FD
32+
#include "mcp2518fd_can.h"
33+
mcp2518fd CAN(SPI_CS_PIN); // Set CS pin
34+
#endif
35+
36+
#ifdef CAN_2515
37+
#include "mcp2515_can.h"
38+
mcp2515_can CAN(SPI_CS_PIN); // Set CS pin
39+
#endif
40+
41+
// Define the list of standard CAN IDs to receive
42+
const unsigned long STANDARD_CAN_IDS[] = {0x123, 0x456};
43+
const int STANDARD_CAN_ID_COUNT = sizeof(STANDARD_CAN_IDS) / sizeof(STANDARD_CAN_IDS[0]);
44+
45+
// Define the list of extended CAN IDs to receive
46+
const unsigned long EXTENDED_CAN_IDS[] = {0x1234567, 0x89ABCDE};
47+
const int EXTENDED_CAN_ID_COUNT = sizeof(EXTENDED_CAN_IDS) / sizeof(EXTENDED_CAN_IDS[0]);
48+
49+
void setup()
50+
{
51+
SERIAL_PORT_MONITOR.begin(115200);
52+
while(!Serial){};
53+
54+
while (CAN_OK != CAN.begin(CAN_250KBPS))
55+
{ // init can bus : baudrate = 250k
56+
SERIAL_PORT_MONITOR.println("CAN init fail, retry...");
57+
delay(100);
58+
}
59+
SERIAL_PORT_MONITOR.println("CAN init ok!");
60+
61+
// Configure the standard frame filters
62+
for (int i = 0; i < STANDARD_CAN_ID_COUNT; i++)
63+
{
64+
CAN.init_Mask(i, 0, 0x7FF); // Check all 11 bits of the standard frame
65+
CAN.init_Filt(i, 0, STANDARD_CAN_IDS[i]);
66+
}
67+
68+
// Configure the extended frame filters
69+
for (int i = 0; i < EXTENDED_CAN_ID_COUNT; i++)
70+
{
71+
CAN.init_Mask(i + STANDARD_CAN_ID_COUNT, 1, 0x1FFFFFFF); // Check all 29 bits of the extended frame
72+
CAN.init_Filt(i + STANDARD_CAN_ID_COUNT, 1, EXTENDED_CAN_IDS[i]);
73+
}
74+
}
75+
76+
void loop()
77+
{
78+
unsigned char len = 0;
79+
unsigned char buf[8];
80+
if (CAN_MSGAVAIL == CAN.checkReceive())
81+
{ // check if data coming
82+
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
83+
84+
unsigned long canId = CAN.getCanId();
85+
86+
// Check if it is a standard CAN ID
87+
for (int i = 0; i < STANDARD_CAN_ID_COUNT; i++)
88+
{
89+
if (canId == STANDARD_CAN_IDS[i])
90+
{
91+
SERIAL_PORT_MONITOR.println("-----------------------------");
92+
SERIAL_PORT_MONITOR.print("Get standard data from ID: 0x");
93+
SERIAL_PORT_MONITOR.println(canId, HEX);
94+
95+
for (int j = 0; j < len; j++)
96+
{ // print the data
97+
SERIAL_PORT_MONITOR.print(buf[j], HEX);
98+
SERIAL_PORT_MONITOR.print("\t");
99+
}
100+
SERIAL_PORT_MONITOR.println();
101+
break;
102+
}
103+
}
104+
105+
// Check if it is an extended CAN ID
106+
for (int i = 0; i < EXTENDED_CAN_ID_COUNT; i++)
107+
{
108+
if (canId == EXTENDED_CAN_IDS[i])
109+
{
110+
SERIAL_PORT_MONITOR.println("-----------------------------");
111+
SERIAL_PORT_MONITOR.print("Get extended data from ID: 0x");
112+
SERIAL_PORT_MONITOR.println(canId, HEX);
113+
114+
for (int j = 0; j < len; j++)
115+
{ // print the data
116+
SERIAL_PORT_MONITOR.print(buf[j], HEX);
117+
SERIAL_PORT_MONITOR.print("\t");
118+
}
119+
SERIAL_PORT_MONITOR.println();
120+
break;
121+
}
122+
}
123+
}
124+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// demo: CAN-BUS Shield, send data
2+
// This is an example of sending both standard frames and extended frames simultaneously.
3+
// I have used two arrays, STANDARD_CAN_IDS and EXTENDED_CAN_IDS, to store the IDs to be sent, and it supports defining custom send IDs
4+
5+
6+
#include <SPI.h>
7+
8+
#define CAN_2515
9+
// #define CAN_2518FD
10+
11+
// Set SPI CS Pin according to your hardware
12+
13+
#if defined(SEEED_WIO_TERMINAL) && defined(CAN_2518FD)
14+
// For Wio Terminal w/ MCP2518FD RPi Hat:
15+
// Channel 0 SPI_CS Pin: BCM 8
16+
// Channel 1 SPI_CS Pin: BCM 7
17+
// Interupt Pin: BCM25
18+
const int SPI_CS_PIN = BCM8;
19+
const int CAN_INT_PIN = BCM25;
20+
#else
21+
22+
// For Arduino MCP2515 Hat:
23+
// the cs pin of the version after v1.1 is default to D9
24+
// v0.9b and v1.0 is default D10
25+
const int SPI_CS_PIN = 9;
26+
const int CAN_INT_PIN = 2;
27+
#endif
28+
29+
30+
#ifdef CAN_2518FD
31+
#include "mcp2518fd_can.h"
32+
mcp2518fd CAN(SPI_CS_PIN); // Set CS pin
33+
#endif
34+
35+
#ifdef CAN_2515
36+
#include "mcp2515_can.h"
37+
mcp2515_can CAN(SPI_CS_PIN); // Set CS pin
38+
#endif
39+
40+
// Define the list of standard CAN IDs to receive
41+
const unsigned long STANDARD_CAN_IDS[] = {0x123, 0x456};
42+
const int STANDARD_CAN_ID_COUNT = sizeof(STANDARD_CAN_IDS) / sizeof(STANDARD_CAN_IDS[0]);
43+
44+
// Define the list of extended CAN IDs to receive
45+
const unsigned long EXTENDED_CAN_IDS[] = {0x1234567, 0x89ABCDE};
46+
const int EXTENDED_CAN_ID_COUNT = sizeof(EXTENDED_CAN_IDS) / sizeof(EXTENDED_CAN_IDS[0]);
47+
48+
void setup()
49+
{
50+
SERIAL_PORT_MONITOR.begin(115200);
51+
while(!Serial){};
52+
53+
while (CAN_OK != CAN.begin(CAN_500KBPS))
54+
{ // init can bus : baudrate = 500k
55+
SERIAL_PORT_MONITOR.println("CAN init fail, retry...");
56+
delay(100);
57+
}
58+
SERIAL_PORT_MONITOR.println("CAN init ok!");
59+
}
60+
61+
unsigned char stmp[8] = {0, 0, 0, 0, 0, 0, 0, 0};
62+
void loop()
63+
{
64+
// send data: id = 0x00, standrad frame, data len = 8, stmp: data buf
65+
stmp[7] = stmp[7] + 1;
66+
if (stmp[7] == 100)
67+
{
68+
stmp[7] = 0;
69+
stmp[6] = stmp[6] + 1;
70+
71+
if (stmp[6] == 100)
72+
{
73+
stmp[6] = 0;
74+
stmp[5] = stmp[5] + 1;
75+
}
76+
}
77+
78+
for (int i = 0; i < STANDARD_CAN_ID_COUNT; i++)
79+
{
80+
CAN.sendMsgBuf(STANDARD_CAN_IDS[i], 0, 8, stmp);//using ID List ID
81+
delay(100);
82+
}
83+
84+
for (int i = 0; i < EXTENDED_CAN_ID_COUNT; i++)
85+
{
86+
CAN.sendMsgBuf(EXTENDED_CAN_IDS[i], 1, 8, stmp);//using ID List ID
87+
delay(100);
88+
}
89+
}
90+
91+
/*********************************************************************************************************
92+
END FILE
93+
*********************************************************************************************************/

0 commit comments

Comments
 (0)