-
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
ac14dfa
commit f64918d
Showing
3 changed files
with
22 additions
and
133 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
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
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 |
---|---|---|
@@ -1,133 +1,21 @@ | ||
/*! | ||
* @file receiveInterrupt.ino | ||
* @brief CAN-BUS Shield, receive data with interrupt mode when in interrupt mode, | ||
* @n the data coming can't be too fast, must >20ms, or else you can use check mode | ||
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com) | ||
* @license The MIT License (MIT) | ||
* @author Arduinolibrary | ||
* @maintainer [qsjhyy]([email protected]) | ||
* @version V1.0 | ||
* @date 2022-05-25 | ||
* @url https://github.com/DFRobot/DFRobot_MCP2515 | ||
*/ | ||
#include "DFRobot_MCP2515.h" | ||
#include <ADCInput.h> | ||
|
||
const int SPI_CS_PIN = 17; | ||
DFRobot_MCP2515 CAN(SPI_CS_PIN); // Set CS pin | ||
ADCInput RPulse(A0); | ||
// For stereo/dual mikes, could use thi | ||
// ADCInput(A0, A1); | ||
|
||
unsigned char flagRecv = 0; | ||
unsigned char len = 0; | ||
unsigned char buf[8]; | ||
char str[20]; | ||
String BuildMessage = ""; | ||
void setup() { | ||
Serial.begin(115200); | ||
|
||
void setup() | ||
{ | ||
Serial.begin(115200); | ||
RPulse.begin(8000); | ||
|
||
while (CAN.begin(CAN_500KBPS)) | ||
{ // init can bus : baudrate = 500k | ||
Serial.println("DFROBOT's CAN BUS Shield init fail"); | ||
Serial.println("Please Init CAN BUS Shield again"); | ||
delay(3000); | ||
} | ||
Serial.println("DFROBOT's CAN BUS Shield init ok!\n"); | ||
|
||
attachInterrupt(20, MCP2515_ISR, FALLING); // start interrupt | ||
} | ||
|
||
void MCP2515_ISR() | ||
{ | ||
flagRecv = 1; | ||
while (1) { | ||
Serial.printf("%d\n", RPulse.read()); | ||
// For stereo/dual mikes, use this | ||
// Serial.printf("%d %d\n", mike. | ||
} | ||
} | ||
#define INT32U unsigned long int | ||
INT32U canId = 0x000; | ||
unsigned char pidchk[8] = {2, 1, 0, 0, 0, 0, 0, 0}; | ||
unsigned char milClr[8] = {2, 1, 1, 0, 0, 0, 0, 0}; | ||
unsigned char coolTemp[8] = {2, 1, 5, 0, 0, 0, 0, 0}; | ||
unsigned char rpmm[8] = {2, 1, 12, 0, 0, 0, 0, 0}; | ||
unsigned char ambtemp[8] = {2, 1, 70, 0, 0, 0, 0, 0}; | ||
void loop() | ||
{ | ||
|
||
char Order = Serial.read(); | ||
if (Order == '1') | ||
{ | ||
CAN.sendMsgBuf(0x02, 0, 8, pidchk); | ||
Order = 0; | ||
} | ||
else if (Order == '2') | ||
{ | ||
CAN.sendMsgBuf(0x02, 0, 8, milClr); | ||
Order = 0; | ||
} | ||
else if (Order == '3') | ||
{ | ||
CAN.sendMsgBuf(0x02, 0, 8, coolTemp); | ||
Order = 0; | ||
} | ||
else if (Order == '4') | ||
{ | ||
CAN.sendMsgBuf(0x02, 0, 8, rpmm); | ||
Order = 0; | ||
} | ||
else if (Order == '5') | ||
{ | ||
CAN.sendMsgBuf(0x02, 0, 8, ambtemp); | ||
Order = 0; | ||
} | ||
else if (Order == 0) | ||
{ | ||
} | ||
if (flagRecv) | ||
{ // check if get data | ||
|
||
flagRecv = 0; // clear flag | ||
|
||
// iterate over all pending messages | ||
// If either the bus is saturated or the MCU is busy, | ||
// both RX buffers may be in use and after having read a single | ||
// message, MCU does clear the corresponding IRQ conditon. | ||
while (CAN_MSGAVAIL == CAN.checkReceive()) | ||
{ | ||
// read data, len: data length, buf: data buf | ||
CAN.readMsgBuf(&len, buf); | ||
canId = CAN.getCanId(); | ||
if (canId == 0x7E8) | ||
{ | ||
if (buf[0] == 4 && buf[1] == 65) | ||
{ | ||
switch (buf[2]) | ||
{ | ||
case 70: | ||
Serial.print("Ambient Temperature: "); | ||
Serial.println(buf[3]); | ||
break; | ||
case 63: | ||
Serial.println("Status Cleared."); | ||
break; | ||
case 5: | ||
Serial.print("Coolant Temperature: "); | ||
Serial.println(buf[3], OCT); | ||
break; | ||
case 12: | ||
Serial.print("TachoMeter: "); | ||
Serial.println(buf[3], OCT); | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
} | ||
} else if (canId) | ||
// print the data | ||
for (int i = 0; i < len; i++) | ||
{ | ||
BuildMessage = BuildMessage + buf[i] + ","; | ||
} | ||
Serial.println(BuildMessage); | ||
BuildMessage = ""; | ||
Serial.println(" "); | ||
} | ||
} | ||
void loop() { | ||
/* Nothing here */ | ||
} |