Skip to content

Commit ed797d4

Browse files
authored
Tested on BluePill for new function of RTC
1 parent 6be069c commit ed797d4

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/* STM32F103C8 Blue Pill ( PC13)
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 PC13 per 4s or 7s by attachAlarmInterrupt for 10 times
7+
2. Second counter by attachSecondsInterrpt
8+
3. Serial output on(41s) or off(21s) by creatAlarm
9+
4. change to your timezone in the sketch; .
10+
3. get Unix epoch time from https://www.epochconverter.com/ ;
11+
4. last step input the 10 bits number( example: 1503945555) to Serialport ;
12+
5. the clock will be reset to you wanted.
13+
14+
## Why the 10 bits Unix epoch time be used?
15+
****Because I wanna connect to NTP server by ESP-8266.
16+
****in the <NTPClient.h> library. getNtpTime() will return this 10 bits Unix epoch time.
17+
*/
18+
19+
20+
#include <RTClock.h>
21+
22+
RTClock rtclock (RTCSEL_LSE); // initialise
23+
int timezone = 8; // change to your timezone
24+
time_t tt;
25+
time_t tt1;
26+
tm_t mtt = { 47, 9, 13, 3, 1, 22, 30, 30 }; // init time 47+1970 = 2017 Unix epoch Time counted from 00:00:00 1 Jan 1970
27+
char weekday1[][7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; // 0,1,2,3,4,5,6
28+
uint8_t dateread[11];
29+
int globAlmCount = 0;
30+
int lastGlobAlmCount;
31+
int SPECAlmCount = 0;
32+
int lastSPECAlmCount;
33+
int i = 0;
34+
int alarmcount = 3;
35+
uint8_t AlarmExchange = 0;
36+
bool dispflag = true;
37+
38+
#define LED_PIN PC13
39+
40+
// This function is called in the attachSecondsInterrpt
41+
void SecondCount ()
42+
{
43+
tt++;
44+
}
45+
// This function is called in the attachAlarmInterrpt
46+
void blink ()
47+
{
48+
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
49+
globAlmCount++;
50+
//tt++;
51+
}
52+
53+
void OnOffSerial ()
54+
{
55+
dispflag = !dispflag;
56+
SPECAlmCount++;
57+
}
58+
59+
void setup()
60+
{
61+
lastGlobAlmCount = ~globAlmCount;
62+
lastSPECAlmCount = ~SPECAlmCount;
63+
Serial.begin(115200);
64+
pinMode(LED_PIN, OUTPUT);
65+
tt = rtclock.makeTime(mtt);
66+
tt1 = tt;
67+
rtclock.attachAlarmInterrupt(blink);// Call blink
68+
rtclock.attachSecondsInterrupt(SecondCount);// Call SecondCount
69+
}
70+
71+
void loop()
72+
{
73+
while (Serial.available())
74+
{ dateread[i] = Serial.read();
75+
if (i < 11) {
76+
i++;
77+
}
78+
else {
79+
i = 0;
80+
tt = (dateread[0] - '0') * 1000000000 + (dateread[1] - '0') * 100000000 + (dateread[2] - '0') * 10000000 + (dateread[3] - '0') * 1000000 + (dateread[4] - '0') * 100000;
81+
tt += (dateread[5] - '0') * 10000 + (dateread[6] - '0') * 1000 + (dateread[7] - '0') * 100 + (dateread[8] - '0') * 10 + (dateread[9] - '0');
82+
tt = rtclock.TimeZone(tt, timezone); //adjust to your local date
83+
}
84+
}
85+
if (lastGlobAlmCount != globAlmCount | lastSPECAlmCount != SPECAlmCount ) {
86+
if (globAlmCount == 10) { // to detachAlarmInterrupt and start creatAlarm after 10 times about 110s
87+
rtclock.detachAlarmInterrupt();
88+
globAlmCount = 0;
89+
rtclock.createAlarm(OnOffSerial, (rtclock.getTime() + 20)); // call OnOffSerial stop output date from Serial after 2 mins
90+
alarmcount = 20; //change to creatAlarm 21S close Serial output and 41s open Serial output.
91+
}
92+
else {
93+
lastGlobAlmCount = globAlmCount;
94+
lastSPECAlmCount = SPECAlmCount;
95+
Serial.println(" Say hello to every guys ");
96+
if(dispflag == false)
97+
Serial.println(" SPECAlarm turn Display Off ");
98+
switch (AlarmExchange) {
99+
case 0:
100+
rtclock.setAlarmTime(rtclock.getTime() + alarmcount); // reset alarm = current time + 4s for attachAlarmInterrupt, 21s for creatAlarm
101+
AlarmExchange = 1;
102+
break;
103+
case 1:
104+
rtclock.breakTime(rtclock.getTime() + alarmcount * 2, mtt); //// reset alarm = current time + 7s for attachAlarmInterrupt, 41s for creatAlarm
105+
rtclock.setAlarmTime(mtt);
106+
AlarmExchange = 0;
107+
break;
108+
}
109+
}
110+
}
111+
if (tt1 != tt & dispflag == true )
112+
{
113+
tt1 = tt;
114+
rtclock.breakTime(tt, mtt);
115+
Serial.print("Date: ");
116+
Serial.print(mtt.day);
117+
Serial.print("- ");
118+
Serial.print(mtt.month);
119+
Serial.print(" ");
120+
Serial.print(mtt.year + 1970);
121+
Serial.print(" ");
122+
Serial.print(weekday1[mtt.weekday]);
123+
Serial.print(" Time: ");
124+
Serial.print(mtt.hour);
125+
Serial.print(" : ");
126+
Serial.print(mtt.minute);
127+
Serial.print(" : ");
128+
Serial.println(mtt.second);
129+
}
130+
}

0 commit comments

Comments
 (0)