Advertisement
pleasedontcode

CAN Setup rev_01

Oct 16th, 2024
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: CAN Setup
  13.     - Source Code NOT compiled for: Arduino Mega
  14.     - Source Code created on: 2024-10-16 07:50:43
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Implement a CAN bus communication system using the */
  21.     /* MCP2515 controller, ensuring reliable message */
  22.     /* transmission and reception via SPI interface with */
  23.     /* Arduino. Utilize digital pin configurations for */
  24.     /* interrupt handling and SPI communication. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <SPI.h>
  29. #include <mcp2515.h>    // https://github.com/autowp/arduino-mcp2515
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34.  
  35. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  36. const uint8_t can_shield_MCP2515_INT_PIN_D2     = 2; // Interrupt pin for MCP2515
  37.  
  38. /***** DEFINITION OF SPI PINS *****/
  39. const uint8_t can_shield_MCP2515_SPI_PIN_MOSI_D51       = 51; // MOSI pin
  40. const uint8_t can_shield_MCP2515_SPI_PIN_MISO_D50       = 50; // MISO pin
  41. const uint8_t can_shield_MCP2515_SPI_PIN_SCLK_D52       = 52; // SCLK pin
  42. const uint8_t can_shield_MCP2515_SPI_PIN_CS_D53     = 53; // CS pin
  43.  
  44. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  45. MCP2515 mcp2515(can_shield_MCP2515_SPI_PIN_CS_D53); // Initialize MCP2515 with CS pin
  46. struct can_frame canMsg; // Structure to hold CAN message
  47.  
  48. void setup(void)
  49. {
  50.     // Set interrupt pin as input with pull-up
  51.     pinMode(can_shield_MCP2515_INT_PIN_D2, INPUT_PULLUP);
  52.  
  53.     // Set CS pin as output
  54.     pinMode(can_shield_MCP2515_SPI_PIN_CS_D53, OUTPUT);
  55.  
  56.     // Start the SPI library
  57.     SPI.begin(); // Initialize SPI
  58.  
  59.     // Initialize MCP2515
  60.     mcp2515.reset(); // Reset the MCP2515
  61.     mcp2515.setBitrate(CAN_125KBPS); // Set bitrate to 125 kbps
  62.     mcp2515.setNormalMode(); // Set MCP2515 to normal mode
  63.  
  64.     Serial.begin(115200); // Initialize Serial communication
  65.     Serial.println("MCP2515 Initialized Successfully");
  66. }
  67.  
  68. void loop(void)
  69. {
  70.     // Check if a message is received
  71.     if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) {
  72.         // Print the received CAN message details
  73.         Serial.print("ID: ");
  74.         Serial.print(canMsg.can_id, HEX); // Print CAN ID in hexadecimal
  75.         Serial.print(" DLC: ");
  76.         Serial.print(canMsg.can_dlc, HEX); // Print Data Length Code in hexadecimal
  77.         Serial.print(" Data: ");
  78.         for (int i = 0; i < canMsg.can_dlc; i++) { // Loop through the data bytes
  79.             Serial.print(canMsg.data[i], HEX); // Print each data byte in hexadecimal
  80.             Serial.print(" ");
  81.         }
  82.         Serial.println(); // New line after printing the message
  83.     }
  84. }
  85.  
  86. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement