RTC

begin()

功能:

初始化RTC时钟

注意:
1.如果您不想使用M5.begin() 初始化RTC时钟,请在使用RTC之前调用此功能

使用示例:

#include <M5Core2.h>

void setup() {
  M5.Rtc.begin();  //初始化 RTC时钟
}

void loop() {
}

SetTime()

功能:

设置实时时钟时间

函数原型:

void SetTime(RTC_TimeTypeDef* RTC_TimeStruct)

使用示例:

#include <M5Core2.h>

RTC_TimeTypeDef TimeStruct;
void setup() {
  M5.begin();
  
  TimeStruct.Hours   = 18;    //设置实时时钟结构体的具体时刻
  TimeStruct.Minutes = 56;
  TimeStruct.Seconds = 10;
  M5.Rtc.SetTime(&TimeStruct);    //将设置的时间写入实时时钟
}
void loop(){}

GetTime()

功能:

获取实时时钟时间

函数原型:

void GetTime(RTC_TimeTypeDef* RTC_TimeStruct)

使用示例:

#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()

功能:

设置实时时钟日期

函数原型:

void SetData(RTC_TimeTypeDef* RTC_DateStruct)

使用示例:

#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()

功能:

获取实时时钟日期

函数原型:

void GetData(RTC_TimeTypeDef* RTC_DateStruct)

使用示例:

#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);    //获取实时时钟的日期
  M5.Lcd.setCursor(0, 15);
  M5.Lcd.printf("Data: %04d-%02d-%02d/n",DateStruct.Year, DateStruct.Month,DateStruct.Date);    //将此刻实时时钟的日期在屏幕输出
  M5.Lcd.printf("Week: %d/n",DateStruct.WeekDay);
  delay(500);
}

SetAlarmIRQ()

功能:

设置中断时钟

函数原型:

int SetAlarmIRQ(int afterSeconds)

int SetAlarmIRQ( const RTC_TimeTypeDef &RTC_TimeStruct)

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

使用示例:

#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()

功能:

清除中断请求

函数原型:

void clearIRQ()

disableIRQ()

功能:

关闭中断请求

函数原型:

void disableIRQ()

shutdown()

函数重载1:

关闭电源,再次启动需要通过PWR按键唤醒

void shutdown()

函数重载2:

关闭电源,根据传入的延时秒数,在延时结束后通过RTC唤醒设备。

int shutdown( int seconds )

函数重载3:

关闭电源,传入指定了某个时间点的RTC时间结构体,当符合该时间的,,的时候通过RTC唤醒设备。

int shutdown( const RTC_TimeTypeDef &RTC_TimeStruct)

函数重载4:

关闭电源,传入指定了某个时间点的RTC时间结构体,当同时符合该时间点的周数,天数,时间的时通过RTC唤醒设备。

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

使用示例:

#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