Created
October 8, 2019 11:55
Star
You must be signed in to star a gist
Arduino_mcp2515_canbus-spi_receiver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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