CabBus simple example using CanBus Shield V01

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
Arkadi
Date:
Mon Jan 07 15:24:52 2019 +0000
Commit message:
Simple can bus example using CanBus shield v01 and stm32F303k8

Changed in this revision

can_example.txt Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/can_example.txt	Mon Jan 07 15:24:52 2019 +0000
@@ -0,0 +1,32 @@
+#include "mbed.h"
+
+
+Ticker ticker;
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+//The constructor takes in RX, and TX pin respectively.
+//These pins, for this example, are defined in mbed_app.json
+CAN can1(MBED_CONF_APP_CAN1_RD, MBED_CONF_APP_CAN1_TD);
+CAN can2(MBED_CONF_APP_CAN2_RD, MBED_CONF_APP_CAN2_TD);
+
+char counter = 0;
+
+void send() {
+    if(can1.write(CANMessage(1337, &counter, 1))) {
+        printf("Message sent: %d\n", counter);
+        counter++;
+    }
+    led1 = !led1;
+}
+
+int main() {
+    ticker.attach(&send, 1);
+   CANMessage msg;
+    while(1) {
+        if(can2.read(msg)) {
+            printf("Message received: %d\n\n", msg.data[0]);
+            led2 = !led2;
+        }
+        wait(0.2);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Jan 07 15:24:52 2019 +0000
@@ -0,0 +1,29 @@
+#include "mbed.h"
+
+Ticker ticker;
+DigitalOut led1(LED1);
+//The constructor takes in RX, and TX pin respectively.
+CAN can1(D10, D2);
+
+char counter = 0;
+
+void send() {
+    if(can1.write(CANMessage(1337, &counter, 1))) {
+        printf("Message sent: %d\r\n", counter);
+        counter++;
+    }
+    //led1 = !led1;
+}
+
+int main() {
+   ticker.attach(&send, 1);
+   CANMessage msg;
+    while(1) {
+        if(can1.read(msg)) {
+            printf("Message received: %d\r\n", msg.data[0]);
+            led1 = !led1;
+        }
+        wait(0.1);
+        
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon Jan 07 15:24:52 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/3a7713b1edbc
\ No newline at end of file