#概要
arduinoでcanやってみました。
#写真
#回路図
#uno写真
#nano写真
#サンプルコード
#include <SPI.h>
#include "mcp_can.h"
const int SPI_CS_PIN = 10;
MCP_CAN CAN(SPI_CS_PIN);
void setup()
{
Serial.begin(115200);
START_INIT:
if (CAN_OK == CAN.begin(CAN_500KBPS))
{
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()
{
CAN.sendMsgBuf(0x00, 0, 8, stmp);
delay(100);
}
#サンプルコード
#include <SPI.h>
#include "mcp_can.h"
const int SPI_CS_PIN = 10;
MCP_CAN CAN(SPI_CS_PIN);
void setup()
{
Serial.begin(115200);
START_INIT:
if (CAN_OK == CAN.begin(CAN_500KBPS))
{
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;
}
}
void loop()
{
unsigned char len = 0;
unsigned char buf[8];
if (CAN_MSGAVAIL == CAN.checkReceive())
{
CAN.readMsgBuf(&len, buf);
unsigned char canId = CAN.getCanId();
Serial.println("-----------------------------");
Serial.println("get data from ID: ");
Serial.println(canId);
for (int i = 0; i < len; i++)
{
Serial.print(buf[i]);
Serial.print("\t");
}
Serial.println();
}
}