![](/media/cache/group/mbed-logo-square-512_3.png.50x50_q85.png)
Additional CAN example
main.cpp@0:06f428d689e4, 2017-01-19 (annotated)
- Committer:
- mab5449
- Date:
- Thu Jan 19 14:21:14 2017 -0600
- Revision:
- 0:06f428d689e4
- Child:
- 1:5791101761f9
Initial commit. Ported code snippets to mbed OS 5 projects
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mab5449 | 0:06f428d689e4 | 1 | #include "mbed.h" |
mab5449 | 0:06f428d689e4 | 2 | |
mab5449 | 0:06f428d689e4 | 3 | Ticker ticker; |
mab5449 | 0:06f428d689e4 | 4 | DigitalOut led1(LED1); |
mab5449 | 0:06f428d689e4 | 5 | DigitalOut led2(LED2); |
mab5449 | 0:06f428d689e4 | 6 | CAN can1(p9, p10); |
mab5449 | 0:06f428d689e4 | 7 | CAN can2(p30, p29); |
mab5449 | 0:06f428d689e4 | 8 | char counter = 0; |
mab5449 | 0:06f428d689e4 | 9 | |
mab5449 | 0:06f428d689e4 | 10 | void send() { |
mab5449 | 0:06f428d689e4 | 11 | printf("send()\n"); |
mab5449 | 0:06f428d689e4 | 12 | if(can1.write(CANMessage(1337, &counter;, 1))) { |
mab5449 | 0:06f428d689e4 | 13 | printf("wloop()\n"); |
mab5449 | 0:06f428d689e4 | 14 | counter++; |
mab5449 | 0:06f428d689e4 | 15 | printf("Message sent: %d\n", counter); |
mab5449 | 0:06f428d689e4 | 16 | } |
mab5449 | 0:06f428d689e4 | 17 | led1 = !led1; |
mab5449 | 0:06f428d689e4 | 18 | } |
mab5449 | 0:06f428d689e4 | 19 | |
mab5449 | 0:06f428d689e4 | 20 | int main() { |
mab5449 | 0:06f428d689e4 | 21 | printf("main()\n"); |
mab5449 | 0:06f428d689e4 | 22 | ticker.attach(&send;, 1); |
mab5449 | 0:06f428d689e4 | 23 | CANMessage msg; |
mab5449 | 0:06f428d689e4 | 24 | while(1) { |
mab5449 | 0:06f428d689e4 | 25 | printf("loop()\n"); |
mab5449 | 0:06f428d689e4 | 26 | if(can2.read(msg)) { |
mab5449 | 0:06f428d689e4 | 27 | printf("Message received: %d\n", msg.data[0]); |
mab5449 | 0:06f428d689e4 | 28 | led2 = !led2; |
mab5449 | 0:06f428d689e4 | 29 | } |
mab5449 | 0:06f428d689e4 | 30 | wait(0.2); |
mab5449 | 0:06f428d689e4 | 31 | } |
mab5449 | 0:06f428d689e4 | 32 | } |