RTC

begin()

Function:

Initialize the RTC clock.

Pay Attention:
1.If you do not want to initialize the RTC clock with M5.begin (), call this Function before using RTC.

Example:

#include <M5Core2.h>

void setup() {
  M5.Rtc.begin();  //Initialize the RTC clock.
}

void loop() {
}

SetTime()

Function:

Set real-time clock time.

Function prototype:

void SetTime(RTC_TimeTypeDef* RTC_TimeStruct)

Example:

#include <M5Core2.h>

RTC_TimeTypeDef TimeStruct;
void setup() {
  M5.begin();

  TimeStruct.Hours   = 18;    //Set the specific time of the real-time clock structure.
  TimeStruct.Minutes = 56;
  TimeStruct.Seconds = 10;
  M5.Rtc.SetTime(&TimeStruct);    //Writes the set time to the real-time clock.
}
void loop(){}

GetTime()

Function:

Get real-time clock time.

Function prototype:

void GetTime(RTC_TimeTypeDef* RTC_TimeStruct)

Example:

#include <M5Core2.h>

RTC_TimeTypeDef TimeStruct;
void setup() {
  M5.begin();
  M5.Lcd.println("RTC Time TEST");
  TimeStruct.Hours   = 18;
  TimeStruct.Minutes = 56;
  TimeStruct.Seconds = 10;
  M5.Rtc.SetTime(&TimeStruct);
}

void loop() {
  M5.Rtc.GetTime(&TimeStruct);    
  M5.Lcd.setCursor(0, 15);
  M5.Lcd.printf("Time: %02d : %02d : %02d/n",TimeStruct.Hours, TimeStruct.Minutes, TimeStruct.Seconds);
  delay(500);
}

SetData()

Function:

Set real-time clock date.

Function prototype:

void SetData(RTC_TimeTypeDef* RTC_DateStruct)

Example:

#include <M5Core2.h>

RTC_DateTypeDef DateStruct;
void setup() {
  M5.begin();
  DateStruct.WeekDay = 3;
  DateStruct.Month = 3;
  DateStruct.Date = 22;
  DateStruct.Year = 2019;
  M5.Rtc.SetData(&DateStruct);
}
void loop(){}

GetData()

Function:

Get real-time clock date.

Function prototype:

void GetData(RTC_TimeTypeDef* RTC_DateStruct)

Example:

#include <M5Core2.h>

RTC_DateTypeDef DateStruct;
void setup() {
  M5.begin();
  M5.Lcd.println("RTC Date TEST");
  DateStruct.WeekDay = 5;
  DateStruct.Month = 7;
  DateStruct.Date = 23;
  DateStruct.Year = 2021;
  M5.Rtc.SetDate(&DateStruct);
}

void loop() {
  M5.Rtc.GetDate(&DateStruct);    //Get the date of the real-time clock. 
  M5.Lcd.setCursor(0, 15);
  M5.Lcd.printf("Data: %04d-%02d-%02d/n",DateStruct.Year, DateStruct.Month,DateStruct.Date);    //Output the date of the current real-time clock on the screen. 
  M5.Lcd.printf("Week: %d/n",DateStruct.WeekDay);
  delay(500);
}

SetAlarmIRQ()

Function:

Setting the interrupt clock

Function prototype:

int SetAlarmIRQ(int afterSeconds)

int SetAlarmIRQ( const RTC_TimeTypeDef &RTC_TimeStruct)

int SetAlarmIRQ( const RTC_DateTypeDef &RTC_DateStruct, const RTC_TimeTypeDef &RTC_TimeStruct)

Example:

#include <M5Core2.h>

RTC_TimeTypeDef TimeStruct;
void setup() {
  M5.begin();
  M5.Lcd.println("RTC SetAlarmIQR");
  TimeStruct.Hours   = 18;
  TimeStruct.Minutes = 56;
  TimeStruct.Seconds = 10;
  M5.Rtc.SetTime(&TimeStruct);
}

void loop() {
  M5.update();
  M5.Rtc.GetTime(&TimeStruct);
  M5.Lcd.setCursor(0, 15);
  M5.Lcd.printf("Time: %02d : %02d : %02d/n",TimeStruct.Hours, TimeStruct.Minutes, TimeStruct.Seconds);
  if(M5.BtnA.wasPressed()){
    M5.Lcd.println("M5Core2 Will Close, Restore After 5 seconds ");
    delay(2000);
    M5.Rtc.clearIRQ();
    M5.Rtc.SetAlarmIRQ(5);
    delay(10);
    M5.Axp.PowerOff();
  }
}

clearIRQ()

Function:

Clear interrupt request

Function prototype:

void clearIRQ()

disableIRQ()

Function:

Turn off interrupt request

Function prototype:

void disableIRQ()

shutdown()

Function 1:

To turn off the power and start it again, you need to wake up with the PWR button

void shutdown()

Function 2:

Turn off the power, and according to the number of seconds of delay passed in, wake up the device through RTC after the delay is over

int shutdown( int seconds )

Function 3:

Turn off the power, pass in the RTC time structure with a specified time point, and wake up the device through RTC when it meets the hrs, minutes, and seconds of that time.

int shutdown( const RTC_TimeTypeDef &RTC_TimeStruct)

Function 4:

Turn off the power, pass in the RTC time structure specified for a certain point in time, and wake up the device by RTC when the week, day, and time of that point in time match at the same time. int shutdown( const RTC_DateTypeDef &RTC_DateStruct, const RTC_TimeTypeDef &RTC_TimeStruct);

int shutdown( const RTC_DateTypeDef &RTC_DateStruct, const RTC_TimeTypeDef &RTC_TimeStruct)

Example:

#include <M5Core2.h>

RTC_TimeTypeDef RTCtime;
RTC_TimeTypeDef RTCtime_Now;

char timeStrbuff[64];

void setup(){
  M5.begin(true,true,true,true);

  RTCtime.Hours = 10;
  RTCtime.Minutes = 30;
  RTCtime.Seconds = 45;

  M5.Lcd.setCursor(0,80);
  M5.Lcd.println("");
  M5.Lcd.println("BtnA:  shutdown, use power button to turn back on");
  M5.Lcd.println("");
  M5.Lcd.println("BtnB:  shutdown, wake up after 5 seconds");
  M5.Lcd.println("");
  M5.Lcd.println("BtnC:  shutdown, wake up at RTC Time 10:30:45");
}

void loop(){
  M5.update();
  if(M5.BtnA.wasPressed()){
    M5.shutdown();
  }else if(M5.BtnB.wasPressed()){
    M5.shutdown(5);
  }else if(M5.BtnC.wasPressed()){
    M5.shutdown(RTCtime);
  }

  M5.Lcd.setCursor(0,140);
  M5.Rtc.GetTime(&RTCtime_Now);
  sprintf(timeStrbuff,"RTC Time Now is %02d:%02d:%02d",
         RTCtime_Now.Hours,RTCtime_Now.Minutes,RTCtime_Now.Seconds);
  M5.Lcd.println(timeStrbuff);

}
On This Page