[136] | 1 | // demo: CAN-BUS Shield, receive data with interrupt mode, and set mask and filter
|
---|
| 2 | //
|
---|
| 3 | // when in interrupt mode, the data coming can't be too fast, must >20ms, or else you can use check mode
|
---|
| 4 | // loovee, 2014-7-8
|
---|
| 5 |
|
---|
| 6 | #include <SPI.h>
|
---|
| 7 | #include "mcp_can.h"
|
---|
| 8 |
|
---|
| 9 | // the cs pin of the version after v1.1 is default to D9
|
---|
| 10 | // v0.9b and v1.0 is default D10
|
---|
| 11 | const int SPI_CS_PIN = 9;
|
---|
| 12 |
|
---|
| 13 | MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
|
---|
| 14 |
|
---|
| 15 |
|
---|
| 16 | unsigned char flagRecv = 0;
|
---|
| 17 | unsigned char len = 0;
|
---|
| 18 | unsigned char buf[8];
|
---|
| 19 | char str[20];
|
---|
| 20 |
|
---|
| 21 | void setup()
|
---|
| 22 | {
|
---|
| 23 | Serial.begin(115200);
|
---|
| 24 |
|
---|
| 25 | START_INIT:
|
---|
| 26 |
|
---|
| 27 | if(CAN_OK == CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
|
---|
| 28 | {
|
---|
| 29 | Serial.println("CAN BUS Shield init ok!");
|
---|
| 30 | }
|
---|
| 31 | else
|
---|
| 32 | {
|
---|
| 33 | Serial.println("CAN BUS Shield init fail");
|
---|
| 34 | Serial.println("Init CAN BUS Shield again");
|
---|
| 35 | delay(100);
|
---|
| 36 | goto START_INIT;
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | attachInterrupt(0, MCP2515_ISR, FALLING); // start interrupt
|
---|
| 40 |
|
---|
| 41 |
|
---|
| 42 | /*
|
---|
| 43 | * set mask, set both the mask to 0x3ff
|
---|
| 44 | */
|
---|
| 45 | CAN.init_Mask(0, 0, 0x3ff); // there are 2 mask in mcp2515, you need to set both of them
|
---|
| 46 | CAN.init_Mask(1, 0, 0x3ff);
|
---|
| 47 |
|
---|
| 48 |
|
---|
| 49 | /*
|
---|
| 50 | * set filter, we can receive id from 0x04 ~ 0x09
|
---|
| 51 | */
|
---|
| 52 | CAN.init_Filt(0, 0, 0x04); // there are 6 filter in mcp2515
|
---|
| 53 | CAN.init_Filt(1, 0, 0x05); // there are 6 filter in mcp2515
|
---|
| 54 |
|
---|
| 55 | CAN.init_Filt(2, 0, 0x06); // there are 6 filter in mcp2515
|
---|
| 56 | CAN.init_Filt(3, 0, 0x07); // there are 6 filter in mcp2515
|
---|
| 57 | CAN.init_Filt(4, 0, 0x08); // there are 6 filter in mcp2515
|
---|
| 58 | CAN.init_Filt(5, 0, 0x09); // there are 6 filter in mcp2515
|
---|
| 59 |
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | void MCP2515_ISR()
|
---|
| 63 | {
|
---|
| 64 | flagRecv = 1;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | void loop()
|
---|
| 68 | {
|
---|
| 69 | if(flagRecv) // check if get data
|
---|
| 70 | {
|
---|
| 71 |
|
---|
| 72 | flagRecv = 0; // clear flag
|
---|
| 73 | CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
|
---|
| 74 |
|
---|
| 75 | Serial.println("\r\n------------------------------------------------------------------");
|
---|
| 76 | Serial.print("Get Data From id: ");
|
---|
| 77 | Serial.println(CAN.getCanId());
|
---|
| 78 | for(int i = 0; i<len; i++) // print the data
|
---|
| 79 | {
|
---|
| 80 | Serial.print("0x");
|
---|
| 81 | Serial.print(buf[i], HEX);
|
---|
| 82 | Serial.print("\t");
|
---|
| 83 | }
|
---|
| 84 | Serial.println();
|
---|
| 85 |
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | /*********************************************************************************************************
|
---|
| 90 | END FILE
|
---|
| 91 | *********************************************************************************************************/
|
---|