Codey OnlineBiblioteka komponentów Arduino i ESP32 › CAN-bus module MCP2515 SPI 5V
CAN-bus module MCP2515 SPI 5V
Czujniki

CAN-bus module MCP2515 SPI 5V

Ten moduł opiera się na kontrolerze CAN MCP2515, który komunikuje się z mikrokontrolerem przez SPI i służy do dodania obsługi magistrali CAN. Zwykle jest łączony z zewnętrznym transceiverem CAN na płytce breakout 5V, aby podłączyć się do sieci CAN w pojazdach lub zastosowaniach przemysłowych.

Szerokość: 43.5 mm

Pinout — CAN-bus module MCP2515 SPI 5V

Pin Sygnał Opis
INT interrupt MCP2515 interrupt output to MCU; typically active-low when CAN events occur.
SCK SCK SPI clock input from the microcontroller to the MCP2515.
SI MOSI SPI data input to MCP2515; connect to MCU MOSI.
SO MISO SPI data output from MCP2515; connect to MCU MISO.
CS chip-select SPI chip-select input for the MCP2515; active low.
GND ground Ground reference for logic, CAN transceiver, and power.
VCC power 5V supply input for the MCP2515 CAN module.
CAN-H CAN-H CAN bus high differential line connection.
CAN-L CAN-L CAN bus low differential line connection.
VCC.1 power Alternate 5V power input/output pin for the module.
GND.1 ground Alternate ground connection for the module power header.
CAN-H.1 CAN-H Screw-terminal CAN high differential bus connection.
CAN-L.1 CAN-L Screw-terminal CAN low differential bus connection.

Przykładowy schemat połączeń — CAN-bus module MCP2515 SPI 5V

Z Do Przewód
board:5V x1:VCC red
board:GND x1:GND black
board:D10 x1:CS green
board:D9 x1:INT blue
board:D13 x1:SCK yellow
board:D11 x1:SI purple
board:D12 x1:SO cyan

Przykładowy kod Arduino — CAN-bus module MCP2515 SPI 5V

Ten przykład został napisany i sprawdzony kompilacją dla arduino-uno. Codey Online dostosuje go do każdej innej obsługiwanej płytki.

#include <SPI.h>
#include <mcp_can.h>

const byte CAN_CS_PIN = 10;
const byte CAN_INT_PIN = 9;

MCP_CAN CAN(CAN_CS_PIN);

volatile bool canInterrupt = false;

void onCanInterrupt() {
  canInterrupt = true;
}

void setup() {
  Serial.begin(115200);
  while (!Serial) {
    ;
  }

  pinMode(CAN_INT_PIN, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(CAN_INT_PIN), onCanInterrupt, FALLING);

  Serial.println("Initializing MCP2515 CAN module...");

  // Common 5V MCP2515 modules often use an 8 MHz crystal.
  // If your board has a different oscillator, adjust MCP_8MHZ accordingly.
  byte initResult = CAN.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ);
  if (initResult == CAN_OK) {
    Serial.println("MCP2515 initialized successfully.");
  } else {
    Serial.print("MCP2515 initialization failed, error code: ");
    Serial.println(initResult);
    while (true) {
      delay(1000);
    }
  }

  CAN.setMode(MCP_NORMAL);
  Serial.println("CAN controller set to normal mode.");
  Serial.println("Waiting for CAN frames...");
}

void loop() {
  if (canInterrupt || CAN.checkReceive() == CAN_MSGAVAIL) {
    canInterrupt = false;

    unsigned long rxId = 0;
    byte len = 0;
    byte buf[8];

    // This mcp_can library version uses readMsgBuf() and readMsgBufID() is not available.
    if (CAN.readMsgBuf(&rxId, &len, buf) == CAN_OK) {
      Serial.print("Received CAN frame. ID=0x");
      Serial.print(rxId, HEX);
      Serial.print(" DLC=");
      Serial.print(len);
      Serial.print(" Data=");

      for (byte i = 0; i < len; i++) {
        if (buf[i] < 16) Serial.print('0');
        Serial.print(buf[i], HEX);
        if (i < len - 1) Serial.print(' ');
      }
      Serial.println();
    }
  }
}

Podobne komponenty

18650 Battery Shield with USB-C Charger and 5V Boost Converter

18650 Battery Shield with USB-C Charger and 5V Boost Converter

2.4 inch OLED Display I2C 3.3V

2.4 inch OLED Display I2C 3.3V

3.5 inch TFT LCD Shield

3.5 inch TFT LCD Shield

50kg Load Cell Weight Sensor

50kg Load Cell Weight Sensor

Zbuduj swój projekt Arduino lub ESP32 z pomocą AI

Opisz, co chcesz zbudować. Codey Online napisze kod, narysuje schemat połączeń, skompiluje projekt i wgra go na płytkę bezpośrednio z przeglądarki.

Zacznij za darmo