This tests the CAN interface between two LISA C027 MBED Boards. By connecting the two boards, pins 1,2,3 respectively on the CAN interface, and loading and executing this code on BOTH boards, CAN messages will be received, and printed, on the PC-USB console interface of both devices.
Dependencies: C027-REVB UbloxUSBModem mbed
Fork of C027_ModemTransparentUSBCDC_revb by
main.cpp@3:048ec5c2dc14, 2013-12-15 (annotated)
- Committer:
- dixter1
- Date:
- Sun Dec 15 21:35:12 2013 +0000
- Revision:
- 3:048ec5c2dc14
- Parent:
- 2:c4ad86683060
Comment update and C027-REVB Library Update.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dixter1 | 0:92ab4f4846f8 | 1 | #include "mbed.h" |
dixter1 | 0:92ab4f4846f8 | 2 | #include "C027.h" |
dixter1 | 0:92ab4f4846f8 | 3 | |
dixter1 | 2:c4ad86683060 | 4 | Ticker ticker; |
dixter1 | 2:c4ad86683060 | 5 | DigitalOut led1(LED); |
dixter1 | 2:c4ad86683060 | 6 | DigitalOut led2(LED1); |
dixter1 | 2:c4ad86683060 | 7 | CAN can1(CANRD, CANTD); |
dixter1 | 2:c4ad86683060 | 8 | Serial pc(USBTX, USBRX); |
dixter1 | 0:92ab4f4846f8 | 9 | |
dixter1 | 2:c4ad86683060 | 10 | char counter = 0; |
dixter1 | 0:92ab4f4846f8 | 11 | |
dixter1 | 3:048ec5c2dc14 | 12 | // this function creates a CAN message with a counter in it |
dixter1 | 3:048ec5c2dc14 | 13 | // and sends it on the CAN interface |
dixter1 | 2:c4ad86683060 | 14 | void send() { |
dixter1 | 2:c4ad86683060 | 15 | if(can1.write(CANMessage(1337, &counter, 1))) { |
dixter1 | 2:c4ad86683060 | 16 | pc.printf("Message sent: %d\r\n", counter); |
dixter1 | 2:c4ad86683060 | 17 | counter++; |
dixter1 | 0:92ab4f4846f8 | 18 | } |
dixter1 | 2:c4ad86683060 | 19 | led1 = !led1; |
dixter1 | 2:c4ad86683060 | 20 | } |
dixter1 | 2:c4ad86683060 | 21 | |
dixter1 | 2:c4ad86683060 | 22 | int main() { |
dixter1 | 2:c4ad86683060 | 23 | |
dixter1 | 2:c4ad86683060 | 24 | DigitalOut can_standby(CANS); |
dixter1 | 2:c4ad86683060 | 25 | |
dixter1 | 2:c4ad86683060 | 26 | // enable the CAN interface |
dixter1 | 2:c4ad86683060 | 27 | // take it out of standby |
dixter1 | 2:c4ad86683060 | 28 | can_standby = 0; |
dixter1 | 2:c4ad86683060 | 29 | |
dixter1 | 3:048ec5c2dc14 | 30 | // specify the baud rate for communications |
dixter1 | 3:048ec5c2dc14 | 31 | // with the serial console to the PC host. |
dixter1 | 0:92ab4f4846f8 | 32 | pc.baud(MDMBAUD); |
dixter1 | 0:92ab4f4846f8 | 33 | |
dixter1 | 2:c4ad86683060 | 34 | pc.printf( "Starting CAN Testing\r\n"); |
dixter1 | 2:c4ad86683060 | 35 | |
dixter1 | 2:c4ad86683060 | 36 | CANMessage msg; |
dixter1 | 2:c4ad86683060 | 37 | while(1) { |
dixter1 | 2:c4ad86683060 | 38 | |
dixter1 | 2:c4ad86683060 | 39 | if(can1.read(msg)) { |
dixter1 | 2:c4ad86683060 | 40 | pc.printf("Message received: %d\r\n", msg.data[0]); |
dixter1 | 2:c4ad86683060 | 41 | led2 = !led2; |
dixter1 | 0:92ab4f4846f8 | 42 | } |
dixter1 | 0:92ab4f4846f8 | 43 | |
dixter1 | 2:c4ad86683060 | 44 | wait(0.2); |
dixter1 | 2:c4ad86683060 | 45 | |
dixter1 | 3:048ec5c2dc14 | 46 | // send a new message. |
dixter1 | 2:c4ad86683060 | 47 | send(); |
dixter1 | 0:92ab4f4846f8 | 48 | } |
dixter1 | 0:92ab4f4846f8 | 49 | } |
dixter1 | 2:c4ad86683060 | 50 | |
dixter1 | 2:c4ad86683060 | 51 |