Skip to content

Instantly share code, notes, and snippets.

@kenny-macchina
Last active July 6, 2020 16:59
Show Gist options
  • Save kenny-macchina/690d95b6c260b15f794510dccb4950c8 to your computer and use it in GitHub Desktop.
Save kenny-macchina/690d95b6c260b15f794510dccb4950c8 to your computer and use it in GitHub Desktop.
// Required libraries
#include "variant.h"
#include <due_can.h>
//Leave defined if you use native port, comment if using programming port
#define Serial SerialUSB
void setup()
{
Serial.begin(115200);
// Initialize CAN0 and CAN1, Set the proper baud rates here
Can0.begin(CAN_BPS_500K);
Can1.begin(CAN_BPS_500K);
Can0.watchFor();
Can1.watchFor();
pinMode(Button1, INPUT_PULLUP);
pinMode(Button2, INPUT_PULLUP);
pinMode(14, OUTPUT);
pinMode(15, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(14, HIGH);
digitalWrite(15, HIGH);
}
void loop()
{
CAN_FRAME outgoing;
outgoing.id = 0x400;
outgoing.extended = false;
outgoing.priority = 4; //0-15 lower is higher priority
outgoing.data.s0 = 0xFEED;
outgoing.data.byte[2] = 0xDD;
outgoing.data.byte[3] = 0x55;
outgoing.data.high = 0xDEADBEEF;
if(digitalRead(Button1)==LOW)
{
digitalWrite(14, LOW);
if(Can0.sendFrame(outgoing))
{
Serial.println("Sent frame on CAN0");
}
else
{
Serial.println("Failed to send frame on CAN0");
}
do
{
delay(5);
}
while(digitalRead(Button1)==LOW);
delay(500);
digitalWrite(14, HIGH);
}
if(digitalRead(Button2)==LOW)
{
digitalWrite(15, LOW);
if(Can1.sendFrame(outgoing))
{
Serial.println("Sent frame on CAN1");
}
else
{
Serial.println("Failed to send frame on CAN1");
}
do
{
delay(5);
}
while(digitalRead(Button2)==LOW);
delay(500);
digitalWrite(15, HIGH);
}
CAN_FRAME incoming;
if (Can0.available() > 0)
{
Can0.read(incoming);
Serial.println("Received frame on CAN0");
flashLED();
}
if (Can1.available() > 0)
{
Can1.read(incoming);
Serial.println("Received frame on CAN1");
flashLED();
}
}
void flashLED()
{
for (int i=0; i<10; i++)
{
digitalWrite(LED_BUILTIN, LOW);
delay(50);
digitalWrite(LED_BUILTIN, HIGH);
delay(50);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment