pub
Dependencies: CANnucleo mbed-dev
Fork of CANnucleo_Hello by
Diff: main.cpp
- Revision:
- 23:f4682a5ddda6
- Parent:
- 22:7120a0dcc8ee
- Child:
- 24:069287e799cd
diff -r 7120a0dcc8ee -r f4682a5ddda6 main.cpp --- a/main.cpp Thu Dec 01 21:14:59 2016 +0000 +++ b/main.cpp Thu Dec 01 22:01:11 2016 +0000 @@ -19,8 +19,8 @@ * */ -//#define BOARD1 1 // comment out this line when compiling for board #2 -#define TARGET_STM32F103C8T6 1 // uncomment this line when using STM32F103C8T6 boards! +#define BOARD1 1 // comment out this line when compiling for board #2 +//#define TARGET_STM32F103C8T6 1 // uncomment this line when using STM32F103C8T6 boards! #if defined(TARGET_STM32F103C8T6) #include "stm32f103c8t6.h" @@ -57,6 +57,7 @@ Timer timer; int counter = 0; volatile bool msgAvailable = false; +Serial* pc; /** * @brief 'CAN receive-complete' interrup handler. @@ -79,6 +80,7 @@ #if defined(TARGET_STM32F103C8T6) confSysClock(); //Configure system clock (72MHz HSE clock, 48MHz USB clock) #endif + pc = new Serial(PA_2, PA_3); can = new CANnucleo::CAN(PA_11, PA_12); // CAN Rx pin name, CAN Tx pin name can->frequency(1000000); // set bit rate to 1Mbps can->attach(&onMsgReceived); // attach 'CAN receive-complete' interrupt handler @@ -86,10 +88,10 @@ #if defined(BOARD1) led = ON; // turn LED on timer.start(); // start timer - printf("CANnucleo_Hello board #1\r\n"); + pc->printf("CANnucleo_Hello board #1\r\n"); #else led = OFF; // turn LED off - printf("CANnucleo_Hello board #2\r\n"); + pc->printf("CANnucleo_Hello board #2\r\n"); #endif while(1) { @@ -104,27 +106,27 @@ txMsg << ledState; // append second data item (total data lenght must be <= 8 bytes!) led = OFF; // turn LED off if(can->write(txMsg)) // transmit message - printf("CAN message sent\r\n"); + pc->printf("CAN message sent\r\n"); else - printf("Transmission error\r\n"); + pc->printf("Transmission error\r\n"); } if(msgAvailable) { msgAvailable = false; // reset flag for next use can->read(rxMsg); // read message into Rx message storage - printf("CAN message received\r\n"); - printf(" ID = 0x%.3x\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 ="); + pc->printf("CAN message received\r\n"); + pc->printf(" ID = 0x%.3x\r\n", rxMsg.id); + pc->printf(" Type = %d\r\n", rxMsg.type); + pc->printf(" Format = %d\r\n", rxMsg.format); + pc->printf(" Length = %d\r\n", rxMsg.len); + pc->printf(" Data ="); for(int i = 0; i < rxMsg.len; i++) - printf(" %.2x", rxMsg.data[i]); - printf("\r\n"); + pc->printf(" %.2x", rxMsg.data[i]); + pc->printf("\r\n"); // Filtering performed by software: if(rxMsg.id == RX_ID) { // See comments in CANnucleo.cpp for filtering performed by hardware rxMsg >> counter; // extract first data item rxMsg >> ledState; // extract second data item - printf(" counter = %d\r\n", counter); + pc->printf(" counter = %d\r\n", counter); led = ON; // turn LED on timer.start(); // transmission lag }