본문으로 바로가기

STM32 내장 RTC 사용하기

category MCU/Arduino STM32F103C8T6 2018. 12. 4. 17:39

https://github.com/PaulStoffregen/Time 에서 라이브러리 다운로드 및 셋업


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Internal RTC 사용하기
 
#include <TimeLib.h>
#include <RTClock.h>
#include <EEPROM.h>
 
#define CONFIG_CLOCK_BIT_ADRRESS 0
#define CONFIGURED 0
 
const char *monthName[12= {
  "Jan""Feb""Mar""Apr""May""Jun""Jul""Aug""Sep""Oct""Nov""Dec"
};
 
RTClock rt (RTCSEL_LSE); // initialise
 
tmElements_t tm;
 
void setup() {
    bool parse=false;
    bool config=false;
 
    Serial.begin(9600);
 
    if (EEPROM.read(CONFIG_CLOCK_BIT_ADRRESS) != CONFIGURED) {
    // get the date and time the compiler was run
        if (getDate(__DATE__) && getTime(__TIME__)) {
            parse = true;
       
            // first configure the STM32 RTC with this info
            rt.setTime(makeTime(tm));
 
            delay(200);
  
            // second sync TimeLib with STM32 RTC
            setTime(rt.getTime());
      
        if (timeStatus() == timeSet) {
            config = true;
        
            EEPROM.write(CONFIG_CLOCK_BIT_ADRRESS, CONFIGURED);
        }
    }
  
    delay(200);
    
        if (parse && config) {
            Serial.print("STM32 RTC configured, Time = ");
            Serial.print(__TIME__);
            Serial.print(", Date = ");
            Serial.println(__DATE__);
        }
        else {
            Serial.print("Could not parse info from the compiler, Time = \"");
            Serial.print(__TIME__);
            Serial.print("\", Date = \"");
            Serial.print(__DATE__);
            Serial.println("\"");
        }
    }
    else {
        Serial.println("STM32 RTC running...");
        setTime(rt.getTime());
    }
}
 
void loop() {
    if (EEPROM.read(CONFIG_CLOCK_BIT_ADRRESS) == CONFIGURED) {
        digitalClockDisplay();
    }
    else {
        Serial.println("The time has not been set.  Please run the Time");
        Serial.println();
        delay(4000);
    }
    delay(1000);
}
 
void digitalClockDisplay() {
    // digital clock display of the time
    Serial.print(year()); 
    Serial.print(".");
    Serial.print(month());
    Serial.print("/");
    Serial.print(day());
    Serial.print("     ");
    if (hour() < 13) {
        Serial.print("AM ");
        Serial.print(hour());
    }
    else {
        Serial.print("PM ");
        Serial.print(hour()-12);
    }
    printDigits(minute());
    printDigits(second());
    Serial.println(); 
}
 
void printDigits(int digits) {
    // utility function for digital clock display: prints preceding colon and leading 0
    Serial.print(":");
    if(digits < 10) {
        Serial.print('0');
    }
    Serial.print(digits);
}
 
bool getTime(const char *str) {
    int Hour, Min, Sec;
 
    if (sscanf(str, "%d:%d:%d"&Hour, &Min, &Sec) != 3) {
        return false;
    }
    tm.Hour = Hour;
    tm.Minute = Min;
    tm.Second = Sec;
    return true;
}
 
bool getDate(const char *str) {
    char Month[12];
    int Day, Year;
    uint8_t monthIndex;
 
    if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) {
        return false;
    }
    for (monthIndex = 0; monthIndex < 12; monthIndex++) {
        if (strcmp(Month, monthName[monthIndex]) == 0break;
    }
    if (monthIndex >= 12) {
        return false;
    }
    tm.Day = Day;
    tm.Month = monthIndex + 1;
    tm.Year = CalendarYrToTm(Year);
    return true;
}
 
cs