TLMoto
Dependencies: CANnucleo CANnucleo_Hello LTC68041 mbed
Fork of CANnucleo_Hello by
Diff: main.cpp
- Revision:
- 13:e741def3e4ff
- Parent:
- 12:e91e44924194
- Child:
- 14:e12ebd1260b1
diff -r e91e44924194 -r e741def3e4ff main.cpp --- a/main.cpp Sat Jul 23 08:29:31 2016 +0000 +++ b/main.cpp Wed Jul 27 09:16:40 2016 +0000 @@ -1,5 +1,5 @@ /* - * An example showing how to use the CANnucleo library: + * An example of using CAN bus with NUCLEO boards: * * Two affordable (less than $4 on ebay) STM32F103C8T6 boards (20kB SRAM, 64kB Flash), * compatible with the NUCLEO-F103RB platform (20kB SRAM, 128kB Flash), @@ -7,22 +7,21 @@ * CAN transceivers are not part of NUCLEO boards, therefore must be added by you. * Remember also that CAN bus (even a short one) must be terminated with 120 Ohm resitors at both ends. * - * For more details see the wiki page <https://developer.mbed.org/users/hudakz/code/CANnucleo_Hello/> + * For more details see the wiki page [https://developer.mbed.org/users/hudakz/code/CANnucleo_Hello/] * - * NOTE: If you'd like to use the official NUCLEO boards comment out line 25 + * NOTE: If you'd like to use the official NUCLEO boards comment out line 24 * * The same code is used for both NUCLEO boards, but: * For board #1 compile the example without any change. - * For board #2 comment out line 26 before compiling + * For board #2 comment out line 25 before compiling * * Once the binaries have been downloaded to the boards reset board #1. * */ #include "mbed.h" -#include "CANnucleo.h" -//#define TARGET_STM32F103C8T6 1 // comment out this line when using official NUCLEO boards! +#define TARGET_STM32F103C8T6 1 // comment out this line when using official NUCLEO boards! #define BOARD1 1 // comment out this line when compiling for board #2 #if defined(TARGET_STM32F103C8T6) @@ -44,24 +43,11 @@ #endif DigitalOut led(LED_PIN); -int ledState; Timer timer; CAN can(PA_11, PA_12); // CAN Rx pin name, CAN Tx pin name CANMessage rxMsg; CANMessage txMsg; -int counter = 0; -volatile bool msgAvailable = false; - -/** - * @brief 'CAN receive-complete' interrup handler. - * @note Called on arrival of new CAN message. - * Keep it as short as possible. - * @param - * @retval - */ -void onMsgReceived() { - msgAvailable = true; -} +char counter = 0; /** * @brief Main @@ -70,8 +56,6 @@ * @retval */ int main() { - can.frequency(1000000); // set bit rate to 1Mbps - can.attach(&onMsgReceived); // attach 'CAN receive-complete' interrupt handler #if defined(BOARD1) led = ON; // turn LED on @@ -81,24 +65,19 @@ #endif while(1) { - if(timer.read() >= 1.0) { // check for timeout + if(timer.read_ms() >= 200) { // check for timeout timer.stop(); // stop timer timer.reset(); // reset timer counter++; // increment counter - ledState = led.read(); // get led state - txMsg.clear(); // clear Tx message storage txMsg.id = TX_ID; // set ID - txMsg << counter; // append first data item - txMsg << ledState; // append second data item (total data lenght must be <= 8 bytes!) + txMsg.data[0] = counter; // set data led = OFF; // turn LED off if(can.write(txMsg)) // transmit message printf("CAN message sent\r\n"); else printf("Transmission error\r\n"); } - if(msgAvailable) { - msgAvailable = false; // reset flag for next use - can.read(rxMsg); // read message into Rx message storage + if(can.read(rxMsg)) { printf("CAN message received:\r\n"); printf(" ID = %#x\r\n", rxMsg.id); printf(" Type = %d\r\n", rxMsg.type); @@ -108,12 +87,11 @@ for(int i = 0; i < rxMsg.len; i++) printf(" %x", rxMsg.data[i]); printf("\r\n"); - // Filtering performed by software: - if(rxMsg.id == RX_ID) { // See comments in CAN.cpp for filtering performed by hardware - rxMsg >> counter; // extract first data item - rxMsg >> ledState; // extract second data item + + if(rxMsg.id == RX_ID) { + counter = rxMsg.data[0]; // set counter printf("counter = %d\r\n", counter); - led = ledState; // set LED + led = !led; // set LED timer.start(); // transmission lag } }