Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Arduino_mcp2515_canbus-spi_receiver
#include <SPI.h>
#include <mcp_can.h>
#define SPI_CS_PIN 9
#define LED 8
MCP_CAN CAN(SPI_CS_PIN);
void setup()
{
Serial.begin(9600);
pinMode(LED,OUTPUT);
while (CAN_OK != CAN.begin(CAN_500KBPS)) //can bus : baudrate = 500k
{
Serial.println("CAN BUS Shield Baslatma Hatasi");
Serial.println("CAN BUS Shield Tekrar Baslatiliyor...");
delay(100);
}
Serial.println("CAN BUS Shield Baslatmasi Basarili!");
}
void loop()
{
unsigned char len = 0;
unsigned char buf[8]={0,0,0,0,0,0,0,0};
if(CAN_MSGAVAIL == CAN.checkReceive())
{
CAN.readMsgBuf(&len, buf);
unsigned long canId = CAN.getCanId();
Serial.println("-----------------------------");
Serial.print("Verici ID: 0x");
Serial.println(canId, HEX);
for(int i = 0; i<len; i++)
{
Serial.print(buf[i]);
Serial.print("\t");
digitalWrite(LED, buf[0]);
delay(100);
}
Serial.println();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.