Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
6 years, 6 months ago.
How do I print out to the serial port, each second, the time starting at Tues Nov 28?
I have a Nucleo-F303RE and want to print out the time to the serial port starting at Tues Nov 28 00:00:00 2017
What is the code that I need to enter into my online compiler?
1 Answer
6 years, 6 months ago.
Hello Henry,
Try to copy and paste the code below into the online compiler and import the Clock library into the project. Then compile.
#include "mbed.h" #include "Clock.h" Serial pc(USBTX, USBRX); Clock rtc; void onClockTick() { time_t t = rtc.time(); pc.printf("Time = %s", ctime(&t)); } int main() { rtc.attach(onClockTick); // Attach a handler function rtc.set(2017, 11, 28, 00, 00, 00); // Set the Clock to 2017, Nov, 28th, 0 hours, 0 minutes, 0 seconds while (1) { } }
You might also have a look at the Clock_Hello example program.
Zoltan, thank you very much for your help.
I, unfortunately, couldn't get your clock programme to work. The aim of the task I have to do is to create a programme that prints the following to the serial port:
Time = Tue Nov 28 00:00:00 2017 Time = Tue Nov 28 00:00:01 2017 Time = Tue Nov 28 00:00:02 2017 Time = Tue Nov 28 00:00:03 2017 Time = Tue Nov 28 00:00:04 2017 etc
posted by 26 May 2018Could you please spell out the issues you have (compilation , run time errors)?
posted by 27 May 2018I can't see any errors. The program compiles correctly, I just don't see the output in CoolTerm. I copied your code and added the clock library as you instructed.
posted by 27 May 2018Close the CoolTerm program, disconnect and then reconnect your MBED board to the PC. Restart the CoolTerm program and click on the Options icon. Select STMicroelectronics STLink Virtual COM Port
as your port. Make sure the Buadrate
is set to 9600
, Data Bits
to 8
, Parity
to none
, Stop Bits
to 1
and no Flow Control
is checked. Save your settings and see how it works.
I only have 2 options within the port dropdown menu - Bluetooth-Incoming-Port and usbmodemFD1313. I running this through an Apple OS, so don't have the ability to install the STMicroelectronics STLink Virtual COM Port
posted by 27 May 2018I have managed to print "Hello World!" to CoolTerm, so I know that they can communicate
posted by 27 May 2018Then select the same serial port as you did when printf("Hello World!")
worked.
I have done that and I didn't get the output. I will try again. Do you use an Instant Messaging platform? As this is a rather cumbersome method of communicating :) I use Skype or Facebook messenger.
posted by 27 May 2018I agree that it's far from being perfect. But on the other hand it could help other people to fix similar issues.
Unfortulately I have no experience with Apple OS. But this Tutorial explains MBED board to PC communication over USB.
NOTE: I have modified the code above (replaced printf
with =pc.printf
) to be in compliance with the that. However, the original version would work as good as the modified one.