EIglesias (Customer) asked a question.

Using the P1AM-100 along with the MKR CANBUS SHIELD, P1AM-ETH, and P1AM-SERIAL, and (x3) P1-04TRS modules

I'm facing challenges while configuring the P1AM-100 Controller with the MKR CANBUS SHIELD, along with the P1AM-ETH, P1AM-SERIAL, and three P1-04TRS modules.

My setup involves integrating CANBUS functionality alongside Ethernet and serial communication modules.

I’ve tried running this configuration with the can.h library, but I haven't been able to achieve the desired outcome. The system fails to initialize the Ethernet, and the CAN is not transmitting the expected messages to the bus.

 

Has anyone encountered similar projects or configurations? Any tips, positive feedback, or advice would be greatly appreciated to help me troubleshoot and improve the setup.


  • FACTS_AdamC (AutomationDirect)

    Since the ETH and CAN are both SPI devices, I would start by setting each of their CS pins high manually.

    In this case that would be pins 5 and 3. Afterwards you can call the init routines and other functions as you'd normally do.

    • EIglesias (Customer)

      1. #include <SPI.h>
      2. #include <Ethernet.h>
      3. #include <CAN.h>
      4. #include <P1AM.h>
      5.  
      6. // Define Chip Select pins for both devices
      7. #define CAN_CS_PIN 3 // Pin for CAN CS
      8. #define ETH_CS_PIN 5 // Pin for Ethernet CS
      9.  
      10. // Define the MAC address for Ethernet
      11. byte macAddress[] = { 0x60, 0x52, 0xD0, 0x0C, 0xBD, 0xAE };
      12.  
      13. void setup() {
      14. // Start Serial Monitor for debugging
      15. Serial.begin(9600);
      16.  
      17. // Initialize the P1AM-100 PLC
      18. Serial.println("Initializing P1AM-100 PLC...");
      19.  
      20.  
      21. // Set both CS pins to HIGH initially (deselect both devices)
      22. pinMode(CAN_CS_PIN, OUTPUT);
      23. pinMode(ETH_CS_PIN, OUTPUT);
      24. digitalWrite(CAN_CS_PIN, HIGH);
      25. digitalWrite(ETH_CS_PIN, HIGH);
      26.  
      27. // Initialize CAN bus
      28. Serial.println("Initializing CAN bus...");
      29. if (!CAN.begin(250E3)) { // 250 kbps CAN baud rate
      30. Serial.println("CAN initialization failed!");
      31. } else {
      32. Serial.println("CAN bus initialized successfully.");
      33. }
      34.  
      35. delay(500); // Delay to ensure SPI bus is stable before starting Ethernet
      36.  
      37. // Initialize Ethernet using a specific CS pin
      38. Serial.println("Initializing Ethernet...");
      39. Ethernet.init(ETH_CS_PIN); // Manually specify the CS pin for Ethernet module
      40. if (Ethernet.begin(macAddress) == 0) {
      41. Serial.println("Ethernet initialization failed.");
      42. } else {
      43. Serial.print("Ethernet initialized successfully. IP Address: ");
      44. Serial.println(Ethernet.localIP());
      45. }
      46.  
      47. P1.init(); // Initialize the P1AM module
      48. delay(500); // Wait to ensure the P1AM initialization is stable
      49. }
      50.  
      51. void loop() {
      52. // Example: Send CAN message with Extended ID
      53. digitalWrite(CAN_CS_PIN, LOW); // Select CAN device
      54. CAN.beginExtendedPacket(0x11FFFFFF); // Start CAN communication with extended ID
      55. CAN.write('H');
      56. CAN.write('e');
      57. CAN.write('l');
      58. CAN.write('l');
      59. CAN.write('o');
      60. CAN.endPacket();
      61. digitalWrite(CAN_CS_PIN, HIGH); // Deselect CAN device
      62.  
      63. delay(1000); // Delay between CAN messages
      64.  
      65. // Example: Send Ethernet data
      66. digitalWrite(ETH_CS_PIN, LOW); // Select Ethernet device
      67. // Ethernet-related code
      68. digitalWrite(ETH_CS_PIN, HIGH); // Deselect Ethernet device
      69.  
      70. // Delay between operations
      71. delay(1000);
      72. }

      I tried implementing your suggestions, but I'm still having trouble getting the communications to work in this configuration. Could you take a look at the attached code and see if you can identify what might be going wrong?

      Expand Post
  • FACTS_AdamC (AutomationDirect)

    Right after you set your CS pins high for ETH and CAN, call P1.init()

     

    Also, do not manually control your CS pins as you have shown. The libraries will handle that for you.

    • EIglesias (Customer)

      I've tried to implement the changes you suggested, but haven't had much success.

      PLC Configuration 04022025

      1. #include <SPI.h>
      2. #include <Ethernet.h>
      3. #include <CAN.h>
      4. #include <P1AM.h>
      5. // Define Chip Select pins for both devices
      6. #define CAN_CS_PIN 3 // Pin for CAN CS
      7. #define ETH_CS_PIN 5 // Pin for Ethernet CS
      8. // Define the MAC address for Ethernet
      9. byte macAddress[] = { 0x60, 0x52, 0xD0, 0x0C, 0xBD, 0xAE };
      10. void setup() {
      11. // Start Serial Monitor for debugging
      12. Serial.begin(9600);
      13. // Initialize the P1AM-100 PLC
      14. Serial.println("Initializing P1AM-100 PLC...");
      15. // Set both CS pins to HIGH initially (deselect both devices)
      16. pinMode(CAN_CS_PIN, OUTPUT);
      17. pinMode(ETH_CS_PIN, OUTPUT);
      18. digitalWrite(CAN_CS_PIN, HIGH);
      19. digitalWrite(ETH_CS_PIN, HIGH);
      20.  
      21. // Initialize the P1AM-100 PLC
      22. Serial.println("Initializing P1AM-100 PLC...");
      23. P1.init();
      24. delay(500); // Wait to ensure the P1AM initialization is stable
      25.  
      26. // Initialize CAN bus
      27. Serial.println("Initializing CAN bus...");
      28. if (!CAN.begin(250E3)) { // 250 kbps CAN baud rate
      29. Serial.println("CAN initialization failed!");
      30. } else {
      31. Serial.println("CAN bus initialized successfully.");
      32. }
      33. delay(500); // Delay to ensure SPI bus is stable before starting Ethernet
      34. // Initialize Ethernet using a specific CS pin
      35. Serial.println("Initializing Ethernet...");
      36. Ethernet.init(ETH_CS_PIN); // Manually specify the CS pin for Ethernet module
      37. if (Ethernet.begin(macAddress) == 0) {
      38. Serial.println("Ethernet initialization failed.");
      39. } else {
      40. Serial.print("Ethernet initialized successfully. IP Address: ");
      41. Serial.println(Ethernet.localIP());
      42. }
      43.  
      44. }
      45. void loop() {
      46. char message[] = {'H', 'E', 'L', 'L', 'O', '\0'}; // Null-terminated character array
      47.  
      48. // Example: Send CAN message with Extended ID
      49. CAN.beginExtendedPacket(0x11FFFFFF); // Start CAN communication with extended ID
      50. // Send each character in the message array
      51. for (int i = 0; message[i] != '\0'; i++) {
      52. CAN.write(message[i]);
      53. }
      54.  
      55. CAN.endPacket();
      56. delay(1000); // Delay
      57. }

       

      Expand Post
      • z28z34man (Customer)

        How are you powering the P1AM. The P1AM itself and the modules on the left can be powered from USB but modules right of the P1AM require either a P1 power supply or to be powered from the terminal on the bottom of the P1AM

      • EIglesias (Customer)

        I am using a DCDC to provide 24VDC to the modules using the power terminal beneath the P1AM-100.

      • FACTS_AdamC (AutomationDirect)

        Is your P1AM library and board package up to date?

      • EIglesias (Customer)

        Yes, the libraries are up to date. The CAN.h library by Sandeep Mistry is version 0.3.1, the Ethernet library is version 2.0.2, and the P1AM library is version 1.0.9. The board is using the P1AM-100 package, version 1.6.21.

  • EIglesias (Customer)

    1. #include <SPI.h>
    2. #include <CAN.h>
    3. #include <P1AM.h>
    4.  
    5. #define CAN_CS_PIN 3 // Pin for CAN CS
    6.  
    7. void setup() {
    8. Serial.begin(115200);
    9. Serial.println("Initializing P1AM-100 PLC...");
    10.  
    11. pinMode(CAN_CS_PIN, OUTPUT);
    12. digitalWrite(CAN_CS_PIN, HIGH);
    13.  
    14. P1.init();
    15. delay(500); // Wait for stable initialization
    16.  
    17. Serial.println("Initializing CAN bus...");
    18. if (!CAN.begin(250E3)) {
    19. Serial.println("CAN initialization failed!");
    20. } else {
    21. Serial.println("CAN bus initialized successfully.");
    22. }
    23. delay(500); // Ensure SPI bus is stable before starting Ethernet
    24.  
    25. }
    26.  
    27. void loop() {
    28.  
    29. CAN.beginExtendedPacket(0x11FFFFFF);
    30.  
    31. uint8_t data[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
    32. // Write each byte individually
    33. for (int i = 0; i < sizeof(data); i++) {
    34. CAN.write(data[i]);
    35. }
    36.  
    37. CAN.endPacket();
    38. delay(1000);
    39.  
    40. }

    Modifying 10E6 to 8E6 in the CAN library successfully initializes the CANBUS, but the messages aren’t being transmitted to the bus. I can observe other device messages on the same CANBUS, indicating that the connection to the analyzer is stable. Using an oscilloscope to inspect the bus, I see no transmission from the CANBUS shield.

    Additionally, the Ethernet module still isn't initializing.

     

    I suspect that the SPI setup, either in the code or the libraries, needs to be adjusted, as the issue affects both the CAN and Ethernet bases. If you have any insights or suggestions regarding the potential causes of these issues, I would greatly appreciate any input.

    Expand Post
  • EIglesias (Customer)

    I noticed that the silkscreen labels for CAN High and CAN Low on the MKR CAN Shield are reversed. I'm not sure if the CAN.h library has a configuration setting to compensate for this, but after swapping the connections manually, everything is now transmitting and receiving correctly.

10 of 11