Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Can_sniffer_BMS4 by
Diff: main.cpp
- Revision:
- 2:49c9430860d1
- Parent:
- 1:267d6288df33
- Child:
- 3:464b06c16d24
--- a/main.cpp Sun Jul 19 14:12:34 2015 +0000 +++ b/main.cpp Thu Jul 23 13:11:08 2015 +0000 @@ -1,7 +1,7 @@ /* * An example showing how to use the CANnucleo library: * - * Two NUCLEO boards are connected to the same CAN bus via CAN transceivers (MPC2551 or TJA1040, or etc.). + * Two NUCLEO boards are connected to the same CAN bus via CAN transceivers (MCP2551 or TJA1040, or etc.). * Transceivers are not part of the NUCLEO boards, therefore must be added by you. * Remember also that CAN bus must be terminated with 120 Ohm resitors on both ends. * See <https://developer.mbed.org/users/WiredHome/notebook/can---getting-started/> @@ -36,7 +36,7 @@ CAN can(PA_11, PA_12); // rx, tx CANMessage rxMsg; CANMessage txMsg; -long int counter; +long int counter = 0; volatile bool msgAvailable = false; /** @@ -61,10 +61,10 @@ can.attach(&onMsgReceived, CAN::RxIrq); // attach 'CAN receive complete' interrupt handler timer.reset(); #if defined(BOARD1) - led = 1; - timer.start(); + led = 1; + timer.start(); #else - led = 0; + led = 0; #endif while(1) { @@ -78,15 +78,23 @@ txMsg << led.read(); // append second data item (always make sure that CAN message total data lenght <= 8 bytes!) can.write(txMsg); // transmit message printf("CAN message sent\r\n"); - led = 0; // turn off led + led = 0; // turn off LED } if(msgAvailable) { msgAvailable = false; // reset flag for next use can.read(rxMsg); // read message into Rx message storage - printf("CAN message with ID = %x received\r\n", rxMsg.id); + printf("CAN message received:\r\n"); + printf(" ID = %#x\r\n", rxMsg.id); + printf(" Type = %d\r\n", rxMsg.type); + printf(" Format = %d\r\n", rxMsg.format); + printf(" Length = %d\r\n", rxMsg.len); + printf(" Data ="); + for(int i = 0; i < rxMsg.len; i++) + printf(" %x", rxMsg.data[i]); + printf("\r\n"); if(rxMsg.id == RX_ID) { // if ID matches rxMsg >> counter; // extract first data item - rxMsg >> led; // extract second data item (and set led status) + rxMsg >> led; // extract second data item (and set LED status) printf("counter = %d\r\n", counter); timer.start(); }