Arduino

아두이노 간 CAN 통신 : MCP2515 LED 사용하기

작성자 임베디드코리아 작성일25-05-16 00:53 조회207회 댓글0건

첨부파일

<* CAN 통신 : MCP2515 LED 사용 *>

---- < CAN 통신 : MCP2515 LED : Master 소스코드 > -------------------
#include <mcp_can.h>
#include <SPI.h>

MCP_CAN CAN0(10);    // Set CS to pin 10

int swPin=5 , ledPin=7;
int Last = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(swPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  // Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
  if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) == CAN_OK) Serial.println("MCP2515 Initialized Successfully!");
  else Serial.println("Error Initializing MCP2515...");

  CAN0.setMode(MCP_NORMAL);  // Change to normal mode to allow messages to be transmitted
}

byte data[8] = {0x01, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};

void loop()
{
  int swPin_Stat = digitalRead(swPin); // 버튼의 상태를 저장합니다.
 
  CAN0.sendMsgBuf(0x100, 0, 1, data);
  if(Last != swPin_Stat) {
    Last = swPin_Stat;
   
    if(swPin_Stat== 1) {
            Serial.println("Button OFF!");
            digitalWrite(ledPin, LOW);
            data[0]=0x00;
            CAN0.sendMsgBuf(0x100, 0, 1, data); // 데이터를 보냅니다:  id = 0x00, standrad flame, 데이터 길이 = 1, 데이터
        }
        else if(swPin_Stat == 0) {
            Serial.println("Button ON!");
            digitalWrite(ledPin, HIGH);
            data[0]=0x01;
            CAN0.sendMsgBuf(0x100, 0, 1, data); // 데이터를 보냅니다:  id = 0x00, standrad flame, 데이터 길이 = 1, 데이터
        }
     
  }
         
  delay(500);  // send data per 100ms
  swPin_Stat=0;
}

---- < CAN 통신 : MCP2515 :  LED Slave 소스 코드 > -------------------
#include <mcp_can.h>
#include <SPI.h>

long unsigned int rxId=0x100;
unsigned char len = 0;
unsigned char rxBuf[8];
char msgString[128];                        // Array to store serial string

#define CAN0_INT  2    // Set Intrrupt 0 to pin 2
#define LEDpin    7    // Set LED to pin 7

MCP_CAN CAN0(10);                              // Set CS to pin 10


void setup()
{
  Serial.begin(9600);
 
  // Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
  if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) == CAN_OK)
    Serial.println("MCP2515 Initialized Successfully!");
  else
    Serial.println("Error Initializing MCP2515...");
 
  CAN0.setMode(MCP_NORMAL);                    // Set operation mode to normal so the MCP2515 sends acks to received data.

  pinMode(CAN0_INT, INPUT);                            // Configuring pin for /INT input
  pinMode(LEDpin, OUTPUT);
 
  Serial.println("MCP2515 Library Receive Example...");
}

void loop()
{
 // if(!digitalRead(CAN0_INT))                        // If CAN0_INT pin is low, read receive buffer
 // {
    Serial.println("MCP2515 Library Receive Example...");
    CAN0.readMsgBuf(&rxId, &len, rxBuf);      // Read data: len = data length, buf = data byte(s)
    if (rxBuf[0] == 1) {
            Serial.println("LED ON!");
            digitalWrite(LEDpin, HIGH);
            delay(1000);
        }
        else if (rxBuf[0] == 0) {
            Serial.println("LED OFF!");
            digitalWrite(LEDpin, LOW);
            delay(1000);
        }
 // }
  delay(1000);
}