by Rob Toulson and Tim Wilmshurst from textbook "Fast and Effective Embedded Systems Design: Applying the ARM mbed"
main.cpp@0:81536a1c322c, 2013-06-16 (annotated)
- Committer:
- robt
- Date:
- Sun Jun 16 15:39:27 2013 +0000
- Revision:
- 0:81536a1c322c
by Rob Toulson and Tim Wilmshurst from textbook "Fast and Effective Embedded Systems Design: Applying the ARM mbed"
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
robt | 0:81536a1c322c | 1 | /* Program Example 13.4: CAN data write – sends an incrementing count value to the CAN bus every second. |
robt | 0:81536a1c322c | 2 | */ |
robt | 0:81536a1c322c | 3 | #include "mbed.h" |
robt | 0:81536a1c322c | 4 | Serial pc(USBTX, USBRX); // tx, rx for Tera Term output |
robt | 0:81536a1c322c | 5 | |
robt | 0:81536a1c322c | 6 | DigitalOut led1(LED1); // status LED |
robt | 0:81536a1c322c | 7 | CAN can1(p30, p29); // CAN interface |
robt | 0:81536a1c322c | 8 | char counter = 0; |
robt | 0:81536a1c322c | 9 | int main() { |
robt | 0:81536a1c322c | 10 | printf("send... "); |
robt | 0:81536a1c322c | 11 | while (1) { |
robt | 0:81536a1c322c | 12 | // send value to CAN bus and monitor return value to check if CAN |
robt | 0:81536a1c322c | 13 | // message was sent successfully. If so display, increment and toggle |
robt | 0:81536a1c322c | 14 | if (can1.write(CANMessage(1, &counter, 1))) { |
robt | 0:81536a1c322c | 15 | pc.printf("Message sent: %d\n", counter); // display |
robt | 0:81536a1c322c | 16 | counter++; // increment |
robt | 0:81536a1c322c | 17 | led1 = !led1; // toggle status LED |
robt | 0:81536a1c322c | 18 | }else{ |
robt | 0:81536a1c322c | 19 | can1.reset(); |
robt | 0:81536a1c322c | 20 | } |
robt | 0:81536a1c322c | 21 | wait(1); |
robt | 0:81536a1c322c | 22 | } |
robt | 0:81536a1c322c | 23 | } |