mirror of
https://github.com/Seeed-Studio/Seeed_Arduino_CAN.git
synced 2026-09-15 02:52:53 +00:00
Pretty printed the Arduino code with astyle
This commit is contained in:
@@ -1,27 +1,27 @@
|
||||
/*************************************************************************************************
|
||||
OBD-II_PIDs TEST CODE
|
||||
LOOVEE @ JUN24, 2017
|
||||
|
||||
Query
|
||||
send id: 0x7df
|
||||
/*************************************************************************************************
|
||||
OBD-II_PIDs TEST CODE
|
||||
LOOVEE @ JUN24, 2017
|
||||
|
||||
Query
|
||||
send id: 0x7df
|
||||
dta: 0x02, 0x01, PID_CODE, 0, 0, 0, 0, 0
|
||||
|
||||
Response
|
||||
From id: 0x7E9 or 0x7EA or 0x7EB
|
||||
Response
|
||||
From id: 0x7E9 or 0x7EA or 0x7EB
|
||||
dta: len, 0x41, PID_CODE, byte0, byte1(option), byte2(option), byte3(option), byte4(option)
|
||||
|
||||
https://en.wikipedia.org/wiki/OBD-II_PIDs
|
||||
|
||||
Input a PID, then you will get reponse from vehicle, the input should be end with '\n'
|
||||
|
||||
https://en.wikipedia.org/wiki/OBD-II_PIDs
|
||||
|
||||
Input a PID, then you will get reponse from vehicle, the input should be end with '\n'
|
||||
***************************************************************************************************/
|
||||
#include <SPI.h>
|
||||
#include "mcp_can.h"
|
||||
|
||||
/*SAMD core*/
|
||||
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
|
||||
#define SERIAL SerialUSB
|
||||
#define SERIAL SerialUSB
|
||||
#else
|
||||
#define SERIAL Serial
|
||||
#define SERIAL Serial
|
||||
#endif
|
||||
|
||||
// the cs pin of the version after v1.1 is default to D9
|
||||
@@ -39,39 +39,35 @@ MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
|
||||
unsigned char PID_INPUT;
|
||||
unsigned char getPid = 0;
|
||||
|
||||
void set_mask_filt()
|
||||
{
|
||||
void set_mask_filt() {
|
||||
/*
|
||||
* set mask, set both the mask to 0x3ff
|
||||
*/
|
||||
set mask, set both the mask to 0x3ff
|
||||
*/
|
||||
CAN.init_Mask(0, 0, 0x7FC);
|
||||
CAN.init_Mask(1, 0, 0x7FC);
|
||||
|
||||
/*
|
||||
* set filter, we can receive id from 0x04 ~ 0x09
|
||||
*/
|
||||
CAN.init_Filt(0, 0, 0x7E8);
|
||||
set filter, we can receive id from 0x04 ~ 0x09
|
||||
*/
|
||||
CAN.init_Filt(0, 0, 0x7E8);
|
||||
CAN.init_Filt(1, 0, 0x7E8);
|
||||
|
||||
CAN.init_Filt(2, 0, 0x7E8);
|
||||
CAN.init_Filt(3, 0, 0x7E8);
|
||||
CAN.init_Filt(4, 0, 0x7E8);
|
||||
CAN.init_Filt(4, 0, 0x7E8);
|
||||
CAN.init_Filt(5, 0, 0x7E8);
|
||||
}
|
||||
|
||||
void sendPid(unsigned char __pid)
|
||||
{
|
||||
void sendPid(unsigned char __pid) {
|
||||
unsigned char tmp[8] = {0x02, 0x01, __pid, 0, 0, 0, 0, 0};
|
||||
SERIAL.print("SEND PID: 0x");
|
||||
SERIAL.println(__pid, HEX);
|
||||
CAN.sendMsgBuf(CAN_ID_PID, 0, 8, tmp);
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
void setup() {
|
||||
SERIAL.begin(115200);
|
||||
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
|
||||
{
|
||||
while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k
|
||||
SERIAL.println("CAN BUS Shield init fail");
|
||||
SERIAL.println(" Init CAN BUS Shield again");
|
||||
delay(100);
|
||||
@@ -81,33 +77,28 @@ void setup()
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
void loop() {
|
||||
taskCanRecv();
|
||||
taskDbg();
|
||||
|
||||
if(getPid) // GET A PID
|
||||
{
|
||||
|
||||
if (getPid) { // GET A PID
|
||||
getPid = 0;
|
||||
sendPid(PID_INPUT);
|
||||
PID_INPUT = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void taskCanRecv()
|
||||
{
|
||||
void taskCanRecv() {
|
||||
unsigned char len = 0;
|
||||
unsigned char buf[8];
|
||||
|
||||
if(CAN_MSGAVAIL == CAN.checkReceive()) // check if get data
|
||||
{
|
||||
if (CAN_MSGAVAIL == CAN.checkReceive()) { // check if get data
|
||||
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
|
||||
|
||||
SERIAL.println("\r\n------------------------------------------------------------------");
|
||||
SERIAL.print("Get Data From id: 0x");
|
||||
SERIAL.println(CAN.getCanId(), HEX);
|
||||
for(int i = 0; i<len; i++) // print the data
|
||||
{
|
||||
for (int i = 0; i < len; i++) { // print the data
|
||||
SERIAL.print("0x");
|
||||
SERIAL.print(buf[i], HEX);
|
||||
SERIAL.print("\t");
|
||||
@@ -116,30 +107,21 @@ void taskCanRecv()
|
||||
}
|
||||
}
|
||||
|
||||
void taskDbg()
|
||||
{
|
||||
while(SERIAL.available())
|
||||
{
|
||||
void taskDbg() {
|
||||
while (SERIAL.available()) {
|
||||
char c = SERIAL.read();
|
||||
|
||||
if(c>='0' && c<='9')
|
||||
{
|
||||
|
||||
if (c >= '0' && c <= '9') {
|
||||
PID_INPUT *= 0x10;
|
||||
PID_INPUT += c-'0';
|
||||
|
||||
}
|
||||
else if(c>='A' && c<='F')
|
||||
{
|
||||
PID_INPUT += c - '0';
|
||||
|
||||
} else if (c >= 'A' && c <= 'F') {
|
||||
PID_INPUT *= 0x10;
|
||||
PID_INPUT += 10+c-'A';
|
||||
}
|
||||
else if(c>='a' && c<='f')
|
||||
{
|
||||
PID_INPUT += 10 + c - 'A';
|
||||
} else if (c >= 'a' && c <= 'f') {
|
||||
PID_INPUT *= 0x10;
|
||||
PID_INPUT += 10+c-'a';
|
||||
}
|
||||
else if(c == '\n') // END
|
||||
{
|
||||
PID_INPUT += 10 + c - 'a';
|
||||
} else if (c == '\n') { // END
|
||||
getPid = 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,9 +5,9 @@
|
||||
|
||||
/*SAMD core*/
|
||||
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
|
||||
#define SERIAL SerialUSB
|
||||
#define SERIAL SerialUSB
|
||||
#else
|
||||
#define SERIAL Serial
|
||||
#define SERIAL Serial
|
||||
#endif
|
||||
|
||||
#define SPI_CS_PIN 10
|
||||
@@ -15,34 +15,28 @@
|
||||
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
void setup() {
|
||||
SERIAL.begin(115200);
|
||||
|
||||
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
|
||||
{
|
||||
while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k
|
||||
SERIAL.println("CAN init failed, retry");
|
||||
delay(100);
|
||||
}
|
||||
SERIAL.println("CAN init ok");
|
||||
|
||||
if(CAN.mcpPinMode(MCP_TX2RTS, MCP_PIN_IN))
|
||||
{
|
||||
if (CAN.mcpPinMode(MCP_TX2RTS, MCP_PIN_IN)) {
|
||||
SERIAL.println("TX2RTS is now an input");
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
SERIAL.println("Could not switch TX2RTS");
|
||||
}
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
void loop() {
|
||||
SERIAL.print("TX2RTS is currently ");
|
||||
SERIAL.println(CAN.mcpDigitalRead(MCP_TX2RTS));
|
||||
delay(500);
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
END FILE
|
||||
END FILE
|
||||
*********************************************************************************************************/
|
||||
|
||||
@@ -5,9 +5,9 @@
|
||||
|
||||
/*SAMD core*/
|
||||
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
|
||||
#define SERIAL SerialUSB
|
||||
#define SERIAL SerialUSB
|
||||
#else
|
||||
#define SERIAL Serial
|
||||
#define SERIAL Serial
|
||||
#endif
|
||||
|
||||
#define SPI_CS_PIN 10
|
||||
@@ -15,38 +15,29 @@
|
||||
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
void setup() {
|
||||
SERIAL.begin(115200);
|
||||
|
||||
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
|
||||
{
|
||||
while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k
|
||||
SERIAL.println("CAN init failed, retry");
|
||||
delay(100);
|
||||
}
|
||||
SERIAL.println("CAN init ok");
|
||||
|
||||
if(CAN.mcpPinMode(MCP_RX0BF, MCP_PIN_OUT))
|
||||
{
|
||||
if (CAN.mcpPinMode(MCP_RX0BF, MCP_PIN_OUT)) {
|
||||
SERIAL.println("RX0BF is now an output");
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
SERIAL.println("Could not switch RX0BF");
|
||||
}
|
||||
|
||||
if(CAN.mcpPinMode(MCP_RX1BF, MCP_PIN_OUT))
|
||||
{
|
||||
if (CAN.mcpPinMode(MCP_RX1BF, MCP_PIN_OUT)) {
|
||||
SERIAL.println("RX1BF is now an output");
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
SERIAL.println("Could not switch RX1BF");
|
||||
}
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
void loop() {
|
||||
SERIAL.println("10");
|
||||
CAN.mcpDigitalWrite(MCP_RX0BF, HIGH);
|
||||
CAN.mcpDigitalWrite(MCP_RX1BF, LOW);
|
||||
@@ -58,5 +49,5 @@ void loop()
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
END FILE
|
||||
END FILE
|
||||
*********************************************************************************************************/
|
||||
|
||||
@@ -7,9 +7,9 @@
|
||||
|
||||
/*SAMD core*/
|
||||
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
|
||||
#define SERIAL SerialUSB
|
||||
#define SERIAL SerialUSB
|
||||
#else
|
||||
#define SERIAL Serial
|
||||
#define SERIAL Serial
|
||||
#endif
|
||||
|
||||
// the cs pin of the version after v1.1 is default to D9
|
||||
@@ -20,13 +20,11 @@ boolean ledON = 1;
|
||||
|
||||
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
|
||||
|
||||
void setup()
|
||||
{
|
||||
void setup() {
|
||||
SERIAL.begin(115200);
|
||||
pinMode(LED,OUTPUT);
|
||||
pinMode(LED, OUTPUT);
|
||||
|
||||
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
|
||||
{
|
||||
while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k
|
||||
SERIAL.println("CAN BUS Shield init fail");
|
||||
SERIAL.println("Init CAN BUS Shield again");
|
||||
delay(100);
|
||||
@@ -35,13 +33,11 @@ void setup()
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
void loop() {
|
||||
unsigned char len = 0;
|
||||
unsigned char buf[8];
|
||||
|
||||
if(CAN_MSGAVAIL == CAN.checkReceive()) // check if data coming
|
||||
{
|
||||
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();
|
||||
@@ -50,19 +46,15 @@ void loop()
|
||||
SERIAL.println("get data from ID: 0x");
|
||||
SERIAL.println(canId, HEX);
|
||||
|
||||
for(int i = 0; i<len; i++) // print the data
|
||||
{
|
||||
for (int i = 0; i < len; i++) { // print the data
|
||||
SERIAL.print(buf[i]);
|
||||
SERIAL.print("\t");
|
||||
if(ledON && i==0)
|
||||
{
|
||||
if (ledON && i == 0) {
|
||||
|
||||
digitalWrite(LED, buf[i]);
|
||||
ledON = 0;
|
||||
delay(500);
|
||||
}
|
||||
else if((!(ledON)) && i==4)
|
||||
{
|
||||
} else if ((!(ledON)) && i == 4) {
|
||||
|
||||
digitalWrite(LED, buf[i]);
|
||||
ledON = 1;
|
||||
|
||||
@@ -8,9 +8,9 @@
|
||||
|
||||
/*SAMD core*/
|
||||
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
|
||||
#define SERIAL SerialUSB
|
||||
#define SERIAL SerialUSB
|
||||
#else
|
||||
#define SERIAL Serial
|
||||
#define SERIAL Serial
|
||||
#endif
|
||||
|
||||
// the cs pin of the version after v1.1 is default to D9
|
||||
@@ -19,12 +19,10 @@ const int SPI_CS_PIN = 9;
|
||||
|
||||
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
|
||||
|
||||
void setup()
|
||||
{
|
||||
void setup() {
|
||||
SERIAL.begin(115200);
|
||||
|
||||
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
|
||||
{
|
||||
while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k
|
||||
SERIAL.println("CAN BUS Shield init fail");
|
||||
SERIAL.println(" Init CAN BUS Shield again");
|
||||
delay(100);
|
||||
@@ -33,23 +31,20 @@ void setup()
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
void loop() {
|
||||
unsigned char len = 0;
|
||||
unsigned char buf[8];
|
||||
|
||||
if(CAN_MSGAVAIL == CAN.checkReceive()) // check if data coming
|
||||
{
|
||||
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.println("-----------------------------");
|
||||
SERIAL.print("Get data from ID: 0x");
|
||||
SERIAL.println(canId, HEX);
|
||||
|
||||
for(int i = 0; i<len; i++) // print the data
|
||||
{
|
||||
for (int i = 0; i < len; i++) { // print the data
|
||||
SERIAL.print(buf[i], HEX);
|
||||
SERIAL.print("\t");
|
||||
}
|
||||
@@ -58,5 +53,5 @@ void loop()
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
END FILE
|
||||
END FILE
|
||||
*********************************************************************************************************/
|
||||
|
||||
@@ -7,9 +7,9 @@
|
||||
|
||||
/*SAMD core*/
|
||||
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
|
||||
#define SERIAL SerialUSB
|
||||
#define SERIAL SerialUSB
|
||||
#else
|
||||
#define SERIAL Serial
|
||||
#define SERIAL Serial
|
||||
#endif
|
||||
|
||||
// the cs pin of the version after v1.1 is default to D9
|
||||
@@ -25,14 +25,12 @@ unsigned char len = 0;
|
||||
unsigned char buf[8];
|
||||
char str[20];
|
||||
|
||||
void setup()
|
||||
{
|
||||
void setup() {
|
||||
SERIAL.begin(115200);
|
||||
while (!SERIAL) {
|
||||
; // wait for serial port to connect. Needed for native USB port only
|
||||
; // wait for serial port to connect. Needed for native USB port only
|
||||
}
|
||||
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
|
||||
{
|
||||
while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k
|
||||
SERIAL.println("CAN BUS Shield init fail");
|
||||
SERIAL.println(" Init CAN BUS Shield again");
|
||||
delay(100);
|
||||
@@ -42,15 +40,13 @@ void setup()
|
||||
attachInterrupt(digitalPinToInterrupt(CAN_INT_PIN), MCP2515_ISR, FALLING); // start interrupt
|
||||
}
|
||||
|
||||
void MCP2515_ISR()
|
||||
{
|
||||
void MCP2515_ISR() {
|
||||
flagRecv = 1;
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
if(flagRecv)
|
||||
{ // check if get data
|
||||
void loop() {
|
||||
if (flagRecv) {
|
||||
// check if get data
|
||||
|
||||
flagRecv = 0; // clear flag
|
||||
|
||||
@@ -58,15 +54,13 @@ void loop()
|
||||
// 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())
|
||||
{
|
||||
while (CAN_MSGAVAIL == CAN.checkReceive()) {
|
||||
// read data, len: data length, buf: data buf
|
||||
CAN.readMsgBuf(&len, buf);
|
||||
|
||||
// print the data
|
||||
for(int i = 0; i<len; i++)
|
||||
{
|
||||
SERIAL.print(buf[i]);SERIAL.print("\t");
|
||||
for (int i = 0; i < len; i++) {
|
||||
SERIAL.print(buf[i]); SERIAL.print("\t");
|
||||
}
|
||||
SERIAL.println();
|
||||
}
|
||||
@@ -74,5 +68,5 @@ void loop()
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
END FILE
|
||||
END FILE
|
||||
*********************************************************************************************************/
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
// demo: CAN Sleep Example - receive
|
||||
// by Kai, based on the receive_interrupt example by loovee and the additions from Zak Kemble (https://github.com/coryjfowler/MCP_CAN_lib/pull/10/files)
|
||||
//
|
||||
// By setting the MCU, the CAN controller (MCP2515) and the transceiver (MCP2551) into sleep mode, you can reduce
|
||||
// the power consumption of the whole setup from around 50mA down to 240uA (Arduino directly connected to 5V, regulator and
|
||||
//
|
||||
// By setting the MCU, the CAN controller (MCP2515) and the transceiver (MCP2551) into sleep mode, you can reduce
|
||||
// the power consumption of the whole setup from around 50mA down to 240uA (Arduino directly connected to 5V, regulator and
|
||||
// power LED removed). The node will wake up when a new message arrives, process the message and go back to sleep
|
||||
// afterwards.
|
||||
//
|
||||
// Known issues:
|
||||
// - Because it takes some time for the controller to wake up, the first message is usually lost. Look at the
|
||||
// - Because it takes some time for the controller to wake up, the first message is usually lost. Look at the
|
||||
// send_sleep example on how to avoid this by sending a special wakeup message before the normal message.
|
||||
// - If you only have 2 devices on the CAN bus (the device running this sketch and some other device sending
|
||||
// messages), you may find that duplicate messages are received when waking up. This is because when
|
||||
// the MCP2515 wakes up it enters LISTENONLY mode where it does not send ACKs to messages, so the transmitter
|
||||
// will retransmit the same message a few times. See below for a simple solution to filter duplicate messages out.
|
||||
// - If you only have 2 devices on the CAN bus (the device running this sketch and some other device sending
|
||||
// messages), you may find that duplicate messages are received when waking up. This is because when
|
||||
// the MCP2515 wakes up it enters LISTENONLY mode where it does not send ACKs to messages, so the transmitter
|
||||
// will retransmit the same message a few times. See below for a simple solution to filter duplicate messages out.
|
||||
|
||||
#include <SPI.h>
|
||||
#include "mcp_can.h"
|
||||
@@ -20,9 +20,9 @@
|
||||
|
||||
/*SAMD core*/
|
||||
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
|
||||
#define SERIAL SerialUSB
|
||||
#define SERIAL SerialUSB
|
||||
#else
|
||||
#define SERIAL Serial
|
||||
#define SERIAL Serial
|
||||
#endif
|
||||
|
||||
// the cs pin of the version after v1.1 is default to D9
|
||||
@@ -30,13 +30,13 @@
|
||||
const int SPI_CS_PIN = 9;
|
||||
#define CAN_INT 2 // Set INT to pin 2
|
||||
|
||||
// To use the sleep mode of the transceiver (MCP2551), it's Rs pin must be connected to either the MCP2515 or
|
||||
// To use the sleep mode of the transceiver (MCP2551), it's Rs pin must be connected to either the MCP2515 or
|
||||
// any free Arduino output.
|
||||
#define RS_TO_MCP2515 true // Set this to false if Rs is connected to your Arduino
|
||||
#define RS_OUTPUT MCP_RX0BF // RX0BF is a pin of the MCP2515. You can also define an Arduino pin here
|
||||
|
||||
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
|
||||
|
||||
|
||||
#define KEEP_AWAKE_TIME 200 // time the controller will stay awake after the last activity on the bus (in ms)
|
||||
unsigned long lastBusActivity = millis();
|
||||
|
||||
@@ -51,12 +51,10 @@ unsigned long lastMsgTime = 0;
|
||||
|
||||
char str[20];
|
||||
|
||||
void setup()
|
||||
{
|
||||
void setup() {
|
||||
SERIAL.begin(115200);
|
||||
|
||||
while (CAN_OK != CAN.begin(CAN_500KBPS, MCP_16MHz)) // init can bus : baudrate = 500k
|
||||
{
|
||||
while (CAN_OK != CAN.begin(CAN_500KBPS, MCP_16MHz)) { // init can bus : baudrate = 500k
|
||||
SERIAL.println("CAN BUS Shield init fail");
|
||||
SERIAL.println(" Init CAN BUS Shield again");
|
||||
delay(100);
|
||||
@@ -70,25 +68,22 @@ void setup()
|
||||
CAN.setSleepWakeup(1); // this tells the MCP2515 to wake up on incoming messages
|
||||
|
||||
// Pull the Rs pin of the MCP2551 transceiver low to enable it:
|
||||
if(RS_TO_MCP2515)
|
||||
{
|
||||
CAN.mcpPinMode(MCP_RX0BF, MCP_PIN_OUT);
|
||||
CAN.mcpDigitalWrite(RS_OUTPUT, LOW);
|
||||
if (RS_TO_MCP2515) {
|
||||
CAN.mcpPinMode(MCP_RX0BF, MCP_PIN_OUT);
|
||||
CAN.mcpDigitalWrite(RS_OUTPUT, LOW);
|
||||
} else {
|
||||
pinMode(RS_OUTPUT, OUTPUT);
|
||||
digitalWrite(RS_OUTPUT, LOW);
|
||||
pinMode(RS_OUTPUT, OUTPUT);
|
||||
digitalWrite(RS_OUTPUT, LOW);
|
||||
}
|
||||
}
|
||||
|
||||
void MCP2515_ISR()
|
||||
{
|
||||
void MCP2515_ISR() {
|
||||
flagRecv = 1;
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
if(flagRecv)
|
||||
{ // check if get data
|
||||
void loop() {
|
||||
if (flagRecv) {
|
||||
// check if get data
|
||||
|
||||
flagRecv = 0; // clear flag
|
||||
lastBusActivity = millis();
|
||||
@@ -97,69 +92,67 @@ void loop()
|
||||
// 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())
|
||||
{
|
||||
while (CAN_MSGAVAIL == CAN.checkReceive()) {
|
||||
// read data, len: data length, buf: data buf
|
||||
CAN.readMsgBuf(&len, buf);
|
||||
|
||||
// check if this is a duplicate message (including a timeout, so that the same message is accepted again after a while)
|
||||
if((len != lastLen) || (millis() > lastMsgTime + DUPLICATE_TIMEOUT) || (memcmp((const void *)lastBuf, (const void *)buf, sizeof(buf)) != 0))
|
||||
{
|
||||
lastLen = len;
|
||||
memcpy(lastBuf, buf, sizeof(buf));
|
||||
lastMsgTime = millis();
|
||||
|
||||
// print the data
|
||||
for(int i = 0; i<len; i++)
|
||||
{
|
||||
SERIAL.print(buf[i]);SERIAL.print("\t");
|
||||
}
|
||||
SERIAL.println();
|
||||
if ((len != lastLen) || (millis() > lastMsgTime + DUPLICATE_TIMEOUT)
|
||||
|| (memcmp((const void*)lastBuf, (const void*)buf, sizeof(buf)) != 0)) {
|
||||
lastLen = len;
|
||||
memcpy(lastBuf, buf, sizeof(buf));
|
||||
lastMsgTime = millis();
|
||||
|
||||
// print the data
|
||||
for (int i = 0; i < len; i++) {
|
||||
SERIAL.print(buf[i]); SERIAL.print("\t");
|
||||
}
|
||||
SERIAL.println();
|
||||
}
|
||||
}
|
||||
} else if(millis() > lastBusActivity + KEEP_AWAKE_TIME)
|
||||
{
|
||||
// Put MCP2515 into sleep mode
|
||||
SERIAL.println(F("CAN sleep"));
|
||||
CAN.sleep();
|
||||
|
||||
// Put the transceiver into standby (by pulling Rs high):
|
||||
if(RS_TO_MCP2515)
|
||||
CAN.mcpDigitalWrite(RS_OUTPUT, HIGH);
|
||||
else
|
||||
digitalWrite(RS_OUTPUT, HIGH);
|
||||
|
||||
// Put the MCU to sleep
|
||||
SERIAL.println(F("MCU sleep"));
|
||||
} else if (millis() > lastBusActivity + KEEP_AWAKE_TIME) {
|
||||
// Put MCP2515 into sleep mode
|
||||
SERIAL.println(F("CAN sleep"));
|
||||
CAN.sleep();
|
||||
|
||||
// Clear serial buffers before sleeping
|
||||
SERIAL.flush();
|
||||
// Put the transceiver into standby (by pulling Rs high):
|
||||
if (RS_TO_MCP2515) {
|
||||
CAN.mcpDigitalWrite(RS_OUTPUT, HIGH);
|
||||
} else {
|
||||
digitalWrite(RS_OUTPUT, HIGH);
|
||||
}
|
||||
|
||||
cli(); // Disable interrupts
|
||||
if(!flagRecv) // Make sure we havn't missed an interrupt between the check above and now. If an interrupt happens between now and sei()/sleep_cpu() then sleep_cpu() will immediately wake up again
|
||||
{
|
||||
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
|
||||
sleep_enable();
|
||||
sleep_bod_disable();
|
||||
// Put the MCU to sleep
|
||||
SERIAL.println(F("MCU sleep"));
|
||||
|
||||
// Clear serial buffers before sleeping
|
||||
SERIAL.flush();
|
||||
|
||||
cli(); // Disable interrupts
|
||||
if (!flagRecv) { // Make sure we havn't missed an interrupt between the check above and now. If an interrupt happens between now and sei()/sleep_cpu() then sleep_cpu() will immediately wake up again
|
||||
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
|
||||
sleep_enable();
|
||||
sleep_bod_disable();
|
||||
sei();
|
||||
sleep_cpu();
|
||||
// Now the Arduino sleeps until the next message arrives...
|
||||
sleep_disable();
|
||||
}
|
||||
sei();
|
||||
sleep_cpu();
|
||||
// Now the Arduino sleeps until the next message arrives...
|
||||
sleep_disable();
|
||||
}
|
||||
sei();
|
||||
|
||||
CAN.wake(); // When the MCP2515 wakes up it will be in LISTENONLY mode, here we put it into the mode it was before sleeping
|
||||
CAN.wake(); // When the MCP2515 wakes up it will be in LISTENONLY mode, here we put it into the mode it was before sleeping
|
||||
|
||||
// Wake up the transceiver:
|
||||
if(RS_TO_MCP2515)
|
||||
CAN.mcpDigitalWrite(RS_OUTPUT, LOW);
|
||||
else
|
||||
digitalWrite(RS_OUTPUT, LOW);
|
||||
// Wake up the transceiver:
|
||||
if (RS_TO_MCP2515) {
|
||||
CAN.mcpDigitalWrite(RS_OUTPUT, LOW);
|
||||
} else {
|
||||
digitalWrite(RS_OUTPUT, LOW);
|
||||
}
|
||||
|
||||
SERIAL.println(F("Woke up"));
|
||||
SERIAL.println(F("Woke up"));
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
END FILE
|
||||
END FILE
|
||||
*********************************************************************************************************/
|
||||
|
||||
@@ -9,9 +9,9 @@
|
||||
|
||||
/*SAMD core*/
|
||||
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
|
||||
#define SERIAL SerialUSB
|
||||
#define SERIAL SerialUSB
|
||||
#else
|
||||
#define SERIAL Serial
|
||||
#define SERIAL Serial
|
||||
#endif
|
||||
|
||||
File myFile;
|
||||
@@ -28,12 +28,10 @@ unsigned char len = 0;
|
||||
unsigned char buf[8];
|
||||
char str[20];
|
||||
|
||||
void setup()
|
||||
{
|
||||
void setup() {
|
||||
SERIAL.begin(115200);
|
||||
|
||||
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
|
||||
{
|
||||
while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k
|
||||
SERIAL.println("CAN BUS Shield init fail");
|
||||
SERIAL.println("Init CAN BUS Shield again");
|
||||
delay(100);
|
||||
@@ -41,55 +39,51 @@ void setup()
|
||||
SERIAL.println("CAN BUS Shield init ok!");
|
||||
|
||||
attachInterrupt(0, MCP2515_ISR, FALLING); // start interrupt
|
||||
|
||||
|
||||
if (!SD.begin(4)) {
|
||||
SERIAL.println("SD initialization failed!");
|
||||
while(1);
|
||||
while (1);
|
||||
}
|
||||
SERIAL.println("SD initialization done.");
|
||||
}
|
||||
|
||||
void MCP2515_ISR()
|
||||
{
|
||||
void MCP2515_ISR() {
|
||||
flagRecv = 1;
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
if(flagRecv)
|
||||
{ // check if get data
|
||||
void loop() {
|
||||
if (flagRecv) {
|
||||
// check if get data
|
||||
|
||||
flagRecv = 0; // clear flag
|
||||
unsigned long id= 0;
|
||||
|
||||
unsigned long id = 0;
|
||||
|
||||
myFile = SD.open("can.csv", FILE_WRITE);
|
||||
|
||||
// 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())
|
||||
{
|
||||
while (CAN_MSGAVAIL == CAN.checkReceive()) {
|
||||
// read data, len: data length, buf: data buf
|
||||
CAN.readMsgBufID(&id, &len, buf);
|
||||
|
||||
|
||||
SERIAL.print(id);
|
||||
SERIAL.print(",");
|
||||
myFile.print(id);
|
||||
myFile.print(",");
|
||||
|
||||
for(int i = 0; i<len; i++)
|
||||
{
|
||||
|
||||
for (int i = 0; i < len; i++) {
|
||||
SERIAL.print(buf[i]);
|
||||
SERIAL.print(",");
|
||||
|
||||
|
||||
myFile.print(buf[i]);
|
||||
myFile.print(",");
|
||||
}
|
||||
SERIAL.println();
|
||||
myFile.println();
|
||||
}
|
||||
|
||||
|
||||
myFile.close();
|
||||
}
|
||||
}
|
||||
|
||||
+10
-15
@@ -6,9 +6,9 @@
|
||||
|
||||
/*SAMD core*/
|
||||
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
|
||||
#define SERIAL SerialUSB
|
||||
#define SERIAL SerialUSB
|
||||
#else
|
||||
#define SERIAL Serial
|
||||
#define SERIAL Serial
|
||||
#endif
|
||||
|
||||
// the cs pin of the version after v1.1 is default to D9
|
||||
@@ -17,12 +17,10 @@ const int SPI_CS_PIN = 9;
|
||||
|
||||
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
|
||||
|
||||
void setup()
|
||||
{
|
||||
void setup() {
|
||||
SERIAL.begin(115200);
|
||||
|
||||
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
|
||||
{
|
||||
while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k
|
||||
SERIAL.println("CAN BUS Shield init fail");
|
||||
SERIAL.println(" Init CAN BUS Shield again");
|
||||
delay(100);
|
||||
@@ -31,22 +29,19 @@ void setup()
|
||||
}
|
||||
|
||||
unsigned char stmp[8] = {0, 0, 0, 0, 0, 0, 0, 0};
|
||||
void loop()
|
||||
{
|
||||
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] = stmp[7] + 1;
|
||||
if (stmp[7] == 100) {
|
||||
stmp[7] = 0;
|
||||
stmp[6] = stmp[6] + 1;
|
||||
|
||||
if(stmp[6] == 100)
|
||||
{
|
||||
|
||||
if (stmp[6] == 100) {
|
||||
stmp[6] = 0;
|
||||
stmp[5] = stmp[6] + 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
CAN.sendMsgBuf(0x00, 0, 8, stmp);
|
||||
delay(100); // send data per 100ms
|
||||
}
|
||||
|
||||
@@ -4,9 +4,9 @@
|
||||
|
||||
/*SAMD core*/
|
||||
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
|
||||
#define SERIAL SerialUSB
|
||||
#define SERIAL SerialUSB
|
||||
#else
|
||||
#define SERIAL Serial
|
||||
#define SERIAL Serial
|
||||
#endif
|
||||
|
||||
// the cs pin of the version after v1.1 is default to D9
|
||||
@@ -17,12 +17,10 @@ const int ledLOW = 0;
|
||||
|
||||
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
|
||||
|
||||
void setup()
|
||||
{
|
||||
void setup() {
|
||||
SERIAL.begin(115200);
|
||||
|
||||
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
|
||||
{
|
||||
while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k
|
||||
SERIAL.println("CAN BUS Shield init fail");
|
||||
SERIAL.println(" Init CAN BUS Shield again");
|
||||
delay(100);
|
||||
@@ -32,13 +30,13 @@ void setup()
|
||||
|
||||
unsigned char stmp[8] = {ledHIGH, 1, 2, 3, ledLOW, 5, 6, 7};
|
||||
|
||||
void loop()
|
||||
{ SERIAL.println("In loop");
|
||||
void loop() {
|
||||
SERIAL.println("In loop");
|
||||
// send data: id = 0x70, standard frame, data len = 8, stmp: data buf
|
||||
CAN.sendMsgBuf(0x70, 0, 8, stmp);
|
||||
delay(1000); // send data once per second
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
END FILE
|
||||
END FILE
|
||||
*********************************************************************************************************/
|
||||
|
||||
@@ -18,42 +18,40 @@
|
||||
|
||||
/*SAMD core*/
|
||||
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
|
||||
#define SERIAL SerialUSB
|
||||
#define SERIAL SerialUSB
|
||||
#else
|
||||
#define SERIAL Serial
|
||||
#define SERIAL Serial
|
||||
#endif
|
||||
|
||||
ros::NodeHandle nh;
|
||||
|
||||
const int SPI_CS_PIN = 9;
|
||||
const int ledHIGH=1;
|
||||
const int ledLOW=0;
|
||||
const int ledHIGH = 1;
|
||||
const int ledLOW = 0;
|
||||
unsigned char stmp[8] = {ledHIGH, 1, 2, 3, ledLOW, 5, 6, 7};
|
||||
|
||||
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
|
||||
|
||||
void messageCb( const std_msgs::Empty& toggle_msg){
|
||||
//digitalWrite(13, HIGH-digitalRead(13)); // blink the led
|
||||
// send data: id = 0x00, standrad frame, data len = 8, stmp: data buf
|
||||
CAN.sendMsgBuf(0x70,0, 8, stmp);
|
||||
delay(1000); // send data per 100ms
|
||||
void messageCb(const std_msgs::Empty& toggle_msg) {
|
||||
//digitalWrite(13, HIGH-digitalRead(13)); // blink the led
|
||||
// send data: id = 0x00, standrad frame, data len = 8, stmp: data buf
|
||||
CAN.sendMsgBuf(0x70, 0, 8, stmp);
|
||||
delay(1000); // send data per 100ms
|
||||
}
|
||||
|
||||
ros::Subscriber<std_msgs::Empty> sub("toggle_led", &messageCb );
|
||||
ros::Subscriber<std_msgs::Empty> sub("toggle_led", &messageCb);
|
||||
|
||||
// the cs pin of the version after v1.1 is default to D9
|
||||
// v0.9b and v1.0 is default D10
|
||||
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
void setup() {
|
||||
SERIAL.begin(115200);
|
||||
nh.initNode();
|
||||
nh.subscribe(sub);
|
||||
|
||||
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
|
||||
{
|
||||
while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k
|
||||
SERIAL.println("CAN BUS Shield init fail");
|
||||
SERIAL.println(" Init CAN BUS Shield again");
|
||||
delay(100);
|
||||
@@ -62,13 +60,12 @@ void setup()
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
|
||||
void loop() {
|
||||
|
||||
nh.spinOnce();
|
||||
delay(1);
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
END FILE
|
||||
END FILE
|
||||
*********************************************************************************************************/
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// demo: CAN Sleep Example - send
|
||||
// by Kai, based on the send example by loovee and the additions from Zak Kemble (https://github.com/coryjfowler/MCP_CAN_lib/pull/10/files)
|
||||
//
|
||||
//
|
||||
// See receive_sleep example for additional notes.
|
||||
|
||||
#include <mcp_can.h>
|
||||
@@ -10,64 +10,61 @@
|
||||
|
||||
/*SAMD core*/
|
||||
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
|
||||
#define SERIAL SerialUSB
|
||||
#define SERIAL SerialUSB
|
||||
#else
|
||||
#define SERIAL Serial
|
||||
#define SERIAL Serial
|
||||
#endif
|
||||
|
||||
// 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;
|
||||
|
||||
// To use the sleep mode of the transceiver (MCP2551), it's Rs pin must be connected to either the MCP2515 or
|
||||
// To use the sleep mode of the transceiver (MCP2551), it's Rs pin must be connected to either the MCP2515 or
|
||||
// any free Arduino output.
|
||||
#define RS_TO_MCP2515 true // Set this to false if Rs is connected to your Arduino
|
||||
#define RS_OUTPUT MCP_RX0BF // RX0BF is a pin of the MCP2515. You can also define an Arduino pin here
|
||||
|
||||
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
|
||||
|
||||
// Watchdog interrupt, used to wake the MCU periodically.
|
||||
ISR (WDT_vect)
|
||||
{
|
||||
wdt_disable(); // disable watchdog
|
||||
}
|
||||
|
||||
void sleepMCU()
|
||||
// Sleep the MCU for one second.
|
||||
// See http://www.gammon.com.au/power for details.
|
||||
// (You can make your life a lot easyer by using one of the numeral low power libraries for Arduino.)
|
||||
{
|
||||
// disable ADC
|
||||
ADCSRA = 0;
|
||||
|
||||
// clear various "reset" flags
|
||||
MCUSR = 0;
|
||||
// allow changes, disable reset
|
||||
WDTCSR = bit (WDCE) | bit (WDE);
|
||||
// set interrupt mode and an interval
|
||||
WDTCSR = bit (WDIE) | bit (WDP2) | bit (WDP1); // set WDIE, and 1 second delay
|
||||
wdt_reset(); // pat the dog
|
||||
|
||||
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
|
||||
noInterrupts (); // timed sequence follows
|
||||
sleep_enable();
|
||||
|
||||
// turn off brown-out enable in software
|
||||
MCUCR = bit (BODS) | bit (BODSE);
|
||||
MCUCR = bit (BODS);
|
||||
interrupts (); // guarantees next instruction executed
|
||||
sleep_cpu ();
|
||||
|
||||
// cancel sleep as a precaution
|
||||
sleep_disable();
|
||||
// Watchdog interrupt, used to wake the MCU periodically.
|
||||
ISR(WDT_vect) {
|
||||
wdt_disable(); // disable watchdog
|
||||
}
|
||||
|
||||
void setup()
|
||||
void sleepMCU()
|
||||
// Sleep the MCU for one second.
|
||||
// See http://www.gammon.com.au/power for details.
|
||||
// (You can make your life a lot easyer by using one of the numeral low power libraries for Arduino.)
|
||||
{
|
||||
// disable ADC
|
||||
ADCSRA = 0;
|
||||
|
||||
// clear various "reset" flags
|
||||
MCUSR = 0;
|
||||
// allow changes, disable reset
|
||||
WDTCSR = bit(WDCE) | bit(WDE);
|
||||
// set interrupt mode and an interval
|
||||
WDTCSR = bit(WDIE) | bit(WDP2) | bit(WDP1); // set WDIE, and 1 second delay
|
||||
wdt_reset(); // pat the dog
|
||||
|
||||
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
|
||||
noInterrupts(); // timed sequence follows
|
||||
sleep_enable();
|
||||
|
||||
// turn off brown-out enable in software
|
||||
MCUCR = bit(BODS) | bit(BODSE);
|
||||
MCUCR = bit(BODS);
|
||||
interrupts(); // guarantees next instruction executed
|
||||
sleep_cpu();
|
||||
|
||||
// cancel sleep as a precaution
|
||||
sleep_disable();
|
||||
}
|
||||
|
||||
void setup() {
|
||||
SERIAL.begin(115200);
|
||||
|
||||
while (CAN_OK != CAN.begin(CAN_500KBPS, MCP_16MHz)) // init can bus : baudrate = 500k
|
||||
{
|
||||
while (CAN_OK != CAN.begin(CAN_500KBPS, MCP_16MHz)) { // init can bus : baudrate = 500k
|
||||
SERIAL.println("CAN BUS Shield init fail");
|
||||
SERIAL.println(" Init CAN BUS Shield again");
|
||||
delay(100);
|
||||
@@ -75,69 +72,67 @@ void setup()
|
||||
SERIAL.println("CAN BUS Shield init ok!");
|
||||
|
||||
CAN.setSleepWakeup(0); // the MCP2515 will NOT wake up on incoming messages,
|
||||
// making it a 'send only' node
|
||||
// making it a 'send only' node
|
||||
|
||||
// Pull the Rs pin of the MCP2551 transceiver low to enable it:
|
||||
if(RS_TO_MCP2515)
|
||||
{
|
||||
CAN.mcpPinMode(MCP_RX0BF, MCP_PIN_OUT);
|
||||
CAN.mcpDigitalWrite(RS_OUTPUT, LOW);
|
||||
if (RS_TO_MCP2515) {
|
||||
CAN.mcpPinMode(MCP_RX0BF, MCP_PIN_OUT);
|
||||
CAN.mcpDigitalWrite(RS_OUTPUT, LOW);
|
||||
} else {
|
||||
pinMode(RS_OUTPUT, OUTPUT);
|
||||
digitalWrite(RS_OUTPUT, LOW);
|
||||
pinMode(RS_OUTPUT, OUTPUT);
|
||||
digitalWrite(RS_OUTPUT, LOW);
|
||||
}
|
||||
}
|
||||
|
||||
unsigned char stmp[8] = {0, 0, 0, 0, 0, 0, 0, 0};
|
||||
|
||||
void loop()
|
||||
{
|
||||
void loop() {
|
||||
SERIAL.println("Sending message");
|
||||
|
||||
CAN.sendMsgBuf(0x00, 0, 0, NULL); // Send empty wakeup message
|
||||
|
||||
delay(100); // give the receiving node some time to wake up
|
||||
|
||||
|
||||
|
||||
// send data: id = 0x00, standard frame, data len = 8, stmp: data buf
|
||||
stmp[7] = stmp[7]+1;
|
||||
if(stmp[7] == 100)
|
||||
{
|
||||
stmp[7] = stmp[7] + 1;
|
||||
if (stmp[7] == 100) {
|
||||
stmp[7] = 0;
|
||||
stmp[6] = stmp[6] + 1;
|
||||
|
||||
if(stmp[6] == 100)
|
||||
{
|
||||
|
||||
if (stmp[6] == 100) {
|
||||
stmp[6] = 0;
|
||||
stmp[5] = stmp[6] + 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
CAN.sendMsgBuf(0x00, 0, 8, stmp);
|
||||
|
||||
|
||||
|
||||
// sleep
|
||||
SERIAL.println("Sleep");
|
||||
SERIAL.flush();
|
||||
|
||||
|
||||
// Put MCP2515 into sleep mode
|
||||
CAN.sleep();
|
||||
|
||||
|
||||
// Put the transceiver into standby (by pulling Rs high):
|
||||
if(RS_TO_MCP2515)
|
||||
CAN.mcpDigitalWrite(RS_OUTPUT, HIGH);
|
||||
else
|
||||
digitalWrite(RS_OUTPUT, HIGH);
|
||||
|
||||
if (RS_TO_MCP2515) {
|
||||
CAN.mcpDigitalWrite(RS_OUTPUT, HIGH);
|
||||
} else {
|
||||
digitalWrite(RS_OUTPUT, HIGH);
|
||||
}
|
||||
|
||||
// Put the MCU to sleep for one second
|
||||
sleepMCU();
|
||||
|
||||
// wake MCP2515 and transceiver
|
||||
CAN.wake();
|
||||
if(RS_TO_MCP2515)
|
||||
CAN.mcpDigitalWrite(RS_OUTPUT, LOW);
|
||||
else
|
||||
digitalWrite(RS_OUTPUT, LOW);
|
||||
// wake MCP2515 and transceiver
|
||||
CAN.wake();
|
||||
if (RS_TO_MCP2515) {
|
||||
CAN.mcpDigitalWrite(RS_OUTPUT, LOW);
|
||||
} else {
|
||||
digitalWrite(RS_OUTPUT, LOW);
|
||||
}
|
||||
}
|
||||
|
||||
// END FILE
|
||||
|
||||
@@ -8,9 +8,9 @@
|
||||
|
||||
/*SAMD core*/
|
||||
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
|
||||
#define SERIAL SerialUSB
|
||||
#define SERIAL SerialUSB
|
||||
#else
|
||||
#define SERIAL Serial
|
||||
#define SERIAL Serial
|
||||
#endif
|
||||
|
||||
// the cs pin of the version after v1.1 is default to D9
|
||||
@@ -25,12 +25,10 @@ unsigned char len = 0;
|
||||
unsigned char buf[8];
|
||||
char str[20];
|
||||
|
||||
void setup()
|
||||
{
|
||||
void setup() {
|
||||
SERIAL.begin(115200);
|
||||
|
||||
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
|
||||
{
|
||||
while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k
|
||||
SERIAL.println("CAN BUS Shield init fail");
|
||||
SERIAL.println(" Init CAN BUS Shield again");
|
||||
delay(100);
|
||||
@@ -41,15 +39,15 @@ void setup()
|
||||
|
||||
|
||||
/*
|
||||
* set mask, set both the mask to 0x3ff
|
||||
*/
|
||||
set mask, set both the mask to 0x3ff
|
||||
*/
|
||||
CAN.init_Mask(0, 0, 0x3ff); // there are 2 mask in mcp2515, you need to set both of them
|
||||
CAN.init_Mask(1, 0, 0x3ff);
|
||||
|
||||
|
||||
/*
|
||||
* set filter, we can receive id from 0x04 ~ 0x09
|
||||
*/
|
||||
set filter, we can receive id from 0x04 ~ 0x09
|
||||
*/
|
||||
CAN.init_Filt(0, 0, 0x04); // there are 6 filter in mcp2515
|
||||
CAN.init_Filt(1, 0, 0x05); // there are 6 filter in mcp2515
|
||||
|
||||
@@ -60,15 +58,12 @@ void setup()
|
||||
|
||||
}
|
||||
|
||||
void MCP2515_ISR()
|
||||
{
|
||||
void MCP2515_ISR() {
|
||||
flagRecv = 1;
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
if(flagRecv) // check if get data
|
||||
{
|
||||
void loop() {
|
||||
if (flagRecv) { // check if get data
|
||||
|
||||
flagRecv = 0; // clear flag
|
||||
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
|
||||
@@ -76,8 +71,7 @@ void loop()
|
||||
SERIAL.println("\r\n------------------------------------------------------------------");
|
||||
SERIAL.print("Get Data From id: ");
|
||||
SERIAL.println(CAN.getCanId());
|
||||
for(int i = 0; i<len; i++) // print the data
|
||||
{
|
||||
for (int i = 0; i < len; i++) { // print the data
|
||||
SERIAL.print("0x");
|
||||
SERIAL.print(buf[i], HEX);
|
||||
SERIAL.print("\t");
|
||||
@@ -88,5 +82,5 @@ void loop()
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
END FILE
|
||||
END FILE
|
||||
*********************************************************************************************************/
|
||||
|
||||
@@ -6,9 +6,9 @@
|
||||
|
||||
/*SAMD core*/
|
||||
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
|
||||
#define SERIAL SerialUSB
|
||||
#define SERIAL SerialUSB
|
||||
#else
|
||||
#define SERIAL Serial
|
||||
#define SERIAL Serial
|
||||
#endif
|
||||
|
||||
// the cs pin of the version after v1.1 is default to D9
|
||||
@@ -17,12 +17,10 @@ const int SPI_CS_PIN = 9;
|
||||
|
||||
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
|
||||
|
||||
void setup()
|
||||
{
|
||||
void setup() {
|
||||
SERIAL.begin(115200);
|
||||
|
||||
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
|
||||
{
|
||||
while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k
|
||||
SERIAL.println("CAN BUS Shield init fail");
|
||||
SERIAL.println(" Init CAN BUS Shield again");
|
||||
delay(100);
|
||||
@@ -32,10 +30,8 @@ void setup()
|
||||
|
||||
unsigned char stmp[8] = {0, 1, 2, 3, 4, 5, 6, 7};
|
||||
|
||||
void loop()
|
||||
{
|
||||
for(int id=0; id<10; id++)
|
||||
{
|
||||
void loop() {
|
||||
for (int id = 0; id < 10; id++) {
|
||||
memset(stmp, id, sizeof(stmp)); // set id to send data buff
|
||||
CAN.sendMsgBuf(id, 0, sizeof(stmp), stmp);
|
||||
delay(100);
|
||||
@@ -43,5 +39,5 @@ void loop()
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
END FILE
|
||||
END FILE
|
||||
*********************************************************************************************************/
|
||||
|
||||
Reference in New Issue
Block a user