From f245065179976f3c1f265a462a485504598c6f2b Mon Sep 17 00:00:00 2001 From: stevstrong Date: Sat, 9 Dec 2017 20:51:09 +0100 Subject: [PATCH 1/6] Update BluePill-RTClock-test.ino - use build time for RTC - reworked serial monitor output to use sprintf - some fixes + cosmetics --- .../BluePill-RTClock-test.ino | 99 +++++++++++++------ 1 file changed, 67 insertions(+), 32 deletions(-) diff --git a/STM32F1/libraries/RTClock/examples/BluePill-RTClock-test/BluePill-RTClock-test.ino b/STM32F1/libraries/RTClock/examples/BluePill-RTClock-test/BluePill-RTClock-test.ino index d49025a..bf133c2 100644 --- a/STM32F1/libraries/RTClock/examples/BluePill-RTClock-test/BluePill-RTClock-test.ino +++ b/STM32F1/libraries/RTClock/examples/BluePill-RTClock-test/BluePill-RTClock-test.ino @@ -6,7 +6,7 @@ 1. Blink on PC13 per 4s or 7s by attachAlarmInterrupt for 10 times 2. Second counter by attachSecondsInterrpt 3. Serial output on(41s) or off(21s) by creatAlarm - 4. change to your timezone in the sketch; . + 4. change to your timezone in the sketch; 3. get Unix epoch time from https://www.epochconverter.com/ ; 4. last step input the 10 bits number( example: 1503945555) to Serialport ; 5. the clock will be reset to you wanted. @@ -28,10 +28,8 @@ RTClock rtclock (RTCSEL_LSE); // initialise int timezone = 8; // change to your timezone -time_t tt; -time_t tt1; -tm_t mtt = { 47, 9, 13, 3, 11, 22, 30, 30 }; // init time 47+1970 = 2017 Unix epoch Time counted from 00:00:00 1 Jan 1970 -char weekday1[][7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; // 0,1,2,3,4,5,6 +time_t tt, tt1; +tm_t mtt; uint8_t dateread[11]; int globAlmCount = 0; int lastGlobAlmCount; @@ -42,40 +40,87 @@ int alarmcount = 3; uint8_t AlarmExchange = 0; bool dispflag = true; -#define LED_PIN PC13 - +//----------------------------------------------------------------------------- +const char * weekdays[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; +const char * months[] = {"Dummy", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; +//----------------------------------------------------------------------------- +uint8_t str2month(const char * d) +{ + uint8_t i = 13; + while ( (--i) && strcmp(months[i], d)!=0 ); + return i; +} +//----------------------------------------------------------------------------- +const char * delim = " :"; +char s[128]; // for sprintf +//----------------------------------------------------------------------------- +void ParseBuildTimestamp(tm_t & mt) +{ + // Timestamp format: "Dec 8 2017, 22:57:54" + sprintf(s, "Timestamp: %s, %s\n", __DATE__, __TIME__); + //Serial.print(s); + char * token = strtok(s, delim); // get first token + // walk through tokens + while( token != NULL ) { + if ( (uint8_t m = str2month((const char*)token))>0 ) { + mt.month = m; + //Serial.print(" month: "); Serial.println(mt.month); + token = strtok(NULL, delim); // get next token + mt.day = atoi(token); + //Serial.print(" day: "); Serial.println(mt.day); + token = strtok(NULL, delim); // get next token + mt.year = atoi(token) - 1970; + //Serial.print(" year: "); Serial.println(mt.year); + token = strtok(NULL, delim); // get next token + mt.hour = atoi(token); + //Serial.print(" hour: "); Serial.println(mt.hour); + token = strtok(NULL, delim); // get next token + mt.minute = atoi(token); + //Serial.print(" minute: "); Serial.println(mt.minute); + token = strtok(NULL, delim); // get next token + mt.second = atoi(token); + //Serial.print(" second: "); Serial.println(mt.second); + } + token = strtok(NULL, delim); + } +} +//----------------------------------------------------------------------------- // This function is called in the attachSecondsInterrpt +//----------------------------------------------------------------------------- void SecondCount () { tt++; } +//----------------------------------------------------------------------------- // This function is called in the attachAlarmInterrpt +//----------------------------------------------------------------------------- void blink () { - digitalWrite(LED_PIN, !digitalRead(LED_PIN)); + digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); globAlmCount++; - //tt++; } - +//----------------------------------------------------------------------------- void OnOffSerial () { dispflag = !dispflag; SPECAlmCount++; } - +//----------------------------------------------------------------------------- void setup() { lastGlobAlmCount = ~globAlmCount; lastSPECAlmCount = ~SPECAlmCount; Serial.begin(115200); - pinMode(LED_PIN, OUTPUT); - tt = rtclock.makeTime(mtt); - rtclock.setTime(tt); + pinMode(LED_BUILTIN, OUTPUT); + //while (!Serial); delay(1000); + ParseBuildTimestamp(mtt); // get the Unix epoch Time counted from 00:00:00 1 Jan 1970 + tt = rtclock.makeTime(mtt); + rtclock.setTime(tt+25); // additional seconds to compensate build and upload delay tt1 = tt; rtclock.attachAlarmInterrupt(blink);// Call blink rtclock.attachSecondsInterrupt(SecondCount);// Call SecondCount } - +//----------------------------------------------------------------------------- void loop() { while (Serial.available()) @@ -91,7 +136,7 @@ void loop() rtclock.setTime(rtclock.TimeZone(tt, timezone)); } } - if (lastGlobAlmCount != globAlmCount | lastSPECAlmCount != SPECAlmCount ) { + if (lastGlobAlmCount != globAlmCount || lastSPECAlmCount != SPECAlmCount ) { if (globAlmCount == 10) { // to detachAlarmInterrupt and start creatAlarm after 10 times about 110s rtclock.detachAlarmInterrupt(); globAlmCount = 0; @@ -117,23 +162,13 @@ void loop() } } } - if (tt1 != tt & dispflag == true ) + if (tt1 != tt && dispflag == true ) { tt1 = tt; - //rtclock.breakTime(tt, mtt); - Serial.print("Date: "); - Serial.print(rtclock.day()); - Serial.print("- "); - Serial.print(rtclock.month()); - Serial.print(" "); - Serial.print(rtclock.year() + 1970); - Serial.print(" "); - Serial.print(weekday1[rtclock.weekday()]); - Serial.print(" Time: "); - Serial.print(rtclock.hour()); - Serial.print(" : "); - Serial.print(rtclock.minute()); - Serial.print(" : "); - Serial.println(rtclock.second()); + // get and print actual RTC timestamp + rtclock.breakTime(rtclock.now(), mtt); + sprintf(s, "RTC timestamp: %s %u %u, %s, %02u:%02u:%02u\n", + months[mtt.month], mtt.day, mtt.year+1970, weekdays[mtt.weekday], mtt.hour, mtt.minute, mtt.second); + Serial.print(s); } } From e70309ae78d9c84ffec88a1554c3a0daa015225c Mon Sep 17 00:00:00 2001 From: stevstrong Date: Sat, 9 Dec 2017 20:56:48 +0100 Subject: [PATCH 2/6] Update BluePill-RTClock-test.ino - tabs replaced by spaces for a better visual text alignment --- .../BluePill-RTClock-test.ino | 66 +++++++++---------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/STM32F1/libraries/RTClock/examples/BluePill-RTClock-test/BluePill-RTClock-test.ino b/STM32F1/libraries/RTClock/examples/BluePill-RTClock-test/BluePill-RTClock-test.ino index bf133c2..87213b7 100644 --- a/STM32F1/libraries/RTClock/examples/BluePill-RTClock-test/BluePill-RTClock-test.ino +++ b/STM32F1/libraries/RTClock/examples/BluePill-RTClock-test/BluePill-RTClock-test.ino @@ -46,9 +46,9 @@ const char * months[] = {"Dummy", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul //----------------------------------------------------------------------------- uint8_t str2month(const char * d) { - uint8_t i = 13; - while ( (--i) && strcmp(months[i], d)!=0 ); - return i; + uint8_t i = 13; + while ( (--i) && strcmp(months[i], d)!=0 ); + return i; } //----------------------------------------------------------------------------- const char * delim = " :"; @@ -56,33 +56,33 @@ char s[128]; // for sprintf //----------------------------------------------------------------------------- void ParseBuildTimestamp(tm_t & mt) { - // Timestamp format: "Dec 8 2017, 22:57:54" - sprintf(s, "Timestamp: %s, %s\n", __DATE__, __TIME__); - //Serial.print(s); - char * token = strtok(s, delim); // get first token - // walk through tokens - while( token != NULL ) { - if ( (uint8_t m = str2month((const char*)token))>0 ) { - mt.month = m; - //Serial.print(" month: "); Serial.println(mt.month); - token = strtok(NULL, delim); // get next token - mt.day = atoi(token); - //Serial.print(" day: "); Serial.println(mt.day); - token = strtok(NULL, delim); // get next token - mt.year = atoi(token) - 1970; - //Serial.print(" year: "); Serial.println(mt.year); - token = strtok(NULL, delim); // get next token - mt.hour = atoi(token); - //Serial.print(" hour: "); Serial.println(mt.hour); - token = strtok(NULL, delim); // get next token - mt.minute = atoi(token); - //Serial.print(" minute: "); Serial.println(mt.minute); - token = strtok(NULL, delim); // get next token - mt.second = atoi(token); - //Serial.print(" second: "); Serial.println(mt.second); - } - token = strtok(NULL, delim); - } + // Timestamp format: "Dec 8 2017, 22:57:54" + sprintf(s, "Timestamp: %s, %s\n", __DATE__, __TIME__); + //Serial.print(s); + char * token = strtok(s, delim); // get first token + // walk through tokens + while( token != NULL ) { + if ( (uint8_t m = str2month((const char*)token))>0 ) { + mt.month = m; + //Serial.print(" month: "); Serial.println(mt.month); + token = strtok(NULL, delim); // get next token + mt.day = atoi(token); + //Serial.print(" day: "); Serial.println(mt.day); + token = strtok(NULL, delim); // get next token + mt.year = atoi(token) - 1970; + //Serial.print(" year: "); Serial.println(mt.year); + token = strtok(NULL, delim); // get next token + mt.hour = atoi(token); + //Serial.print(" hour: "); Serial.println(mt.hour); + token = strtok(NULL, delim); // get next token + mt.minute = atoi(token); + //Serial.print(" minute: "); Serial.println(mt.minute); + token = strtok(NULL, delim); // get next token + mt.second = atoi(token); + //Serial.print(" second: "); Serial.println(mt.second); + } + token = strtok(NULL, delim); + } } //----------------------------------------------------------------------------- // This function is called in the attachSecondsInterrpt @@ -165,10 +165,10 @@ void loop() if (tt1 != tt && dispflag == true ) { tt1 = tt; - // get and print actual RTC timestamp + // get and print actual RTC timestamp rtclock.breakTime(rtclock.now(), mtt); - sprintf(s, "RTC timestamp: %s %u %u, %s, %02u:%02u:%02u\n", - months[mtt.month], mtt.day, mtt.year+1970, weekdays[mtt.weekday], mtt.hour, mtt.minute, mtt.second); + sprintf(s, "RTC timestamp: %s %u %u, %s, %02u:%02u:%02u\n", + months[mtt.month], mtt.day, mtt.year+1970, weekdays[mtt.weekday], mtt.hour, mtt.minute, mtt.second); Serial.print(s); } } From f8b7a7ab14ae422835849369b19bccc0d8fea25d Mon Sep 17 00:00:00 2001 From: stevstrong Date: Sat, 9 Dec 2017 21:04:07 +0100 Subject: [PATCH 3/6] Update BluePill-RTClock-test.ino make compiler happy --- .../examples/BluePill-RTClock-test/BluePill-RTClock-test.ino | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/STM32F1/libraries/RTClock/examples/BluePill-RTClock-test/BluePill-RTClock-test.ino b/STM32F1/libraries/RTClock/examples/BluePill-RTClock-test/BluePill-RTClock-test.ino index 87213b7..394a130 100644 --- a/STM32F1/libraries/RTClock/examples/BluePill-RTClock-test/BluePill-RTClock-test.ino +++ b/STM32F1/libraries/RTClock/examples/BluePill-RTClock-test/BluePill-RTClock-test.ino @@ -62,7 +62,8 @@ void ParseBuildTimestamp(tm_t & mt) char * token = strtok(s, delim); // get first token // walk through tokens while( token != NULL ) { - if ( (uint8_t m = str2month((const char*)token))>0 ) { + uint8_t m = str2month((const char*)token); + if ( m>0 ) { mt.month = m; //Serial.print(" month: "); Serial.println(mt.month); token = strtok(NULL, delim); // get next token From 75f21e3194ebeac17427c066a6b907f6b57099c6 Mon Sep 17 00:00:00 2001 From: stevstrong Date: Sat, 9 Dec 2017 21:05:47 +0100 Subject: [PATCH 4/6] Update BluePill-RTClock-test.ino corrected text alignment (again) --- .../examples/BluePill-RTClock-test/BluePill-RTClock-test.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/STM32F1/libraries/RTClock/examples/BluePill-RTClock-test/BluePill-RTClock-test.ino b/STM32F1/libraries/RTClock/examples/BluePill-RTClock-test/BluePill-RTClock-test.ino index 394a130..697b094 100644 --- a/STM32F1/libraries/RTClock/examples/BluePill-RTClock-test/BluePill-RTClock-test.ino +++ b/STM32F1/libraries/RTClock/examples/BluePill-RTClock-test/BluePill-RTClock-test.ino @@ -62,8 +62,8 @@ void ParseBuildTimestamp(tm_t & mt) char * token = strtok(s, delim); // get first token // walk through tokens while( token != NULL ) { - uint8_t m = str2month((const char*)token); - if ( m>0 ) { + uint8_t m = str2month((const char*)token); + if ( m>0 ) { mt.month = m; //Serial.print(" month: "); Serial.println(mt.month); token = strtok(NULL, delim); // get next token From f21895e48d0cedbf7645df7fdd42726921795f54 Mon Sep 17 00:00:00 2001 From: stevstrong Date: Sun, 10 Dec 2017 02:07:29 +0100 Subject: [PATCH 5/6] Update BluePill-RTClock-test.ino - corrected typo in comments - reworked serial date input --- .../BluePill-RTClock-test.ino | 28 ++++++++----------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/STM32F1/libraries/RTClock/examples/BluePill-RTClock-test/BluePill-RTClock-test.ino b/STM32F1/libraries/RTClock/examples/BluePill-RTClock-test/BluePill-RTClock-test.ino index 697b094..5f7b847 100644 --- a/STM32F1/libraries/RTClock/examples/BluePill-RTClock-test/BluePill-RTClock-test.ino +++ b/STM32F1/libraries/RTClock/examples/BluePill-RTClock-test/BluePill-RTClock-test.ino @@ -4,7 +4,7 @@ based on https://github.com/rogerclarkmelbourne/Arduino_STM32 1. Blink on PC13 per 4s or 7s by attachAlarmInterrupt for 10 times - 2. Second counter by attachSecondsInterrpt + 2. Second counter by attachSecondsInterrupt 3. Serial output on(41s) or off(21s) by creatAlarm 4. change to your timezone in the sketch; 3. get Unix epoch time from https://www.epochconverter.com/ ; @@ -35,7 +35,6 @@ int globAlmCount = 0; int lastGlobAlmCount; int SPECAlmCount = 0; int lastSPECAlmCount; -int i = 0; int alarmcount = 3; uint8_t AlarmExchange = 0; bool dispflag = true; @@ -86,14 +85,14 @@ void ParseBuildTimestamp(tm_t & mt) } } //----------------------------------------------------------------------------- -// This function is called in the attachSecondsInterrpt +// This function is called in the attachSecondsInterrupt //----------------------------------------------------------------------------- void SecondCount () { tt++; } //----------------------------------------------------------------------------- -// This function is called in the attachAlarmInterrpt +// This function is called in the attachAlarmInterrupt //----------------------------------------------------------------------------- void blink () { @@ -115,8 +114,8 @@ void setup() pinMode(LED_BUILTIN, OUTPUT); //while (!Serial); delay(1000); ParseBuildTimestamp(mtt); // get the Unix epoch Time counted from 00:00:00 1 Jan 1970 - tt = rtclock.makeTime(mtt); - rtclock.setTime(tt+25); // additional seconds to compensate build and upload delay + tt = rtclock.makeTime(mtt) + 25; // additional seconds to compensate build and upload delay + rtclock.setTime(tt); tt1 = tt; rtclock.attachAlarmInterrupt(blink);// Call blink rtclock.attachSecondsInterrupt(SecondCount);// Call SecondCount @@ -124,18 +123,13 @@ void setup() //----------------------------------------------------------------------------- void loop() { - while (Serial.available()) - { dateread[i] = Serial.read(); - if (i < 11) { - i++; - } - else { - i = 0; - tt = (dateread[0] - '0') * 1000000000 + (dateread[1] - '0') * 100000000 + (dateread[2] - '0') * 10000000 + (dateread[3] - '0') * 1000000 + (dateread[4] - '0') * 100000; - tt += (dateread[5] - '0') * 10000 + (dateread[6] - '0') * 1000 + (dateread[7] - '0') * 100 + (dateread[8] - '0') * 10 + (dateread[9] - '0'); - rtclock.TimeZone(tt, timezone); //adjust to your local date - rtclock.setTime(rtclock.TimeZone(tt, timezone)); + if ( Serial.available()>10 ) { + for (uint8_t i = 0; i<11; i++) { + dateread[i] = Serial.read(); } + Serial.flush(); + tt = atol((char*)dateread); + rtclock.setTime(rtclock.TimeZone(tt, timezone)); //adjust to your local date } if (lastGlobAlmCount != globAlmCount || lastSPECAlmCount != SPECAlmCount ) { if (globAlmCount == 10) { // to detachAlarmInterrupt and start creatAlarm after 10 times about 110s From 000b6ffcd93ebb9a0fe9a745c3d2bf8b124adfc5 Mon Sep 17 00:00:00 2001 From: stevstrong Date: Sun, 10 Dec 2017 02:17:58 +0100 Subject: [PATCH 6/6] Update BluePill-RTClock-test.ino - small alignment fix. --- .../examples/BluePill-RTClock-test/BluePill-RTClock-test.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/STM32F1/libraries/RTClock/examples/BluePill-RTClock-test/BluePill-RTClock-test.ino b/STM32F1/libraries/RTClock/examples/BluePill-RTClock-test/BluePill-RTClock-test.ino index 5f7b847..d5789db 100644 --- a/STM32F1/libraries/RTClock/examples/BluePill-RTClock-test/BluePill-RTClock-test.ino +++ b/STM32F1/libraries/RTClock/examples/BluePill-RTClock-test/BluePill-RTClock-test.ino @@ -163,7 +163,7 @@ void loop() // get and print actual RTC timestamp rtclock.breakTime(rtclock.now(), mtt); sprintf(s, "RTC timestamp: %s %u %u, %s, %02u:%02u:%02u\n", - months[mtt.month], mtt.day, mtt.year+1970, weekdays[mtt.weekday], mtt.hour, mtt.minute, mtt.second); + months[mtt.month], mtt.day, mtt.year+1970, weekdays[mtt.weekday], mtt.hour, mtt.minute, mtt.second); Serial.print(s); } }