Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
MCP79412RTC/examples/rtcSetSerial/rtcSetSerial.ino at master · epsilon537/MCP79412RTC · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
epsilon537
/
MCP79412RTC
Public
forked from
JChristensen/MCP79412RTC
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
master
Breadcrumbs
MCP79412RTC
/
examples
/
rtcSetSerial
/
rtcSetSerial.ino
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
114 lines (103 loc) · 3.8 KB
master
Breadcrumbs
MCP79412RTC
/
examples
/
rtcSetSerial
/
rtcSetSerial.ino
Copy path
Top
File metadata and controls
Code
Blame
114 lines (103 loc) · 3.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
//
Arduino MCP79412RTC Library
//
https://github.com/JChristensen/MCP79412RTC
//
Copyright (C) 2018 by Jack Christensen and licensed under
//
GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html
//
//
Example sketch to set the RTC by entering a "Set" command on the
//
serial monitor. Use a 24-hour clock and enter the command
//
(case sensitive) exactly as follows: Set yyyy-mm-dd hh:mm:ss
#
include
<
MCP79412RTC.h
>
//
https://github.com/JChristensen/MCP79412RTC
#
include
<
TimeLib.h
>
//
https://github.com/PaulStoffregen/Time
const
uint32_t
PRINT_INTERVAL
(
10000
);
//
ms between printing the time
uint32_t
ms, msLast;
void
setup
()
{
delay
(
2000
);
Serial.
begin
(
9600
);
setSyncProvider
(
RTC
.
get
);
//
the function to get the time from the RTC
Serial.
print
(
"
RTC SYNC
"
);
if
(
timeStatus
() != timeSet) Serial.
print
(
"
FAIL
"
);
Serial.
println
();
}
void
loop
()
{
ms =
millis
();
readCommand
();
if
(ms - msLast >=
PRINT_INTERVAL
) {
msLast = ms;
printTime
(
now
());
}
}
//
Read command from the Arduino serial monitor to set the RTC.
//
Case-sensitive and must be entered exactly as (24-hour clock):
//
Set yyyy-mm-dd hh:mm:ss
void
readCommand
()
{
char
cmd[
24
] =
"
Set yyyy-mm-dd hh:mm:ss
"
;
//
serial terminal will send 23 char command plus line terminator (0x0A)
if
(Serial.
available
() >=
24
) {
//
enough characters for the whole command?
unsigned
int
i =
0
;
//
yes, read the available characters
while
(Serial.
available
() >
0
) {
if
(i >=
sizeof
(cmd)) {
//
more than we can enjoy
flushInput
();
//
clear out the input buffer
cmd[
sizeof
(cmd) -
1
] =
0
;
//
string terminator
Serial.
print
(
"
Too long:
"
);
Serial.
println
(cmd);
return
;
}
delay
(
2
);
//
let the next character trickle in
char
c = Serial.
read
();
if
(c >=
'
'
) cmd[i++] = c;
//
printable characters and spaces only
}
cmd[i] =
0
;
//
put in string terminator
tmElements_t tmSet;
if
(
strncmp
(cmd,
"
Set
"
,
4
) ==
0
) {
tmSet.
Year
=
1000
* (cmd[
4
] -
'
0
'
) +
100
* (cmd[
5
] -
'
0
'
) +
10
* (cmd[
6
] -
'
0
'
) + cmd[
7
] -
'
0
'
-
1970
;
tmSet.
Month
=
10
* (cmd[
9
] -
'
0
'
) + cmd[
10
] -
'
0
'
;
tmSet.
Day
=
10
* (cmd[
12
] -
'
0
'
) + cmd[
13
] -
'
0
'
;
tmSet.
Hour
=
10
* (cmd[
15
] -
'
0
'
) + cmd[
16
] -
'
0
'
;
tmSet.
Minute
=
10
* (cmd[
18
] -
'
0
'
) + cmd[
19
] -
'
0
'
;
tmSet.
Second
=
10
* (cmd[
21
] -
'
0
'
) + cmd[
22
] -
'
0
'
;
time_t
tSet =
makeTime
(tmSet);
//
convert to time_t
setTime
(tSet);
//
set the system time
RTC
.
set
(
now
());
//
set the rtc
Serial.
println
(
"
RTC set!
"
);
printTime
(
RTC
.
get
());
flushInput
();
//
discard any extraneous trailing characters
}
else
{
Serial.
print
(
"
Unknown:
"
);
Serial.
println
(cmd);
}
}
}
void
flushInput
()
{
do
{
delay
(
2
);
Serial.
read
();
}
while
(Serial.
available
() >
0
);
}
//
Print time (and date) given a time_t value
void
printTime
(
time_t
t)
{
printI00
(
hour
(t),
'
:
'
);
printI00
(
minute
(t),
'
:
'
);
printI00
(
second
(t),
'
'
);
Serial.
print
(
dayShortStr
(
weekday
(t)));
Serial.
print
(
'
'
);
printI00
(
day
(t),
'
'
);
Serial.
print
(
monthShortStr
(
month
(t)));
Serial.
print
(
'
'
);
Serial.
println
(
year
(t));
}
//
Print an integer in "00" format (with leading zero),
//
followed by a delimiter.
//
Input value assumed to be between 0 and 99.
void
printI00
(
int
val,
char
delim)
{
if
(val <
10
) Serial.
print
(
'
0
'
);
Serial.
print
(val);
Serial.
print
(delim);
return
;
}
You can’t perform that action at this time.