Skip to content

Added new example sketch - sending periodic frames #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions examples/CAN_periodicFrames/CAN_periodicFrames.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include <SPI.h>
#include <mcp2515.h>

/* Configuration defines */
#define MCP2512_CS_PIN 10
#define MCP2515_CLOCK MCP_8MHZ
#define MCP2515_CAN_SPEED CAN_250KBPS

/* Internal variables */
static unsigned long currentTime;
static unsigned long previousTime;
static MCP2515 mcp2515(MCP2512_CS_PIN);

/* Example frames */
can_frame canMsg1 = { .can_id = 0x100, .can_dlc = 1, .data = { 0x00 } };
can_frame canMsg2 = { .can_id = 0x500, .can_dlc = 8, .data = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07} };


void setup() {
while (!Serial);
Serial.begin(115200);

if(mcp2515.reset() != MCP2515::ERROR_OK)
{
Serial.println("reset error");
}
if(mcp2515.setBitrate(MCP2515_CAN_SPEED, MCP2515_CLOCK) != MCP2515::ERROR_OK)
{
Serial.println("setBitrate error");
}
if(mcp2515.setNormalMode() != MCP2515::ERROR_OK)
{
Serial.println("setNormalMode error");
}
}


static void _100ms_task(void)
{
/* Enter your code here */

mcp2515.sendMessage(&canMsg1);
canMsg1.data[0]++;
}

static void _500ms_task(void)
{
/* Enter your code here */

mcp2515.sendMessage(&canMsg2);
}

static void _1000ms_task(void)
{
/* Enter your code here */
}

static void simpleScheduler(void)
{
currentTime = millis();

if(currentTime > previousTime)
{
previousTime = currentTime;

if(currentTime % 100 == 0)
{
_100ms_task();
}
if(currentTime % 500 == 0)
{
_500ms_task();
}
if(currentTime % 1000 == 0)
{
_1000ms_task();
}
}
}

void loop() {
simpleScheduler();

/* Another non-blocking code */
}