forked from coloz/Arduino-book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
8.2.1.1-1307Set.ino
93 lines (84 loc) · 1.97 KB
/
8.2.1.1-1307Set.ino
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
/*
设置DS1307的时间
RTC模块的使用
*/
//声明这个模块用到的三个类库
#include <DS1307RTC.h>
#include <Time.h>
#include <Wire.h>
const char *monthName[12] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
// tmElements_t为保存日期时间的结构体类型
tmElements_t tm;
void setup()
{
bool parse = false;
bool config = false;
// 获取编译时的时间和日期
if (getDate(__DATE__) && getTime(__TIME__))
{
parse = true;
// 将时间数据写入RTC模块
if (RTC.write(tm))
{
config = true;
}
}
Serial.begin(9600);
delay(200);
if (parse && config)
{
Serial.print("DS1307 configured Time=");
Serial.print(__TIME__);
Serial.print(", Date=");
Serial.println(__DATE__);
}
else if (parse)
{
Serial.println("DS1307 Communication Error :-{");
Serial.println("Please check your circuitry");
}
else
{
Serial.print("Could not parse info from the compiler, Time=\"");
Serial.print(__TIME__);
Serial.print("\", Date=\"");
Serial.print(__DATE__);
Serial.println("\"");
}
}
void loop()
{
}
// 获取时间数据并存入tm
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;
}
// 获取日期数据并存入tm
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]) == 0)
break;
}
if (monthIndex >= 12)
return false;
tm.Day = Day;
tm.Month = monthIndex + 1;
tm.Year = CalendarYrToTm(Year);
return true;
}