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.
Dependencies: SoftPWM MotorSMLAP
Fork of CANnucleo_Hello by
Diff: main.cpp
- Revision:
- 28:dde6c4aef759
- Parent:
- 27:50dcf8aea3ee
- Child:
- 29:9ae558ec888c
--- a/main.cpp Wed Mar 08 19:42:46 2017 +0000
+++ b/main.cpp Sat Mar 11 10:14:45 2017 +0000
@@ -20,7 +20,7 @@
*/
#define BOARD1 1 // comment out this line when compiling for board #2
-//#define TARGET_STM32F103C8T6 1 // uncomment this line when using STM32F103C8T6 boards!
+#define TARGET_STM32F103C8T6 1 // uncomment this line when using STM32F103C8T6 boards!
#if defined(TARGET_STM32F103C8T6)
#include "stm32f103c8t6.h"
@@ -44,21 +44,16 @@
#include "CANnucleo.h"
#include "mbed.h"
-/*
- * To avaoid name collision with the CAN and CANMessage classes built into the mbed library
- * the CANnucleo's CAN and CANMessage classes have been moved into the CANnucleo namespace.
- * Remember to qualify them with the CANnucleo namespace.
- */
-CANnucleo::CAN* can;
-CANnucleo::CANMessage rxMsg;
-CANnucleo::CANMessage txMsg;
-DigitalOut led(LED_PIN);
-Timer timer;
-uint8_t counter = 0;
-AnalogIn analogIn(A0);
-float voltage;
-volatile bool msgAvailable = false;
-Serial* pc;
+Serial pc(PA_2, PA_3);
+CAN* can;
+CANMessage rxMsg;
+CANMessage txMsg;
+DigitalOut led(LED_PIN);
+Timer timer;
+uint8_t counter = 0;
+AnalogIn analogIn(A0);
+float voltage;
+volatile bool msgAvailable = false;
/**
* @brief 'CAN receive-complete' interrup handler.
@@ -77,15 +72,15 @@
* @param CANMessage to print
* @retval none
*/
-void printMsg(CANnucleo::CANMessage& msg) {
- pc->printf(" ID = 0x%.3x\r\n", msg.id);
- pc->printf(" Type = %d\r\n", msg.type);
- pc->printf(" Format = %d\r\n", msg.format);
- pc->printf(" Length = %d\r\n", msg.len);
- pc->printf(" Data =");
+void printMsg(CANMessage& msg) {
+ pc.printf(" ID = 0x%.3x\r\n", msg.id);
+ pc.printf(" Type = %d\r\n", msg.type);
+ pc.printf(" Format = %d\r\n", msg.format);
+ pc.printf(" Length = %d\r\n", msg.len);
+ pc.printf(" Data =");
for(int i = 0; i < msg.len; i++)
- pc->printf(" %.2x", msg.data[i]);
- pc->printf("\r\n");
+ pc.printf(" 0x%.2X", msg.data[i]);
+ pc.printf("\r\n");
}
/**
@@ -98,18 +93,19 @@
#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
+ pc.baud(9600); // set Serial speed
+
+ can = new 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
#if defined(BOARD1)
led = ON; // turn LED on
timer.start(); // start timer
- pc->printf("CANnucleo_Hello board #1\r\n");
+ pc.printf("CANnucleo_Hello board #1\r\n");
#else
led = OFF; // turn LED off
- pc->printf("CANnucleo_Hello board #2\r\n");
+ pc.printf("CANnucleo_Hello board #2\r\n");
#endif
while(1) {
@@ -117,32 +113,33 @@
timer.stop(); // stop timer
timer.reset(); // reset timer
counter++; // increment counter
- voltage = (analogIn * 3.3f)/4096.0f;// read small floating voltage at analog input
+ voltage = (analogIn * 3.3f)/4096.0f;// read the small drifting voltage from analog input
txMsg.clear(); // clear Tx message storage
txMsg.id = TX_ID; // set ID
txMsg << counter << voltage; // append data (total data length must be <= 8 bytes!)
if(can->write(txMsg)) { // transmit message
led = OFF; // turn LED off
- pc->printf("-----------------------------------\r\n");
- pc->printf("CAN message sent\r\n");
+ pc.printf("-------------------------------------\r\n");
+ pc.printf("CAN message sent\r\n");
printMsg(txMsg);
- pc->printf(" counter = %d\r\n", counter);
- pc->printf(" voltage = %e V\r\n", voltage);
+ pc.printf(" counter = %d\r\n", counter);
+ pc.printf(" voltage = %e V\r\n", voltage);
}
else
- pc->printf("Transmission error\r\n");
+ pc.printf("Transmission error\r\n");
}
if(msgAvailable) {
- msgAvailable = false; // reset flag for next use
+ msgAvailable = false; // reset the flag for next use the in interrupt service routine
can->read(rxMsg); // read message into Rx message storage
led = ON; // turn LED on
- pc->printf("CAN message received\r\n");
+ pc.printf("-------------------------------------\r\n");
+ pc.printf("CAN message received\r\n");
printMsg(rxMsg);
// Filtering performed by software:
if(rxMsg.id == RX_ID) { // about filtering performed by hardware see comments in CANnucleo.cpp
rxMsg >> counter >> voltage; // extract data from the received CAN message
- pc->printf(" counter = %d\r\n", counter);
- pc->printf(" voltage = %e V\r\n", voltage);
+ pc.printf(" counter = %d\r\n", counter);
+ pc.printf(" voltage = %e V\r\n", voltage);
timer.start(); // transmission lag
}
}
