2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

M5Stack Core2: RTC を使った時計

Last updated at Posted at 2021-09-03

IMG_20210903_155614.jpg

こちらのプログラムを改造して、RTC の割り込みを使うようにしました。
省エネの為、画面下部のボタンを押して 10秒間、時刻を表示するようにしました。
M5Stack Core 2: NTP で時刻を合わせる

フォルダー構造

$ tree clock01/
clock01/
├── clock01.ino
└── ntp.ino
clock01.ino
// ---------------------------------------------------------------
/*
	clock01.ino

					Sep/03/2021

*/
// ---------------------------------------------------------------
# include <M5Core2.h>
# include <WiFiMulti.h>
# include <time.h>

// for WiFi
char ssid[] = "some-ssid";
char password[] = "some-password";
WiFiMulti wifiMulti;

RTC_DateTypeDef RTC_DateStruct; // Data
RTC_TimeTypeDef RTC_TimeStruct; // Time

hw_timer_t *timer1 = NULL;  

boolean flag_display = false;
int sec_display = 10;
// ---------------------------------------------------------------
void IRAM_ATTR onTimer_proc()
{
	int iix = millis() / 100;

	Serial.println("*** onTimer_proc *** " + String(iix));

	if (0 < sec_display)
		{
		flag_display = true;
		sec_display--;
		}
}

// ---------------------------------------------------------------
void setup()
{
	M5.begin();
	M5.Lcd.fillScreen(BLACK);
	M5.Lcd.setTextColor(WHITE,BLACK);

	wifiMulti.addAP(ssid, password);

	timer1 = timerBegin(0, 80, true); 

	timerAttachInterrupt(timer1, &onTimer_proc, true); 

	timerAlarmWrite(timer1, 1000000, true); 

	timerAlarmEnable(timer1);

	Serial.println("*** setup *** end ***");
}

// ---------------------------------------------------------------
void loop()
{
	// for Touch
	TouchPoint_t pos= M5.Touch.getPressPoint();

	if(240 < pos.y)
		{
		sec_display = 10;
		if(pos.x < 109)
			M5.Lcd.setTextColor(RED,BLACK);
		else if(109 <= pos.x && pos.x <= 218)
			M5.Lcd.setTextColor(GREEN,BLACK);
		else if(218 < pos.x)
			M5.Lcd.setTextColor(WHITE,BLACK);
	}

	if ((flag_display) && (0 < sec_display))
		{
		display_time_proc();
		delay(100);

		flag_display = false;
		}
	else if (sec_display == 0)
		{
		M5.Lcd.fillScreen(BLACK);
		}
}

// ---------------------------------------------------------------
void display_time_proc()
{
	const char *wd[7] = {"Sun","Mon","Tue","Wed","Thr","Fri","Sat"};

	M5.Rtc.GetDate(&RTC_DateStruct);
	M5.Rtc.GetTime(&RTC_TimeStruct);

	M5.Lcd.setTextSize(3);
	M5.Lcd.setCursor(10, 60, 1);
	M5.Lcd.printf("%04d.%02d.%02d %s\n", RTC_DateStruct.Year, RTC_DateStruct.Month, RTC_DateStruct.Date, wd[RTC_DateStruct.WeekDay]);
 M5.Lcd.setTextSize(5);
	M5.Lcd.setCursor(30, 140, 1);
	M5.Lcd.printf("%02d:%02d:%02d", RTC_TimeStruct.Hours, RTC_TimeStruct.Minutes, RTC_TimeStruct.Seconds);

}

// ---------------------------------------------------------------
ntp.ino
// ---------------------------------------------------------------
/*
	ntp.ino

					Sep/03/2021

*/
// ---------------------------------------------------------------
void setNTP2RTC()
{
	const char* ntpServer = "ntp.jst.mfeed.ad.jp";
	const long  gmtOffset_sec = 9 * 3600;
	const int   daylightOffset_sec = 0;

	struct tm timeinfo;

	configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
	while (!getLocalTime(&timeinfo))
		{
		delay(1000);
		}

	getLocalTime(&timeinfo);
	// read RTC
	M5.Rtc.GetTime(&RTC_TimeStruct);
	M5.Rtc.GetDate(&RTC_DateStruct);

	// --- to over write date&time
	RTC_DateStruct.Year = timeinfo.tm_year + 1900;
	RTC_DateStruct.Month = timeinfo.tm_mon + 1;
	RTC_DateStruct.Date = timeinfo.tm_mday;
	RTC_DateStruct.WeekDay = timeinfo.tm_wday;
	M5.Rtc.SetDate(&RTC_DateStruct);
	RTC_TimeStruct.Hours = timeinfo.tm_hour;
	RTC_TimeStruct.Minutes = timeinfo.tm_min;
	RTC_TimeStruct.Seconds = timeinfo.tm_sec;
	M5.Rtc.SetTime(&RTC_TimeStruct);
}

// ---------------------------------------------------------------
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?