forked from SEAME-pt/SEAME-Course-24-25
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c01bed
commit 4d25954
Showing
1 changed file
with
80 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,80 @@ | ||
// demo: CAN-BUS Shield, receive data with check mode | ||
// send data coming to fast, such as less than 10ms, you can use this way | ||
// loovee, 2014-6-13 | ||
#include <SPI.h> | ||
|
||
#define CAN_2515 | ||
// #define CAN_2518FD | ||
|
||
// Set SPI CS Pin according to your hardware | ||
|
||
#if defined(SEEED_WIO_TERMINAL) && defined(CAN_2518FD) | ||
// For Wio Terminal w/ MCP2518FD RPi Hat: | ||
// Channel 0 SPI_CS Pin: BCM 8 | ||
// Channel 1 SPI_CS Pin: BCM 7 | ||
// Interupt Pin: BCM25 | ||
const int SPI_CS_PIN = BCM8; | ||
const int CAN_INT_PIN = BCM25; | ||
#else | ||
|
||
// For Arduino MCP2515 Hat: | ||
// 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; | ||
const int CAN_INT_PIN = 2; | ||
#endif | ||
|
||
|
||
#ifdef CAN_2518FD | ||
#include "mcp2518fd_can.h" | ||
mcp2518fd CAN(SPI_CS_PIN); // Set CS pin | ||
#endif | ||
|
||
#ifdef CAN_2515 | ||
#include "mcp2515_can.h" | ||
mcp2515_can CAN(SPI_CS_PIN); // Set CS pin | ||
#endif | ||
|
||
void setup() { | ||
SERIAL_PORT_MONITOR.begin(9600); | ||
|
||
while (CAN_OK != CAN.begin(CAN_500KBPS, MCP_8MHz)) { // init can bus : baudrate = 500k | ||
SERIAL_PORT_MONITOR.println("CAN init fail, retry..."); | ||
delay(100); | ||
} | ||
SERIAL_PORT_MONITOR.println("CAN init ok!"); | ||
} | ||
|
||
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 | ||
|
||
unsigned long canId = CAN.getCanId(); | ||
|
||
SERIAL_PORT_MONITOR.println("-----------------------------"); | ||
SERIAL_PORT_MONITOR.print("Get data from ID: 0x"); | ||
// SPEED | ||
byte speed = buf[0]; | ||
|
||
// RPM | ||
byte lowByte = buf[1]; | ||
byte highByte = buf[2]; | ||
word rpm = word(highByte, lowByte); | ||
|
||
SERIAL_PORT_MONITOR.println(canId, HEX); | ||
|
||
SERIAL_PORT_MONITOR.print("SPEED: "); | ||
SERIAL_PORT_MONITOR.print(speed); | ||
SERIAL_PORT_MONITOR.print("\t"); | ||
SERIAL_PORT_MONITOR.print("RPM: "); | ||
SERIAL_PORT_MONITOR.print(rpm); | ||
SERIAL_PORT_MONITOR.println(); | ||
} | ||
} | ||
|
||
/********************************************************************************************************* | ||
END FILE | ||
*********************************************************************************************************/ |