Infineon Team
Published © MIT

Mastering CAN Communication with Arduino and XMC

Learn how to master CAN communication using Arduino and the KIT_XMC14_2GO.

BeginnerProtip2 hours372
Mastering CAN Communication with Arduino and XMC

Things used in this project

Hardware components

Infineon XMC1400 2GO KIT
Infineon XMC1400 2GO KIT
×2
Jumper wires (generic)
Jumper wires (generic)
×1
OLED Display
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

CAN

Code

Receiver

Arduino
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <CAN.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

void setup() {
  Serial.begin(9600);
  while (!Serial);
  delay(1000);
  Serial.println("CAN Receiver");

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);

  if (!CAN.begin(500000)) {
    Serial.println("Starting CAN failed!");
    while (1);
  }
}

void loop() {
  int packetSize = CAN.parsePacket();

  if (packetSize) {
    display.clearDisplay();
    display.setCursor(0, 0);
    display.print("Received CAN message:");
    display.setCursor(0, 10);
    display.print("ID: 0x");
    display.print(CAN.packetId(), HEX);

    if (CAN.packetExtended()) {
      display.print(" (Extended)");
    } else {
      display.print(" (Standard)");
    }

    display.setCursor(0, 20);
    display.print("Length: ");
    display.print(packetSize);

    if (!CAN.packetRtr()) {
      display.setCursor(0, 30);
      display.print("Data: ");
      for (int i = 0; i < packetSize; i++) {
        display.print((char)CAN.read());
      }
    }

    display.display();

    // Scroll the display to show the full message
    for (int offset = 0; offset <= display.height(); offset++) {
      display.startscrollright(0x00, 0x07);
      delay(100);
    }
    display.stopscroll();
  }
}

Credits

Infineon Team
100 projects • 157 followers
Contact

Comments

Please log in or sign up to comment.