Bonjour,
Je viens d'acquérir un DS3231 et j'ai un peu de mal à comprendre le fonctionnement.
J'utilise un exemple de code de la librairie RTClib comme suivant :
Code injecté pour définir la date :
// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include "RTClib.h"RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup () {
Serial.begin(57600);
if (! rtc.begin())
{
Serial.println("Couldn't find RTC");
Serial.flush();
while (1) delay(10);}
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); Serial.println("Set time");
}
void loop () {
DateTime now = rtc.now();Serial.print(now.year(), DEC); Serial.print('/'); Serial.print(now.month(), DEC); Serial.print('/'); Serial.print(now.day(), DEC); Serial.print(" ("); Serial.print(daysOfTheWeek[now.dayOfTheWeek()]); Serial.print(") "); Serial.print(now.hour(), DEC); Serial.print(':'); Serial.print(now.minute(), DEC); Serial.print(':'); Serial.print(now.second(), DEC); Serial.println(); Serial.println(); delay(3000);
}
Code injecté après le précédent code pour lire la date et l'heure :
// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include "RTClib.h"RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup () {
Serial.begin(57600);
if (! rtc.begin())
{
Serial.println("Couldn't find RTC");
Serial.flush();
while (1) delay(10);}
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); //Serial.println("Set time");
}
void loop () {
DateTime now = rtc.now();Serial.print(now.year(), DEC); Serial.print('/'); Serial.print(now.month(), DEC); Serial.print('/'); Serial.print(now.day(), DEC); Serial.print(" ("); Serial.print(daysOfTheWeek[now.dayOfTheWeek()]); Serial.print(") "); Serial.print(now.hour(), DEC); Serial.print(':'); Serial.print(now.minute(), DEC); Serial.print(':'); Serial.print(now.second(), DEC); Serial.println(); Serial.println(); delay(3000);
}
Résultat :
13:01:38.809 ->
13:01:41.794 -> 2022/11/23 (Wednesday) 13:1:35
13:01:41.794 ->
13:01:44.813 -> 2022/11/23 (Wednesday) 13:1:38
13:01:44.813 ->
13:01:47.810 -> 2022/11/23 (Wednesday) 13:1:41
13:01:47.810 ->
13:01:50.804 -> 2022/11/23 (Wednesday) 13:1:44
13:01:50.804 ->
INJECTION NOUVEAU CODE
13:02:05.459 -> 2000/1/1 (Saturday) 0:0:3
13:02:05.459 ->
13:02:08.470 -> 2000/1/1 (Saturday) 0:0:6
13:02:08.470 ->
13:02:11.471 -> 2000/1/1 (Saturday) 0:0:9
13:02:11.471 ->
13:02:14.442 -> 2000/1/1 (Saturday) 0:0:12
13:02:14.476 ->
13:02:17.470 -> 2000/1/1 (Saturday) 0:0:15
Ici je comprend que le module ne retient pas la date alors que dans le second code je ne fais que la lire ! Autrement dit le module il est impératif de donner la date ou module RTC après chaque coupure de courant ?!?
Autre bout de code qui ne fonctionne pas :
// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include "RTClib.h"RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup () {
Serial.begin(57600);
if (! rtc.begin())
{
Serial.println("Couldn't find RTC");
Serial.flush();
while (1) delay(10);}
if (rtc.lostPower()) {
Serial.println("RTC lost power, let's set the time!");
// When time needs to be set on a new device, or after a power loss, the
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(DATE), F(TIME)));
Serial.println("Set time");}
void loop () {
DateTime now = rtc.now();Serial.print(now.year(), DEC); Serial.print('/'); Serial.print(now.month(), DEC); Serial.print('/'); Serial.print(now.day(), DEC); Serial.print(" ("); Serial.print(daysOfTheWeek[now.dayOfTheWeek()]); Serial.print(") "); Serial.print(now.hour(), DEC); Serial.print(':'); Serial.print(now.minute(), DEC); Serial.print(':'); Serial.print(now.second(), DEC); Serial.println(); Serial.println(); delay(3000);
}
Avec comme résultat :
13:17:25.043 -> 2022/11/23 (Wednesday) 13:17:20
13:17:25.077 ->
13:17:28.067 -> 2022/11/23 (Wednesday) 13:17:23
13:17:28.067 ->
13:17:31.075 -> 2022/11/23 (Wednesday) 13:17:26
13:17:31.075 ->
13:17:34.073 -> 2022/11/23 (Wednesday) 13:17:29
13:17:34.073 ->
DEBRANCHEMENT & REBRANCHEMENT
13:17:46.414 -> 2022/11/23 (Wednesday) 13:17:17
13:17:46.447 ->
13:17:49.430 -> 2022/11/23 (Wednesday) 13:17:20
13:17:49.430 ->
13:17:52.428 -> 2022/11/23 (Wednesday) 13:17:23
13:17:52.428 ->
Clairement, après débranchement on voit que la date repart au moment de la compilation.
Je pense que je fais ou que je comprends quelque chose de mal, un idée de quoi ?
Mon module DS 3231 serait-il défaillant ?