-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Self send and receive + FD examples
- Loading branch information
1 parent
c11356b
commit 8c8500a
Showing
2 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
This is an example of sending and receiving on its own, connect two channels to each other using jumpers. | ||
*/ | ||
|
||
#include <SPI.h> | ||
#include "mcp2518fd_can.h" | ||
|
||
/*SAMD core*/ | ||
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE | ||
#define SERIAL SerialUSB | ||
#else | ||
#define SERIAL Serial | ||
#endif | ||
|
||
// Set SPI CS Pin according to your hardware | ||
// For Wio Terminal w/ MCP2518FD RPi Hat: | ||
// Channel 0 SPI_CS Pin: BCM 8 | ||
// Channel 1 SPI_CS Pin: BCM 7 | ||
// Interupt Pin: BCM25 | ||
// ***************************************** | ||
// For Arduino MCP2515 Hat: | ||
// SPI_CS Pin: D9 | ||
|
||
const int SPI_CS_PIN_SEND = 9; | ||
const int SPI_CS_PIN_RECEIVE = 10; | ||
|
||
mcp2518fd CAN_SEND(SPI_CS_PIN_SEND); | ||
mcp2518fd CAN_RECEIVE(SPI_CS_PIN_RECEIVE); | ||
|
||
unsigned char len = 0; | ||
unsigned char buf[8]; | ||
|
||
void setup() { | ||
SERIAL.begin(115200); | ||
while(!Serial); // wait for Serial | ||
|
||
if (CAN_SEND.begin((byte)CAN_500K_1M) != 0 || CAN_RECEIVE.begin((byte)CAN_500K_1M) != 0) { | ||
Serial.println("CAN-BUS initiliased error!"); | ||
while(1); | ||
} | ||
|
||
SERIAL.println("CAN BUS Shield init ok!"); | ||
} | ||
|
||
unsigned char stmp[8] = {0, 0, 0, 0, 0, 0, 0, 0}; | ||
void loop() { | ||
// send data: id = 0x00, standrad frame, data len = 8, stmp: data buf | ||
stmp[7] = stmp[7] + 1; | ||
if (stmp[7] == 100) { | ||
stmp[7] = 0; | ||
stmp[6] = stmp[6] + 1; | ||
|
||
if (stmp[6] == 100) { | ||
stmp[6] = 0; | ||
stmp[5] = stmp[6] + 1; | ||
} | ||
} | ||
|
||
CAN_SEND.sendMsgBuf(0x00, 0, 8, stmp); | ||
delay(100); // send data per 100ms | ||
SERIAL.println("CAN BUS sendMsgBuf ok!"); | ||
|
||
// --------------------- | ||
|
||
if (CAN_MSGAVAIL == CAN_RECEIVE.checkReceive()) { | ||
// read data, len: data length, buf: data buf | ||
SERIAL.println("checkReceive"); | ||
CAN_RECEIVE.readMsgBuf(&len, buf); | ||
// print the data | ||
for (int i = 0; i < len; i++) { | ||
SERIAL.print(buf[i]); SERIAL.print(" "); | ||
} | ||
SERIAL.println(); | ||
} | ||
SERIAL.println("---LOOP END---"); | ||
} | ||
|
||
// END FILE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
This is an example of sending and receiving (FD) on its own, connect two channels to each other using jumpers. | ||
*/ | ||
|
||
#include <SPI.h> | ||
#include "mcp2518fd_can.h" | ||
|
||
/*SAMD core*/ | ||
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE | ||
#define SERIAL SerialUSB | ||
#else | ||
#define SERIAL Serial | ||
#endif | ||
|
||
// Set SPI CS Pin according to your hardware | ||
// For Wio Terminal w/ MCP2518FD RPi Hat: | ||
// Channel 0 SPI_CS Pin: BCM 8 | ||
// Channel 1 SPI_CS Pin: BCM 7 | ||
// Interupt Pin: BCM25 | ||
// ***************************************** | ||
// For Arduino MCP2515 Hat: | ||
// SPI_CS Pin: D9 | ||
|
||
const int SPI_CS_PIN_SEND = 9; | ||
const int SPI_CS_PIN_RECEIVE = 2; | ||
|
||
mcp2518fd CAN_SEND(SPI_CS_PIN_SEND); | ||
mcp2518fd CAN_RECEIVE(SPI_CS_PIN_RECEIVE); | ||
|
||
void setup() { | ||
SERIAL.begin(115200); | ||
while(!Serial); // wait for Serial | ||
CAN_SEND.setMode(0); | ||
CAN_RECEIVE.setMode(0); | ||
|
||
if (CAN_SEND.begin((byte)CAN_500K_1M) != 0 || CAN_RECEIVE.begin((byte)CAN_500K_1M) != 0) { | ||
Serial.println("CAN-BUS initiliased error!"); | ||
while(1); | ||
} | ||
byte send_mode = CAN_SEND.getMode(); | ||
byte receive_mode = CAN_RECEIVE.getMode(); | ||
SERIAL.print("CAN BUS Send Mode = "); SERIAL.println(send_mode); | ||
SERIAL.print("CAN BUS Receive Mode = "); SERIAL.println(receive_mode); | ||
SERIAL.println("CAN BUS Shield init ok!"); | ||
} | ||
|
||
unsigned char stmp[64] = {0}; | ||
unsigned char len = 0; | ||
unsigned char buf[64]; | ||
|
||
void loop() { | ||
stmp[63] = stmp[63] + 1; | ||
if (stmp[63] == 100) { | ||
stmp[63] = 0; | ||
stmp[62] = stmp[62] + 1; | ||
|
||
if (stmp[62] == 100) { | ||
stmp[62] = 0; | ||
stmp[61] = stmp[62] + 1; | ||
} | ||
} | ||
|
||
CAN_SEND.sendMsgBuf(0x00, 0, 15, stmp); | ||
delay(100); // send data per 100ms | ||
SERIAL.println("CAN BUS sendMsgBuf ok!"); | ||
|
||
// --------------------- | ||
|
||
if (CAN_MSGAVAIL == CAN_RECEIVE.checkReceive()) { | ||
// read data, len: data length, buf: data buf | ||
SERIAL.println("checkReceive"); | ||
CAN_RECEIVE.readMsgBuf(&len, buf); | ||
// print the data | ||
for (int i = 0; i < len; i++) { | ||
SERIAL.print(buf[i]); SERIAL.print(""); | ||
} | ||
SERIAL.println(); | ||
} | ||
SERIAL.println("---LOOP END---"); | ||
} | ||
|
||
// END FILE |