Difference between revisions of "Grove-RTC"
(→Code Exemple->) |
(→Liens externes) |
||
| (19 intermediate revisions by the same user not shown) | |||
| Line 2: | Line 2: | ||
=== Caractéristiques === | === Caractéristiques === | ||
24-heures ou 12-heures format avec indication AM/PM <br> | 24-heures ou 12-heures format avec indication AM/PM <br> | ||
| + | Connectique : I2C protocol | ||
| + | [[File:I2c.png|30px]]<br> | ||
[[File:Grove-RTC.jpg|300px]] | [[File:Grove-RTC.jpg|300px]] | ||
[[File:Grove-RTC_Arduino_Connection.jpg|300px]] | [[File:Grove-RTC_Arduino_Connection.jpg|300px]] | ||
=== Support === | === Support === | ||
| − | DS1307 clock chip<br> | + | RTC '''DS1307''' clock chip<br> |
| − | Lithium | + | Lithium Cell Battery (3-Volt '''CR1225''' Lithium)<br> |
<br> | <br> | ||
| − | |||
=== Voltage === | === Voltage === | ||
| Line 77: | Line 78: | ||
Serial.println(" "); | Serial.println(" "); | ||
} | } | ||
| + | </pre> | ||
| + | |||
| + | === Code SetTime === | ||
| + | https://www.pjrc.com/teensy/td_libs_DS1307RTC.html | ||
| + | |||
| + | https://github.com/PaulStoffregen/Time | ||
| + | |||
| + | https://github.com/PaulStoffregen/DS1307RTC | ||
| + | |||
| + | <pre> | ||
| + | #include <Wire.h> | ||
| + | #include <TimeLib.h> | ||
| + | #include <DS1307RTC.h> | ||
| + | |||
| + | const char *monthName[12] = { | ||
| + | "Jan", "Feb", "Mar", "Apr", "May", "Jun", | ||
| + | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" | ||
| + | }; | ||
| + | |||
| + | tmElements_t tm; | ||
| + | |||
| + | void setup() { | ||
| + | bool parse=false; | ||
| + | bool config=false; | ||
| + | |||
| + | // get the date and time the compiler was run | ||
| + | if (getDate(__DATE__) && getTime(__TIME__)) { | ||
| + | parse = true; | ||
| + | // and configure the RTC with this info | ||
| + | if (RTC.write(tm)) { | ||
| + | config = true; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | Serial.begin(9600); | ||
| + | while (!Serial) ; // wait for Arduino Serial Monitor | ||
| + | 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() { | ||
| + | } | ||
| + | |||
| + | 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]) == 0) break; | ||
| + | } | ||
| + | if (monthIndex >= 12) return false; | ||
| + | tm.Day = Day; | ||
| + | tm.Month = monthIndex + 1; | ||
| + | tm.Year = CalendarYrToTm(Year); | ||
| + | return true; | ||
| + | } | ||
| + | |||
| + | </pre> | ||
| + | |||
| + | === Code ReadTime === | ||
| + | https://www.pjrc.com/teensy/td_libs_DS1307RTC.html | ||
| + | |||
| + | https://github.com/PaulStoffregen/Time | ||
| + | |||
| + | https://github.com/PaulStoffregen/DS1307RTC | ||
| + | |||
| + | <pre> | ||
| + | #include <Wire.h> | ||
| + | #include <TimeLib.h> | ||
| + | #include <DS1307RTC.h> | ||
| + | |||
| + | void setup() { | ||
| + | Serial.begin(9600); | ||
| + | while (!Serial) ; // wait for serial | ||
| + | delay(200); | ||
| + | Serial.println("DS1307RTC Read Test"); | ||
| + | Serial.println("-------------------"); | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | tmElements_t tm; | ||
| + | |||
| + | if (RTC.read(tm)) { | ||
| + | Serial.print("Ok, Time = "); | ||
| + | print2digits(tm.Hour); | ||
| + | Serial.write(':'); | ||
| + | print2digits(tm.Minute); | ||
| + | Serial.write(':'); | ||
| + | print2digits(tm.Second); | ||
| + | Serial.print(", Date (D/M/Y) = "); | ||
| + | Serial.print(tm.Day); | ||
| + | Serial.write('/'); | ||
| + | Serial.print(tm.Month); | ||
| + | Serial.write('/'); | ||
| + | Serial.print(tmYearToCalendar(tm.Year)); | ||
| + | Serial.println(); | ||
| + | } else { | ||
| + | if (RTC.chipPresent()) { | ||
| + | Serial.println("The DS1307 is stopped. Please run the SetTime"); | ||
| + | Serial.println("example to initialize the time and begin running."); | ||
| + | Serial.println(); | ||
| + | } else { | ||
| + | Serial.println("DS1307 read error! Please check the circuitry."); | ||
| + | Serial.println(); | ||
| + | } | ||
| + | delay(9000); | ||
| + | } | ||
| + | delay(1000); | ||
| + | } | ||
| + | |||
| + | void print2digits(int number) { | ||
| + | if (number >= 0 && number < 10) { | ||
| + | Serial.write('0'); | ||
| + | } | ||
| + | Serial.print(number); | ||
| + | } | ||
| + | |||
</pre> | </pre> | ||
==== Liens internes ==== | ==== Liens internes ==== | ||
| − | ==== Liens externes ==== | + | ==== Liens externes Library ==== |
| − | [https:// | + | [https://github.com/Seeed-Studio/RTC_DS1307/ RTC Library]<br> |
| + | [https://raw.githubusercontent.com/SeeedDocument/Grove-RTC/master/res/RTC_Library.zip RTC Library ZIP]<br> | ||
| − | [http://wiki. | + | [https://www.seeedstudio.com/Grove-RTC-p-758.html Seeed Studio shop]<br> |
| + | [http://wiki.seeedstudio.com/Grove-RTC/ Seeed Studio wiki] | ||
Latest revision as of 21:32, 15 October 2024
Contents
Grove - RTC (Seeed-Studio)
Caractéristiques
24-heures ou 12-heures format avec indication AM/PM
Connectique : I2C protocol
![]()
Support
RTC DS1307 clock chip
Lithium Cell Battery (3-Volt CR1225 Lithium)
Voltage
3.3~5.5V
Code Exemple->File -> Example ->RTC->SetTimeAndDisplay
#include <Wire.h>
#include "DS1307.h"
DS1307 clock;//define a object of DS1307 class
void setup()
{
Serial.begin(9600);
clock.begin();
clock.fillByYMD(2013,1,19);//Jan 19,2013
clock.fillByHMS(15,28,30);//15:28 30"
clock.fillDayOfWeek(SAT);//Saturday
clock.setTime();//write time to the RTC chip
}
void loop()
{
printTime();
}
/*Function: Display time on the serial monitor*/
void printTime()
{
clock.getTime();
Serial.print(clock.hour, DEC);
Serial.print(":");
Serial.print(clock.minute, DEC);
Serial.print(":");
Serial.print(clock.second, DEC);
Serial.print(" ");
Serial.print(clock.month, DEC);
Serial.print("/");
Serial.print(clock.dayOfMonth, DEC);
Serial.print("/");
Serial.print(clock.year+2000, DEC);
Serial.print(" ");
Serial.print(clock.dayOfMonth);
Serial.print("*");
switch (clock.dayOfWeek)// Friendly printout the weekday
{
case MON:
Serial.print("MON");
break;
case TUE:
Serial.print("TUE");
break;
case WED:
Serial.print("WED");
break;
case THU:
Serial.print("THU");
break;
case FRI:
Serial.print("FRI");
break;
case SAT:
Serial.print("SAT");
break;
case SUN:
Serial.print("SUN");
break;
}
Serial.println(" ");
}
Code SetTime
https://www.pjrc.com/teensy/td_libs_DS1307RTC.html
https://github.com/PaulStoffregen/Time
https://github.com/PaulStoffregen/DS1307RTC
#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
const char *monthName[12] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
tmElements_t tm;
void setup() {
bool parse=false;
bool config=false;
// get the date and time the compiler was run
if (getDate(__DATE__) && getTime(__TIME__)) {
parse = true;
// and configure the RTC with this info
if (RTC.write(tm)) {
config = true;
}
}
Serial.begin(9600);
while (!Serial) ; // wait for Arduino Serial Monitor
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() {
}
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]) == 0) break;
}
if (monthIndex >= 12) return false;
tm.Day = Day;
tm.Month = monthIndex + 1;
tm.Year = CalendarYrToTm(Year);
return true;
}
Code ReadTime
https://www.pjrc.com/teensy/td_libs_DS1307RTC.html
https://github.com/PaulStoffregen/Time
https://github.com/PaulStoffregen/DS1307RTC
#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
void setup() {
Serial.begin(9600);
while (!Serial) ; // wait for serial
delay(200);
Serial.println("DS1307RTC Read Test");
Serial.println("-------------------");
}
void loop() {
tmElements_t tm;
if (RTC.read(tm)) {
Serial.print("Ok, Time = ");
print2digits(tm.Hour);
Serial.write(':');
print2digits(tm.Minute);
Serial.write(':');
print2digits(tm.Second);
Serial.print(", Date (D/M/Y) = ");
Serial.print(tm.Day);
Serial.write('/');
Serial.print(tm.Month);
Serial.write('/');
Serial.print(tmYearToCalendar(tm.Year));
Serial.println();
} else {
if (RTC.chipPresent()) {
Serial.println("The DS1307 is stopped. Please run the SetTime");
Serial.println("example to initialize the time and begin running.");
Serial.println();
} else {
Serial.println("DS1307 read error! Please check the circuitry.");
Serial.println();
}
delay(9000);
}
delay(1000);
}
void print2digits(int number) {
if (number >= 0 && number < 10) {
Serial.write('0');
}
Serial.print(number);
}