Class RTC
Description
A class used for displaying date and time and alarm configuration using RTC, the independent BCD (Binary-Coded-Decimal) timer.
Syntax
class RTC
Members
Public Constructors |
|
---|---|
A public constructor should not be used as this class is intended to be a singleton class. Access member functions using the object instance named RTC. |
Public Methods |
|
---|---|
RTC:: Init |
Initializes the RTC device, including the Clock, the RTC registers, and other functions |
RTC:: DeInit |
Deinitialize the RTC device |
RTC:: Write |
Set the specified timestamp in seconds to RTC |
RTC:: Read |
Get the current timestamp in seconds from RTC |
RTC:: Wait |
Wait for 1 second |
RTC:: SetEpoch |
Convert human-readable time to epoch time |
- RTC::Init
Description
Initializes the RTC device, including the Clock, the RTC registers, and other functions.
Syntax
void RTC::Init(void);
Parameters
The function requires no input parameter.
Returns
The function returns nothing.
Example Code
Example: RTC
RTC1 /** 2* This function describes how to use the RTC API. 3* The RTC function is implemented by an independent BCD timer/counter. 4* This example will print out the time information every second. 5*/ 6#include "stdio.h" 7#include "time.h" 8 9#include "rtc.h" 10 11#define YEAR 2020 12#define MONTH 9 13#define DAY 10 14#define HOUR 20 15#define MIN 30 16#define SEC 40 17 18/* Create an rtc object */ 19RTC rtc; 20int32_t seconds; 21struct tm *timeinfo; 22 23void setup() { 24 Serial.begin(115200); 25 rtc.Init(); // initialize RTC 26} 27 28void loop() { 29 // step 1: convert user time to epoch 30 int epochTime = humanReadableToEpoch(YEAR, MONTH, DAY, HOUR, MIN, SEC); 31 32 // step 2: write epoch time to rtc 33 rtc.Write(epochTime); 34 while (1) { 35 seconds = rtc.Read(); 36 printf("Epoch Time (in s) since January 1, 1970 = %ds\n", seconds); 37 printf("Time as a basic string = %s", ctime(&seconds)); 38 timeinfo = localtime(&seconds); 39 printf("Time as a custom formatted string = %d-%d-%d %d:%d:%d\n", 40 (timeinfo->tm_year + 1900), (timeinfo->tm_mon + 1), timeinfo->tm_mday, timeinfo->tm_hour, 41 timeinfo->tm_min, timeinfo->tm_sec); 42 Serial.println(); 43 rtc.wait(1); 44 } 45} 46 47// convert human readable time to epoch time 48int humanReadableToEpoch(int year, int month, int day, int hour, int min, int sec) { 49 struct tm t; 50 time_t t_of_day; 51 52 t.tm_year = year - 1900; // Year - 1970 53 t.tm_mon = month - 1; // Month, where 0 = jan 54 t.tm_mday = day; // Day of the month 55 t.tm_hour = hour; 56 t.tm_min = min; 57 t.tm_sec = sec; 58 t.tm_isdst = -1; // Is DST on? 1 = yes, 0 = no, -1 = unknown 59 t_of_day = mktime(&t); 60 61 // printf("seconds since the Epoch: %d\n", (long)t_of_day); 62 return t_of_day; 63}
Notes and Warnings
NA
- RTC::DeInit
Description
Deinitializes the RTC device.
Syntax
void RTC::DeInit(void);
Parameters
The function requires no input parameter.
Returns
The function returns nothing.
Example Code
Example: RTC
Details of the code can be found in the previous section of RTC::Init
.
Notes and Warnings
NA
- RTC:: Write
Description
Set the specified timestamp in seconds to RTC. Seconds from 1970.1.1 00:00:00 (YEAR.MONTH.DAY, HOUR: MIN: SECONDS) to specified date and time which is to be set.
Syntax
void RTC::Write(int t);
Parameters
t
: Seconds from 1970.1.1 00:00:00 (YEAR.MONTH.DAY, HOUR: MIN: SECONDS) to specified date and time which is to be set.
Returns
The function returns nothing.
Example Code
Example: RTC
Details of the code can be found in the previous section of RTC::Init.
Notes and Warnings
NA
- RTC::Read
Description
Get the current timestamp in seconds from RTC. The current timestamp n seconds which is calculated from 1970.1.1 00:00:00 (YEAR.MONTH.DAY, OUR: MIN: SECONDS).
Syntax
int32_t RTC::Read(void);
Parameters
The function requires no input parameter.
Returns
The function returns the current timestamp in seconds which is alculated from 1970.1.1 00:00:00 (YEAR.MONTH.DAY, HOUR: MIN: ECONDS).
Example Code
Example: RTC
Details of the code can be found in the previous section of RTC::Init
.
Notes and Warnings NA
- RTC:: Wait
Description
Send IR raw data.
Syntax
void RTC::wait(float s);
Parameters
s
: unit microseconds (1 us)
Returns
The function returns nothing.
Example Code
Example: RTC
Details of the code can be found in the previous section of RTC::Init
.
Notes and Warnings
NA
RTC:: SetEpoch
Description
Convert human-readable time to epoch time
Syntax
int RTC:: SetEpoch(int year, int month, int day, int hour, int min, int sec);
Parameters
year
: user input year
month
: user input month
day
: user input day
hour
: user input hour
min
: user input minutes
sec
: user input seconds
Returns
The function returns epoch time in seconds for RTC use.
Example Code
Example: RTC
Details of the code can be found in the previous section of RTC::Init
.
Notes and Warnings
NA