logo elektroda
logo elektroda
X
logo elektroda

[ESP32][CAN] MCP2515 hangs up when connected to recuperator

Uzytkowniik 930 2
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 20978085
    Uzytkowniik
    Level 16  
    Hi,
    I am trying to make a device that communicates with a recuperator via CAN bus. I'm using ESP32 and a ready-made MCP2515+TJA1050 module ( module: https://sklep.avt.pl/pl/products/modul-can-bu...-mcp2515-can-spi-tja1050-arduino-174337.html) for this. On the desktop without CAN connection it looks like everything is working. At the output of the module I can see on the oscilloscope the CAN waveform. As soon as I connect the chip to CAN, the chip immediately crashes and does not pass initialisation. As if the program is waiting for something on the SPI. I tried with the 120 ohm terminator connected and disconnected. The recuperator and CAN is working because after connecting another device everything works fine.
    The only thing I can think of is replacing the terminating resistor with a different one and/or different CAN speeds on my device and the recuperator, but the recuperator definitely works on 50kbit/s. I haven't changed the resistor yet. Has anyone encountered this problem? Am I setting the speed correctly in the software? Any suggestions?
    I am using a ready-made library from Adafruit.
    
    /*
     * Adafruit MCP2515 FeatherWing CAN Sender Example
     */
    
    #include <Adafruit_MCP2515.h>
    
    #ifdef ESP8266
       #define CS_PIN    2
    #elif defined(ESP32) && !defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S2) && !defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S3)
       #define CS_PIN    5
    #elif defined(TEENSYDUINO)
       #define CS_PIN    8
    #elif defined(ARDUINO_STM32_FEATHER)
       #define CS_PIN    PC5
    #elif defined(ARDUINO_NRF52832_FEATHER)  /* BSP 0.6.5 and higher! */
       #define CS_PIN    27
    #elif defined(ARDUINO_MAX32620FTHR) || defined(ARDUINO_MAX32630FTHR)
       #define CS_PIN    P3_2
    #elif defined(ARDUINO_ADAFRUIT_FEATHER_RP2040)
       #define CS_PIN    7
    #elif defined(ARDUINO_ADAFRUIT_FEATHER_RP2040_CAN)
       #define CS_PIN    PIN_CAN_CS
    #elif defined(ARDUINO_RASPBERRY_PI_PICO) || defined(ARDUINO_RASPBERRY_PI_PICO_W) // PiCowbell CAN Bus
       #define CS_PIN    20
    #else
        // Anything else, defaults!
       #define CS_PIN    5
    #endif
    
    // Set CAN bus baud rate
    #define CAN_BAUDRATE (50E3)
    
    Adafruit_MCP2515 mcp(CS_PIN);
    
    void setup() {
      Serial.begin(115200);
      //while(!Serial) delay(10);
    
      Serial.println("MCP2515 Sender test!");
    
      if (!mcp.begin(CAN_BAUDRATE)) {
        Serial.println("Error initializing MCP2515.");
        while(1) delay(10);
      }
      Serial.println("MCP2515 chip found");
    }
    
    void loop() {
    
    
      // send extended packet: id is 29 bits, packet can contain up to 8 bytes of data
      Serial.print("Sending extended packet ... ");
    
      Serial.print("a");
      mcp.beginExtendedPacket(0x1F074051,8,false);
      Serial.print("b");
      mcp.write(0x00);
      mcp.write(0x84);
      mcp.write(0x15);
      mcp.write(0x01);
      mcp.write(0x01);
      mcp.write(0x00);
      mcp.write(0x00);
      mcp.write(0x00);
      mcp.endPacket();
    
      Serial.print("1/2 ....");
    
      mcp.beginExtendedPacket(0x1F074051,7,false);
      mcp.write(0x81);
      mcp.write(0x00);
      mcp.write(0x01);
      mcp.write(0x00);
      mcp.write(0x00);
      mcp.write(0x00);
      mcp.write(0x02);
      mcp.endPacket();
    
      Serial.println("done");
    
      delay(5000);
    }
    
    .
    Do you have a problem with Arduino? Ask question. Visit our forum Arduino.
  • ADVERTISEMENT
  • Helpful post
    #2 20979888
    gps79
    Level 36  
    I can see from the pictures of this module that it has an 8MHz quartz, and the library that supports it assumes by default that the chip runs with a 16MHz clock. I suggest before calling the begin() method to set the clock manually if there is an 8MHz quartz on your board:

    mcp.setClockFrequency(8000000);
    if (!mcp.begin(CAN_BAUDRATE)) {
    ...
  • #3 20981593
    Uzytkowniik
    Level 16  
    Thanks. Of course everything works
ADVERTISEMENT