Skip to content

Instantly share code, notes, and snippets.

@ericdes
Created November 29, 2017 10:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericdes/fc63e545a54945f42e2d466f9699c2c2 to your computer and use it in GitHub Desktop.
Save ericdes/fc63e545a54945f42e2d466f9699c2c2 to your computer and use it in GitHub Desktop.
CAN Bus simulator (initial)
#include <mcp_can.h>
#include <SPI.h>
MCP_CAN CAN(10); // Set CS to pin 10
void setup()
{
Serial.begin(115200);
START_INIT:
if (CAN.begin(CAN_500KBPS) == CAN_OK) // init can bus : baudrate = 500k
{
Serial.println("CAN BUS Shield init ok!");
}
else
{
Serial.println("CAN BUS Shield init fail");
Serial.println("Init CAN BUS Shield again");
delay(100);
goto START_INIT;
}
}
void loop()
{
// CAN.sendMsgBuf(INT8U id, INT8U ext, INT8U len, data_buf);
// - id represents where the data comes from.
// - ext represents the status of the frame.‘0’ means standard frame.‘1’ means extended frame.
// - len represents the length of this frame.
// - data_buf is the content of this message.
INT8U txMsg[8] = { 0, 1, 2, 3, 4, 5, 6, 7 }; // Message we're sending
CAN.sendMsgBuf(0x00, 0, 8, txMsg);
delay(100); // Send message every 100ms
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment