7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

arduinoでcan

Last updated at Posted at 2016-01-28

#概要
arduinoでcanやってみました。
#写真
can.JPG
#回路図
can1.JPG
#uno写真
MVC-011S.JPG
#nano写真
MVC-012S.JPG
#サンプルコード

#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();
	}
}
7
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?