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.
main.cpp
00001 #include "mbed.h" 00002 00003 Serial serial(USBTX,USBRX); 00004 00005 void serialISR(); // ISR that is called when serial data is received 00006 void setTime(); // function to set the UNIX time 00007 int setTimeFlag = 0; // flag for ISR 00008 00009 char rxString[16]; // buffer to store received string 00010 00011 int main() 00012 { 00013 serial.attach(&serialISR); // attach serial ISR 00014 char buffer[30]; // buffer used to store time string 00015 00016 set_time(0); // initialise time to 1st January 1970 00017 00018 while(1) { 00019 00020 time_t seconds = time(NULL); // get current time 00021 // format time into a string (time and date) 00022 strftime(buffer, 30 , "%X %D", localtime(&seconds)); 00023 // print over serial 00024 serial.printf("Time = %s\n",buffer); 00025 wait(1.0); // delay for a second 00026 00027 if (setTimeFlag) { // if updated time has been sent 00028 setTimeFlag = 0; // clear flag 00029 setTime(); // update time 00030 } 00031 } 00032 00033 } 00034 00035 void setTime() { 00036 // print time for debugging 00037 serial.printf("set_time - %s",rxString); 00038 // atoi() converts a string to an integer 00039 int time = atoi(rxString); 00040 // update the time 00041 set_time(time); 00042 } 00043 00044 void serialISR() { 00045 // when a serial interrupt occurs, read rx string into buffer 00046 serial.gets(rxString,16); 00047 // set flag 00048 setTimeFlag = 1; 00049 }
Generated on Wed Aug 3 2022 07:35:53 by
1.7.2