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+ }
0 commit comments