mirror of
https://github.com/Seeed-Studio/Seeed_Arduino_CAN.git
synced 2026-09-15 19:13:05 +00:00
update examples
This commit is contained in:
@@ -5,10 +5,14 @@
|
||||
#include <SPI.h>
|
||||
#include "mcp_can.h"
|
||||
|
||||
MCP_CAN CAN(10); // Set CS to pin 10
|
||||
// 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;
|
||||
|
||||
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
|
||||
|
||||
|
||||
unsigned char Flag_Recv = 0;
|
||||
unsigned char flagRecv = 0;
|
||||
unsigned char len = 0;
|
||||
unsigned char buf[8];
|
||||
char str[20];
|
||||
@@ -17,7 +21,7 @@ void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
START_INIT:
|
||||
START_INIT:
|
||||
|
||||
if(CAN_OK == CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
|
||||
{
|
||||
@@ -30,36 +34,39 @@ START_INIT:
|
||||
delay(100);
|
||||
goto START_INIT;
|
||||
}
|
||||
|
||||
|
||||
attachInterrupt(0, MCP2515_ISR, FALLING); // start interrupt
|
||||
}
|
||||
|
||||
void MCP2515_ISR()
|
||||
{
|
||||
Flag_Recv = 1;
|
||||
flagRecv = 1;
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
if(Flag_Recv) { // check if get data
|
||||
|
||||
Flag_Recv = 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 reading a single
|
||||
// message does not clear the IRQ conditon.
|
||||
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");
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
}
|
||||
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 reading a single
|
||||
// message does not clear the IRQ conditon.
|
||||
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");
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
|
||||
Reference in New Issue
Block a user