Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include "mcp_can.h"
- #define CAN_CS_PIN 10
- MCP_CAN CAN(CAN_CS_PIN);
- void setup() {
- Serial.begin(115200);
- Serial.println("Booting up");
- // uint8_t can_speed = 0;
- uint8_t can_speed = CAN_1000KBPS;
- uint8_t can_status = CAN.begin(can_speed);
- while(can_status != CAN_OK)
- {
- Serial.println("Could not init CAN, retrying...");
- delay(250);
- can_status = CAN.begin(can_speed);
- }
- Serial.println("CAN MCP2515 init ok!");
- //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);
- }
- void ReadCan()
- {
- uint8_t len = 0;
- uint8_t buf[8];
- uint8_t st = CAN.checkReceive();
- //Serial.print("S: "); Serial.println(st);
- if(st != CAN_MSGAVAIL)
- {
- return;
- }
- //Must read message before checking ID and other flags.
- CAN.readMsgBuf(&len, buf);
- Serial.print("ID "); Serial.print(CAN.getCanId(), HEX);
- Serial.print(CAN.isExtendedFrame()?" E L":" s L");
- Serial.print(len); Serial.print(" ");
- for(int i = 0; i<len; i++)
- {
- if(buf[i]<0x10)
- {
- Serial.print("0");
- }
- Serial.print(buf[i], HEX);
- if(i<len-1)
- {
- Serial.print(":");
- }
- }
- Serial.println();
- }
- void loop() {
- ReadCan();
- delay(500);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement