Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <mcp_can.h>
- #define CAN0_INT 2 // Set INT to pin 2
- MCP_CAN CAN0(10); // Set CS to pin 53
- void setup() {
- Serial.begin(115200);
- // Initialize MCP2515 CAN controller at the specified baud rate
- if (CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) == CAN_OK) {
- Serial.println("CAN BUS Shield init ok!");
- } else {
- Serial.println("CAN BUS Shield init fail");
- Serial.println(" Init CAN BUS Shield again");
- delay(100);
- while (1);
- }
- // Set receive interrupt
- CAN0.setMode(MCP_NORMAL);
- pinMode(CAN0_INT, INPUT);
- Serial.println("Starting to Receive CAN BUS Message...");
- }
- void loop() {
- long unsigned int rxId;
- unsigned char len = 0;
- unsigned char rxBuf[8];
- // Check if data is coming
- if (!digitalRead(CAN0_INT)) {
- CAN0.readMsgBuf(&rxId, &len, rxBuf);
- Serial.print("CAN ID: ");
- Serial.print(rxId, HEX);
- Serial.print(" Data: ");
- for (int i = 0; i < len; i++) {
- Serial.print(rxBuf[i], HEX);
- Serial.print("\t");
- }
- Serial.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement