|
| 1 | +/* STM32F103C8 Black Pill ( PB12) |
| 2 | +
|
| 3 | + Serialport set and display RTC clock , Write by CSNOL https://github.com/csnol/STM32-Examples |
| 4 | + based on https://github.com/rogerclarkmelbourne/Arduino_STM32 |
| 5 | +
|
| 6 | + 1. Blink on PB12 per 1s. |
| 7 | + 2. change to your timezone in the sketch; . |
| 8 | + 3. get Unix epoch time from https://www.epochconverter.com/ ; |
| 9 | + 4. last step input the 10 bits number( example: 1503945555) to Serialport ; |
| 10 | + 5. the clock will be reset to you wanted. |
| 11 | +
|
| 12 | + ## Why the 10 bits Unix epoch time be used? |
| 13 | +****Because I wanna connect to NTP server by ESP-8266. |
| 14 | +****in the <NTPClient.h> library. getNtpTime() will return this 10 bits Unix epoch time. |
| 15 | +*/ |
| 16 | + |
| 17 | + |
| 18 | +#include <RTClock.h> |
| 19 | + |
| 20 | +RTClock rtclock (RTCSEL_LSE); // initialise |
| 21 | +time_t tt; |
| 22 | +tm_t mtt = { 47, 8, 27, 0, 1, 20, 30, 30 }; |
| 23 | +char weekday1[][7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; // 0,1,2,3,4,5,6 |
| 24 | +uint8_t timeread[11]; |
| 25 | +int timezone = 8 * 3600; // timezone -8 + DST +1 = -7 CA USA |
| 26 | +time_t tt1; |
| 27 | + |
| 28 | +#define LED_PIN PB12 |
| 29 | + |
| 30 | +// This function is called in the attachSecondsInterrpt |
| 31 | +void blink () |
| 32 | +{ |
| 33 | + digitalWrite(LED_PIN, !digitalRead(LED_PIN)); |
| 34 | + tt++; |
| 35 | +} |
| 36 | + |
| 37 | +void setup() |
| 38 | +{ |
| 39 | + Serial.begin(115200); |
| 40 | + pinMode(LED_PIN, OUTPUT); |
| 41 | + tt = rtclock.makeTime(mtt); |
| 42 | + tt1 = tt; |
| 43 | + rtclock.attachSecondsInterrupt(blink);// Call blink |
| 44 | +} |
| 45 | + |
| 46 | + |
| 47 | +int i = 0; |
| 48 | +void loop() |
| 49 | +{ |
| 50 | + while (Serial.available()) |
| 51 | + { timeread[i] = Serial.read(); |
| 52 | + if (i < 11) { |
| 53 | + i++; |
| 54 | + } |
| 55 | + else { |
| 56 | + i = 0; |
| 57 | + tt = (timeread[0] - '0') * 1000000000 + (timeread[1] - '0') * 100000000 + (timeread[2] - '0') * 10000000 + (timeread[3] - '0') * 1000000 + (timeread[4] - '0') * 100000; |
| 58 | + tt += (timeread[5] - '0') * 10000 + (timeread[6] - '0') * 1000 + (timeread[7] - '0') * 100 + (timeread[8] - '0') * 10 + (timeread[9] - '0') + timezone; |
| 59 | + } |
| 60 | + } |
| 61 | + if (tt1 != tt) |
| 62 | + { |
| 63 | + tt1 = tt; |
| 64 | + rtclock.breakTime(tt, mtt); |
| 65 | + Serial.print("Date: "); |
| 66 | + Serial.print(mtt.day); |
| 67 | + Serial.print("- "); |
| 68 | + Serial.print(mtt.month); |
| 69 | + Serial.print(" "); |
| 70 | + Serial.print(mtt.year + 1970); |
| 71 | + Serial.print(" "); |
| 72 | + Serial.print(weekday1[mtt.weekday]); |
| 73 | + Serial.print(" Time: "); |
| 74 | + Serial.print(mtt.hour); |
| 75 | + Serial.print(" : "); |
| 76 | + Serial.print(mtt.minute); |
| 77 | + Serial.print(" : "); |
| 78 | + Serial.println(mtt.second); |
| 79 | + } |
| 80 | +} |
0 commit comments