From d310e299c635043beefdd097bb92a978cbffde80 Mon Sep 17 00:00:00 2001 From: wangcoolc <1387172050@163.com> Date: Wed, 9 Dec 2020 15:32:14 +0800 Subject: [PATCH] Add canbus FD receive interrupt example --- .../receive_interruptFD.ino | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 examples/receive_interruptFD/receive_interruptFD.ino diff --git a/examples/receive_interruptFD/receive_interruptFD.ino b/examples/receive_interruptFD/receive_interruptFD.ino new file mode 100644 index 0000000..afb5faa --- /dev/null +++ b/examples/receive_interruptFD/receive_interruptFD.ino @@ -0,0 +1,79 @@ +// demo: CAN-BUS Shield, receive data with interrupt mode +// when in interrupt mode, the data coming can't be too fast, must >20ms, or else you can use check mode +// loovee, 2014-6-13 + +#include +#include "mcp2518fd_can.h" + +/*SAMD core*/ +#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE + #define SERIAL SerialUSB +#else + #define SERIAL Serial +#endif + +#define CAN_2518FD + +// 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 = BCM8; +const int CAN_INT_PIN = BCM25; + +#ifdef CAN_2518FD +mcp2518fd CAN(SPI_CS_PIN); // Set CS pin +#endif + +unsigned char flagRecv = 0; +unsigned char len = 0; +unsigned char buf[64]; +char str[20]; + +void setup() { + SERIAL.begin(115200); + while (!SERIAL) { + ; // wait for serial port to connect. Needed for native USB port only + } + CAN.setMode(0); + while (0 != CAN.begin((byte)CAN_500K_1M)) { // init can bus : baudrate = 500k + SERIAL.println("CAN BUS Shield init fail"); + SERIAL.println(" Init CAN BUS Shield again"); + delay(100); + } + byte mode = CAN.getMode(); + SERIAL.printf("CAN BUS get mode = %d\n\r",mode); + SERIAL.println("CAN BUS Shield init ok!"); + SERIAL.println("CAN BUS Shield init ok!"); + pinMode(CAN_INT_PIN, INPUT); + attachInterrupt(digitalPinToInterrupt(CAN_INT_PIN), MCP2515_ISR, FALLING); // start interrupt +} + +void MCP2515_ISR() { + flagRecv = 1; +} + +void loop() { + if (flagRecv) { + // check if get data + + flagRecv = 0; // clear flag + SERIAL.println("into loop"); + // 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 + SERIAL.println("checkReceive"); + CAN.readMsgBuf(&len, buf); + // print the data + for (int i = 0; i < len; i++) { + SERIAL.print(buf[i]); SERIAL.print("\t"); + } + SERIAL.println(); + } + } +} + +/********************************************************************************************************* + END FILE +*********************************************************************************************************/