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: CANnucleo
Diff: main.cpp
- Revision:
- 15:6449443e2207
- Parent:
- 14:e12ebd1260b1
- Child:
- 16:a86f339d1c25
diff -r e12ebd1260b1 -r 6449443e2207 main.cpp
--- a/main.cpp Wed Jul 27 10:25:42 2016 +0000
+++ b/main.cpp Fri Jul 29 10:24:45 2016 +0000
@@ -1,8 +1,7 @@
/*
* 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),
+ * Two affordable (less than $3 on ebay) STM32F103C8T6 boards compatible with the NUCLEO-F103RB platform
* are connected to the same CAN bus via transceivers (MCP2551 or TJA1040, or etc.).
* 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.
@@ -19,6 +18,7 @@
*
*/
+
#include "mbed.h"
//#define TARGET_STM32F103C8T6 1 // comment out this line when using official NUCLEO boards!
@@ -44,7 +44,7 @@
DigitalOut led(LED_PIN);
Timer timer;
-CAN can(PA_11, PA_12); // CAN Rx pin name, CAN Tx pin name
+CAN can(PA_11, PA_12); // CAN RD pin name, CAN TD pin name
CANMessage rxMsg;
char counter = 0;
@@ -56,20 +56,22 @@
led = OFF; // turn LED off
#endif
+ can.frequency(1000000); // set bit rate to 1Mbps
+
while(1) {
- if(timer.read_ms() >= 200) { // check for timeout
+ if(timer.read_ms() >= 1000) { // check for timeout
timer.stop(); // stop timer
timer.reset(); // reset timer
counter++; // increment counter
led = OFF; // turn LED off
if(can.write(CANMessage(TX_ID, &counter, 1))) // transmit message
- printf("CAN message sent\r\n");
+ printf("CAN message sent\r\n\r\n");
else
- printf("Transmission error\r\n");
+ printf("Transmission error\r\n\r\n");
}
- if(can.read(rxMsg)) {
- printf("CAN message received:\r\n");
- printf(" ID = %#x\r\n", rxMsg.id);
+ if(can.read(rxMsg)) { // check for new message
+ printf("CAN message received:\r\n"); // print message details
+ 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);
@@ -80,8 +82,8 @@
if(rxMsg.id == RX_ID) {
counter = rxMsg.data[0]; // set counter
- printf("counter = %d\r\n", counter); // print counter
- led = ON; // set LED
+ printf("counter = %d\r\n\r\n", counter); // print counter
+ led = ON; // turn LED on
timer.start(); // transmission lag
}
}