shuzo-kino.hateblo.jp
の続き
今度は受信側です
実際のところ
何も受信できてないとクラッシュするのでバッファに何か入れられるようになるまでヒアします
#include <M5Stack.h> #include <mcp_can.h> #include <SPI.h> #define CAN0_INT 15 // 割り込みピンを設定 volatile bool messageReceived = false; void CANInterrupt() { messageReceived = true; } const int SPI_CS_PIN = 12; MCP_CAN CAN0(SPI_CS_PIN); byte len = 0; byte buf[8]; void setup() { M5.begin(); M5.Power.begin(); M5.Lcd.println("M5Stack wake up!"); delay(1000); pinMode(CAN0_INT, INPUT); attachInterrupt(digitalPinToInterrupt(CAN0_INT), CANInterrupt, FALLING); if (CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) == CAN_OK) { M5.Lcd.println("Init!"); } else { M5.Lcd.println("Fail!"); } CAN0.setMode(MCP_NORMAL); M5.Lcd.println("CAN BUS init success!"); } void loop() { if (messageReceived) { messageReceived = false; if (CAN0.checkReceive() == CAN_MSGAVAIL) { unsigned long canId = 1; CAN0.readMsgBuf(&canId, &len, buf); // Read data: len = data length, buf = data byte(s M5.Lcd.printf("Receive: ID: 0x%lX, Length: %d, Data: ", canId, len); for (int i = 0; i < len; i++) { M5.Lcd.printf("0x%02X ", buf[i]); } M5.Lcd.println(); } } M5.Lcd.print("."); delay(200); M5.update(); }