mirror of
https://github.com/Seeed-Studio/Seeed_Arduino_CAN.git
synced 2026-07-28 04:06:45 +00:00
add set mask and filter example
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
// demo: CAN-BUS Shield, receive data with interrupt mode, and set mask and filter
|
||||
//
|
||||
// when in interrupt mode, the data coming can't be too fast, must >20ms, or else you can use check mode
|
||||
// loovee, 2014-7-8
|
||||
|
||||
#include <SPI.h>
|
||||
#include "mcp_can.h"
|
||||
|
||||
MCP_CAN CAN(9); // Set CS to pin 10
|
||||
|
||||
|
||||
unsigned char Flag_Recv = 0;
|
||||
unsigned char len = 0;
|
||||
unsigned char buf[8];
|
||||
char str[20];
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
START_INIT:
|
||||
|
||||
if(CAN_OK == CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
|
||||
{
|
||||
Serial.println("CAN BUS Shield init ok!");
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("CAN BUS Shield init fail");
|
||||
Serial.println("Init CAN BUS Shield again");
|
||||
delay(100);
|
||||
goto START_INIT;
|
||||
}
|
||||
|
||||
attachInterrupt(0, MCP2515_ISR, FALLING); // start interrupt
|
||||
|
||||
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
CAN.init_Filt(0, 0, 0x04); // there are 6 filter in mcp2515
|
||||
CAN.init_Filt(1, 0, 0x05); // there are 6 filter in mcp2515
|
||||
|
||||
CAN.init_Filt(2, 0, 0x06); // there are 6 filter in mcp2515
|
||||
CAN.init_Filt(3, 0, 0x07); // there are 6 filter in mcp2515
|
||||
CAN.init_Filt(4, 0, 0x08); // there are 6 filter in mcp2515
|
||||
CAN.init_Filt(5, 0, 0x09); // there are 6 filter in mcp2515
|
||||
|
||||
}
|
||||
|
||||
void MCP2515_ISR()
|
||||
{
|
||||
Flag_Recv = 1;
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
if(Flag_Recv) // check if get data
|
||||
{
|
||||
|
||||
Flag_Recv = 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.print("0x");
|
||||
Serial.print(buf[i], HEX);
|
||||
Serial.print("\t");
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
END FILE
|
||||
*********************************************************************************************************/
|
||||
@@ -0,0 +1,42 @@
|
||||
// demo: set_mask_filter_send
|
||||
// this demo will show you how to use mask and filter
|
||||
|
||||
#include <mcp_can.h>
|
||||
#include <SPI.h>
|
||||
|
||||
MCP_CAN CAN(10); // Set CS to pin 10
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
START_INIT:
|
||||
|
||||
if(CAN_OK == CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
|
||||
{
|
||||
Serial.println("CAN BUS Shield init ok!");
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("CAN BUS Shield init fail");
|
||||
Serial.println("Init CAN BUS Shield again");
|
||||
delay(100);
|
||||
goto START_INIT;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned char stmp[8] = {0, 1, 2, 3, 4, 5, 6, 7};
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
END FILE
|
||||
*********************************************************************************************************/
|
||||
+13
-11
@@ -29,7 +29,7 @@
|
||||
** Function name: mcp2515_reset
|
||||
** Descriptions: reset the device
|
||||
*********************************************************************************************************/
|
||||
void MCP_CAN::mcp2515_reset(void)
|
||||
void MCP_CAN::mcp2515_reset(void)
|
||||
{
|
||||
MCP2515_SELECT();
|
||||
spi_readwrite(MCP_RESET);
|
||||
@@ -271,16 +271,16 @@ void MCP_CAN::mcp2515_initCANBuffers(void)
|
||||
INT32U ulMask = 0x00, ulFilt = 0x00;
|
||||
|
||||
|
||||
mcp2515_write_id(MCP_RXM0SIDH, ext, ulMask); /*Set both masks to 0 */
|
||||
mcp2515_write_id(MCP_RXM1SIDH, ext, ulMask); /*Mask register ignores ext bit */
|
||||
//mcp2515_write_id(MCP_RXM0SIDH, ext, ulMask); /*Set both masks to 0 */
|
||||
//mcp2515_write_id(MCP_RXM1SIDH, ext, ulMask); /*Mask register ignores ext bit */
|
||||
|
||||
/* Set all filters to 0 */
|
||||
mcp2515_write_id(MCP_RXF0SIDH, ext, ulFilt); /* RXB0: extended */
|
||||
mcp2515_write_id(MCP_RXF1SIDH, std, ulFilt); /* RXB1: standard */
|
||||
mcp2515_write_id(MCP_RXF2SIDH, ext, ulFilt); /* RXB2: extended */
|
||||
mcp2515_write_id(MCP_RXF3SIDH, std, ulFilt); /* RXB3: standard */
|
||||
mcp2515_write_id(MCP_RXF4SIDH, ext, ulFilt);
|
||||
mcp2515_write_id(MCP_RXF5SIDH, std, ulFilt);
|
||||
/* Set all filters to 0 */
|
||||
//mcp2515_write_id(MCP_RXF0SIDH, ext, ulFilt); /* RXB0: extended */
|
||||
//mcp2515_write_id(MCP_RXF1SIDH, std, ulFilt); /* RXB1: standard */
|
||||
//mcp2515_write_id(MCP_RXF2SIDH, ext, ulFilt); /* RXB2: extended */
|
||||
//mcp2515_write_id(MCP_RXF3SIDH, std, ulFilt); /* RXB3: standard */
|
||||
//mcp2515_write_id(MCP_RXF4SIDH, ext, ulFilt);
|
||||
//mcp2515_write_id(MCP_RXF5SIDH, std, ulFilt);
|
||||
|
||||
/* Clear, deactivate the three */
|
||||
/* transmit buffers */
|
||||
@@ -658,7 +658,9 @@ INT8U MCP_CAN::setMsg(INT32U id, INT8U ext, INT8U len, INT8U *pData)
|
||||
m_nID = id;
|
||||
m_nDlc = len;
|
||||
for(i = 0; i<MAX_CHAR_IN_MESSAGE; i++)
|
||||
m_nDta[i] = *(pData+i);
|
||||
{
|
||||
m_nDta[i] = *(pData+i);
|
||||
}
|
||||
return MCP2515_OK;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user