Additional CAN example

Committer:
aashishc1988
Date:
Thu Oct 11 16:02:16 2018 +0000
Revision:
2:d6cbcdb04ada
Parent:
1:5791101761f9
The example was not compiling due to incorrect pinnames. Also, the example should error out properly if the board doesnt support CAN.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mab5449 0:06f428d689e4 1 #include "mbed.h"
mab5449 0:06f428d689e4 2
aashishc1988 2:d6cbcdb04ada 3 #if defined (DEVICE_CAN) || defined(DOXYGEN_ONLY)
aashishc1988 2:d6cbcdb04ada 4
mab5449 0:06f428d689e4 5 Ticker ticker;
mab5449 0:06f428d689e4 6 DigitalOut led1(LED1);
mab5449 0:06f428d689e4 7 DigitalOut led2(LED2);
aashishc1988 2:d6cbcdb04ada 8 /** The constructor takes in RX, and TX pin respectively.
aashishc1988 2:d6cbcdb04ada 9 * These pins, for this example, are defined in mbed_app.json
aashishc1988 2:d6cbcdb04ada 10 */
mab5449 1:5791101761f9 11 CAN can1(MBED_CONF_APP_CAN1_RD, MBED_CONF_APP_CAN1_TD);
mab5449 1:5791101761f9 12 CAN can2(MBED_CONF_APP_CAN2_RD, MBED_CONF_APP_CAN2_TD);
mab5449 0:06f428d689e4 13 char counter = 0;
mab5449 0:06f428d689e4 14
mab5449 0:06f428d689e4 15 void send() {
mab5449 0:06f428d689e4 16 printf("send()\n");
mab5449 1:5791101761f9 17 if(can1.write(CANMessage(1337, &counter, 1))) {
mab5449 0:06f428d689e4 18 printf("wloop()\n");
mab5449 0:06f428d689e4 19 counter++;
mab5449 0:06f428d689e4 20 printf("Message sent: %d\n", counter);
mab5449 0:06f428d689e4 21 }
mab5449 0:06f428d689e4 22 led1 = !led1;
mab5449 0:06f428d689e4 23 }
mab5449 0:06f428d689e4 24
mab5449 0:06f428d689e4 25 int main() {
mab5449 0:06f428d689e4 26 printf("main()\n");
mab5449 1:5791101761f9 27 ticker.attach(&send, 1);
mab5449 0:06f428d689e4 28 CANMessage msg;
mab5449 0:06f428d689e4 29 while(1) {
mab5449 0:06f428d689e4 30 printf("loop()\n");
mab5449 0:06f428d689e4 31 if(can2.read(msg)) {
mab5449 0:06f428d689e4 32 printf("Message received: %d\n", msg.data[0]);
mab5449 0:06f428d689e4 33 led2 = !led2;
mab5449 0:06f428d689e4 34 }
mab5449 0:06f428d689e4 35 wait(0.2);
mab5449 0:06f428d689e4 36 }
mab5449 0:06f428d689e4 37 }
aashishc1988 2:d6cbcdb04ada 38
aashishc1988 2:d6cbcdb04ada 39 #else
aashishc1988 2:d6cbcdb04ada 40 #error CAN NOT SUPPORTED
aashishc1988 2:d6cbcdb04ada 41
aashishc1988 2:d6cbcdb04ada 42 #endif