Skip to content

Instantly share code, notes, and snippets.

  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save RobolinkAkademi/5652055bed05b760e3cde30205e1ed13 to your computer and use it in GitHub Desktop.
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