|
楼主 |
发表于 2020-10-25 17:20
|
显示全部楼层
本帖最后由 helloword1111 于 2020-10-25 17:23 编辑
#include <SPI.h>
#include "df_can.h"
const int SPI_CS_PIN = 10;
MCPCAN CAN(SPI_CS_PIN); // Set CS pin
unsigned char flagRecv = 0;
unsigned char len = 0;
unsigned char buf[8];
char str[20];
void setup()
{
Serial.begin(115200);
int count = 50; // the max numbers of initializint the CAN-BUS, if initialize failed first!.
do {
CAN.init(); //must initialize the Can interface here!
CAN.init_Mask(MCP_RXM0, 0, 0x3ff); // there are 2 mask in mcp2515, you need to set both of them
CAN.init_Mask(MCP_RXM1, 0, 0x3ff);
/*
* set filter, we can receive id from 0x04 ~ 0x09 except for 0x06
* // there are 6 filter in mcp2515,so it can filter six id,i.e.0x04~0x09.
*/
CAN.init_Filter(MCP_RXF5, 0, 0x09); // filter 5 for id = 0x09
if(CAN_OK == CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
Serial.println("DFROBOT's CAN BUS Shield init ok!");
break;
}
else
{
Serial.println("DFROBOT's CAN BUS Shield init fail");
Serial.println("Please Init CAN BUS Shield again");
delay(100);
if (count <= 1)
Serial.println("Please give up trying!, trying is useless!");
}
}while(count--);
attachInterrupt(0, MCP2515_ISR, FALLING); // start interrupt
/*
* set mask, set both the mask to 0x3ff
*/
}
void MCP2515_ISR()
{
flagRecv = 1;
}
unsigned char data[] = "node 2";
void loop()
{
if(flagRecv) // check if get data
{
flagRecv = 0; // clear flag
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
Serial.println("\r\n------------------------------------------------------------------");
Serial.print("Get Data From id: ");
Serial.println(CAN.getCanId());
for(int i = 0; i<len; i++) // print the data
{
Serial.write(buf);
Serial.print("\t");
}
Serial.println();
}
// send data: id = 0x08, standrad flame, data len = 8, data: data buf
CAN.sendMsgBuf(0x08, 0, sizeof(data), data);
delay(1000); // send data per 100ms
}
你好,这个是接收模块的例程程序,我的就是用着个程序,这个程序的说明是这样的“node 2只接收id 为0x09的数据帧和发送id为0x08且数据为”node 2“的数据帧”
我在实验时发现,无论发什么ID过来,这个接收点都会接收到,所以想问问怎么设置滤波。
完整的资料在这个网站:https://arduino.ncnynl.com/archives/201605/2081.html
|
|