Advertisement
Guest User

SYNC3ARDUINO

a guest
Mar 10th, 2024
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. REVISION 1:
  2.  
  3. #include <SPI.h>
  4. #include <mcp_can.h>
  5.  
  6. const int SPI_CS_PIN = 53; // CS pin for MCP2515
  7. MCP_CAN CAN(SPI_CS_PIN); // Initialize MCP2515
  8.  
  9. void setup() {
  10. Serial.begin(9600);
  11.  
  12. // Initialize MCP2515
  13. if(CAN.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) == CAN_OK) {
  14. Serial.println("CAN BUS Shield init ok!");
  15. } else {
  16. Serial.println("Error initializing CAN BUS Shield");
  17. while(1);
  18. }
  19. }
  20.  
  21. void loop() {
  22. // Create and send CAN messages
  23.  
  24. // Message 1
  25. unsigned char msg1[11] = {0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0xE0, 0x00};
  26. unsigned char len1 = 11;
  27. unsigned int canId1 = 0x048;
  28. CAN.sendMsgBuf(canId1, 0, len1, msg1);
  29. Serial.println("On Message SENT");
  30.  
  31. delay(100);
  32.  
  33. //delay(1000);
  34.  
  35. //Message 3
  36. unsigned char msg3[11] = {0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0xe0, 0x00};
  37. unsigned char len3 = 11;
  38. unsigned int canId3 = 0x3B3;
  39. CAN.sendMsgBuf(canId3, 0, len3, msg3);
  40.  
  41.  
  42.  
  43.  
  44. unsigned char msg2[11] = {0x88, 0xC0, 0x0C, 0x10, 0x04, 0x00, 0x02, 0x00};
  45. unsigned char len2 = 11;
  46. unsigned int canId2 = 0x044;
  47. CAN.sendMsgBuf(canId2, 0, len2, msg2);
  48.  
  49. // Message 4
  50. unsigned char msg4[11] = {0x00, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x28};
  51. unsigned char len4 = 11;
  52. unsigned int canId4 = 0x109;
  53. CAN.sendMsgBuf(canId4, 0, len4, msg4);
  54.  
  55. delay(3000);
  56.  
  57.  
  58. REVISION 2:
  59.  
  60. #include <SPI.h>
  61. #include <mcp_can.h>
  62.  
  63. const int SPI_CS_PIN = 53; // CS pin for MCP2515, adjust to your setup
  64. MCP_CAN CAN(SPI_CS_PIN); // Initialize MCP2515
  65.  
  66. const int bluePillLED = 13;
  67.  
  68. struct can_messages {
  69. unsigned long can_id;
  70. uint8_t can_message[11]; // Adjusted to handle 11 bytes
  71. };
  72.  
  73. static can_messages cMessages[4] = {
  74. {0x048, {0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0xE0, 0x00}}, // turn on C1MCA
  75. {0x044, {0x88, 0xC0, 0x0C, 0x10, 0x04, 0x00, 0x02, 0x00}}, // sync4 turn on
  76. {0x3B3, {0x41, 0x00, 0x00, 0x00, 0x4C, 0x00, 0xE0, 0x00}}, // turn CGEA 1.3 - second option
  77. {0x109, {0x00, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x28}} // Accessory ON, Gear Park, Speed 0
  78. };
  79.  
  80. uint8_t txDataLen = 11; // Adjusted to 11 bytes
  81. uint32_t txDly = 100; // ms
  82.  
  83. void setup() {
  84. pinMode(bluePillLED, OUTPUT);
  85.  
  86. Serial.begin(9600);
  87. Serial.println("Initializing MCP2515...");
  88.  
  89. // Initialize MCP2515
  90. if(CAN.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) == CAN_OK) {
  91. Serial.println("CAN BUS Shield init ok!");
  92. } else {
  93. Serial.println("Error initializing CAN BUS Shield");
  94. while(1);
  95. }
  96. }
  97.  
  98. void loop() {
  99. static uint32_t last = 0;
  100. static int cMSG = 0;
  101.  
  102. if (millis() / txDly != last) {
  103. last = millis() / txDly;
  104.  
  105. // Send CAN message
  106. CAN.sendMsgBuf(cMessages[cMSG].can_id, 0, txDataLen, cMessages[cMSG].can_message);
  107.  
  108. if (cMSG < (sizeof(cMessages) / sizeof(cMessages[0]))) {
  109. cMSG++;
  110. } else {
  111. cMSG = 0;
  112. }
  113.  
  114. digitalWrite(bluePillLED, !digitalRead(bluePillLED)); // Toggle LED
  115. }
  116. }
  117.  
  118. ASBUILT:
  119.  
  120. ;Block 1
  121. 7D0G1G1A321040302A6
  122. 7D0G1G2008902000A6F
  123. ;Block 2
  124. 7D0G2G15553010340C6
  125. 7D0G2G282000000005D
  126. 7D0G2G30005E1
  127. ;Block 3
  128. 7D0G3G10000010300DF
  129. ;Block 4
  130. 7D0G4G1010011000AF8
  131. 7D0G4G20004E1
  132. ;Block 5
  133. 7D0G5G1198C198C2E55
  134. 7D0G5G20EEC
  135. ;Block 6
  136. 7D0G6G1805E
  137. ;Block 7
  138. 7D0G7G10A003300001C
  139. 7D0G7G200000EF900E7
  140. 7D0G7G3005656840011
  141. 7D0G7G4694B
  142. ;Block 8
  143. 7D0G8G10000000001E1
  144. 7D0G8G20000000000E1
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement