Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- REVISION 1:
- #include <SPI.h>
- #include <mcp_can.h>
- const int SPI_CS_PIN = 53; // CS pin for MCP2515
- MCP_CAN CAN(SPI_CS_PIN); // Initialize MCP2515
- void setup() {
- Serial.begin(9600);
- // Initialize MCP2515
- if(CAN.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) == CAN_OK) {
- Serial.println("CAN BUS Shield init ok!");
- } else {
- Serial.println("Error initializing CAN BUS Shield");
- while(1);
- }
- }
- void loop() {
- // Create and send CAN messages
- // Message 1
- unsigned char msg1[11] = {0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0xE0, 0x00};
- unsigned char len1 = 11;
- unsigned int canId1 = 0x048;
- CAN.sendMsgBuf(canId1, 0, len1, msg1);
- Serial.println("On Message SENT");
- delay(100);
- //delay(1000);
- //Message 3
- unsigned char msg3[11] = {0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0xe0, 0x00};
- unsigned char len3 = 11;
- unsigned int canId3 = 0x3B3;
- CAN.sendMsgBuf(canId3, 0, len3, msg3);
- unsigned char msg2[11] = {0x88, 0xC0, 0x0C, 0x10, 0x04, 0x00, 0x02, 0x00};
- unsigned char len2 = 11;
- unsigned int canId2 = 0x044;
- CAN.sendMsgBuf(canId2, 0, len2, msg2);
- // Message 4
- unsigned char msg4[11] = {0x00, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x28};
- unsigned char len4 = 11;
- unsigned int canId4 = 0x109;
- CAN.sendMsgBuf(canId4, 0, len4, msg4);
- delay(3000);
- REVISION 2:
- #include <SPI.h>
- #include <mcp_can.h>
- const int SPI_CS_PIN = 53; // CS pin for MCP2515, adjust to your setup
- MCP_CAN CAN(SPI_CS_PIN); // Initialize MCP2515
- const int bluePillLED = 13;
- struct can_messages {
- unsigned long can_id;
- uint8_t can_message[11]; // Adjusted to handle 11 bytes
- };
- static can_messages cMessages[4] = {
- {0x048, {0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0xE0, 0x00}}, // turn on C1MCA
- {0x044, {0x88, 0xC0, 0x0C, 0x10, 0x04, 0x00, 0x02, 0x00}}, // sync4 turn on
- {0x3B3, {0x41, 0x00, 0x00, 0x00, 0x4C, 0x00, 0xE0, 0x00}}, // turn CGEA 1.3 - second option
- {0x109, {0x00, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x28}} // Accessory ON, Gear Park, Speed 0
- };
- uint8_t txDataLen = 11; // Adjusted to 11 bytes
- uint32_t txDly = 100; // ms
- void setup() {
- pinMode(bluePillLED, OUTPUT);
- Serial.begin(9600);
- Serial.println("Initializing MCP2515...");
- // Initialize MCP2515
- if(CAN.begin(MCP_ANY, CAN_500KBPS, MCP_8MHZ) == CAN_OK) {
- Serial.println("CAN BUS Shield init ok!");
- } else {
- Serial.println("Error initializing CAN BUS Shield");
- while(1);
- }
- }
- void loop() {
- static uint32_t last = 0;
- static int cMSG = 0;
- if (millis() / txDly != last) {
- last = millis() / txDly;
- // Send CAN message
- CAN.sendMsgBuf(cMessages[cMSG].can_id, 0, txDataLen, cMessages[cMSG].can_message);
- if (cMSG < (sizeof(cMessages) / sizeof(cMessages[0]))) {
- cMSG++;
- } else {
- cMSG = 0;
- }
- digitalWrite(bluePillLED, !digitalRead(bluePillLED)); // Toggle LED
- }
- }
- ASBUILT:
- ;Block 1
- 7D0G1G1A321040302A6
- 7D0G1G2008902000A6F
- ;Block 2
- 7D0G2G15553010340C6
- 7D0G2G282000000005D
- 7D0G2G30005E1
- ;Block 3
- 7D0G3G10000010300DF
- ;Block 4
- 7D0G4G1010011000AF8
- 7D0G4G20004E1
- ;Block 5
- 7D0G5G1198C198C2E55
- 7D0G5G20EEC
- ;Block 6
- 7D0G6G1805E
- ;Block 7
- 7D0G7G10A003300001C
- 7D0G7G200000EF900E7
- 7D0G7G3005656840011
- 7D0G7G4694B
- ;Block 8
- 7D0G8G10000000001E1
- 7D0G8G20000000000E1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement