3
2

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 1 year has passed since last update.

M5Stack Core2: タイマー割り込み

Last updated at Posted at 2021-09-02

こちらのページを参考にしました。
RepeatTimer

時間設定の単位は、microseconds です。
5 秒は 5000000 になります。

プログラム

timer/timer.ino
// ---------------------------------------------------------------
/*
	timer.ino

					Sep/02/2021
*/
// ---------------------------------------------------------------
#include  <M5Core2.h>

hw_timer_t *timer1 = NULL;  

// ---------------------------------------------------------------
void IRAM_ATTR onTimer_proc()
{
  	int iix = millis() / 100;
 
	Serial.println("*** onTimer_proc *** " + String(iix));
}
 
// ---------------------------------------------------------------
void setup()
{
	M5.begin();

	timer1 = timerBegin(0, 80, true); 

	timerAttachInterrupt(timer1, &onTimer_proc, true); 

	timerAlarmWrite(timer1, 5000000, true); 

	timerAlarmEnable(timer1); 

	Serial.println("*** setup *** aaa ***");
	delay(1000);
	Serial.println("*** setup *** bbb ***");
	delay(1000);
	Serial.println("*** setup *** ccc ***");
	delay(1000);
} 

// ---------------------------------------------------------------
void loop()
{ 
  	float ffx = millis()/ 1000.0;
	Serial.println("\t*** loop *** aaa *** " + String(ffx));
	delay(3000);
  	ffx = millis()/ 1000.0;
	Serial.println("\t*** loop *** bbb ***"  + String(ffx));
	delay(3000);
  	ffx = millis()/ 1000.0;
	Serial.println("\t*** loop *** ccc ***"  + String(ffx));
	delay(3000);
}
 
// ---------------------------------------------------------------

Arduino IDE

image.png

実行結果

$ cu -s 115200 -l /dev/ttyUSB0
Connected.
	*** loop *** aaa *** 4.66
*** onTimer_proc *** 66
	*** loop *** bbb ***7.66
	*** loop *** ccc ***10.66
*** onTimer_proc *** 116
	*** loop *** aaa *** 13.66
*** onTimer_proc *** 166
	*** loop *** bbb ***16.66
	*** loop *** ccc ***19.66
*** onTimer_proc *** 216
	*** loop *** aaa *** 22.66
	*** loop *** bbb ***25.66
*** onTimer_proc *** 266
	*** loop *** ccc ***28.66
*** onTimer_proc *** 316
	*** loop *** aaa *** 31.66
	*** loop *** bbb ***34.66
3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?