Modulo RTC - DS1302 com suporte para pilha CR1220para que nunca perca a data e hora do sue projecto com Arduino, Raspberry, ESP, STM ou qualquer outro tipo de projecto com microcontroladores com suporte de comunicação three-wire (semelhante a SPI).
The DS1302 module is a Real-Time Clock (RTC) module that can track years, months, days, weekdays, hours, minutes, and seconds. It also has the ability to adjust for leap years. It is useful for creating projects requiring precise timing and scheduling.
DS1302 is a trickle charging clock chip, launched by DALLAS in America. With a built-in real-time clock/calendar and a 31-byte static RAM, it can communicate with MCU through simple serial ports. The real-time clock/calendar circuit provides information about second, minute, hour, day, week, month, and year. DS1302 can automatically adjust the number of days per month and days in leap year. You can determine to use a 24-hour or 12-hour system by AM/PM selection. It can simply communicate with MCU in synchronous serial way and only needs to use three port cables: Reset (RST) cable, I/O data (SDA) cable and serial clock (SCL) cable.
This function initializes the serial communication and sets up the RTC module. Various checks are made to ensure the RTC is running correctly.
voidsetup(){Serial.begin(9600);Serial.print("compiled: ");Serial.print(__DATE__);Serial.println(__TIME__);Rtc.Begin();RtcDateTimecompiled=RtcDateTime(__DATE__,__TIME__);printDateTime(compiled);Serial.println();if(!Rtc.IsDateTimeValid()){// Common Causes:// 1) first time you ran and the device wasn't running yet// 2) the battery on the device is low or even missingSerial.println("RTC lost confidence in the DateTime!");Rtc.SetDateTime(compiled);}if(Rtc.GetIsWriteProtected()){Serial.println("RTC was write protected, enabling writing now");Rtc.SetIsWriteProtected(false);}if(!Rtc.GetIsRunning()){Serial.println("RTC was not actively running, starting now");Rtc.SetIsRunning(true);}RtcDateTimenow=Rtc.GetDateTime();if(now<compiled){Serial.println("RTC is older than compile time! (Updating DateTime)");Rtc.SetDateTime(compiled);}elseif(now>compiled){Serial.println("RTC is newer than compile time. (this is expected)");}elseif(now==compiled){Serial.println("RTC is the same as compile time! (not expected but all is fine)");}}
loop() function
This function periodically fetches the current date and time from the RTC and prints it on the serial monitor. It also checks if the RTC is still maintaining a valid date and time.
voidloop(){RtcDateTimenow=Rtc.GetDateTime();printDateTime(now);Serial.println();if(!now.IsValid()){// Common Causes:// 1) the battery on the device is low or even missing and the power line was disconnectedSerial.println("RTC lost confidence in the DateTime!");}delay(5000);// five seconds}
Date and time printing function
A helper function that takes a RtcDateTime object and prints the formatted date and time to the serial monitor.
Modulo RTC - DS1302 com suporte para pilha CR1220para que nunca perca a data e hora do sue projecto com Arduino, Raspberry, ESP, STM ou qualquer outro tipo de projecto com microcontroladores com suporte de comunicação three-wire (semelhante a SPI).