2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ESP32: Timer 割り込み処理

Last updated at Posted at 2022-11-28

参考ページ
Timer

Ubuntu 24.04 上の Arduino IDE 2.3.2 を使いました。

プログラム

image.png

test_interrupt.ino
// ---------------------------------------------------------------
/*
	test_interrupt.ino

					Jul/27/2024
*/
// ---------------------------------------------------------------
hw_timer_t *timer = NULL;
volatile SemaphoreHandle_t timerSemaphore;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;

volatile uint32_t isrCounter = 0;
volatile uint32_t lastIsrAt = 0;
 
// ---------------------------------------------------------------
void IRAM_ATTR onTimer() {
	portENTER_CRITICAL_ISR(&timerMux);
	isrCounter++;
	lastIsrAt = millis();
	portEXIT_CRITICAL_ISR(&timerMux);
	xSemaphoreGiveFromISR(timerSemaphore, NULL);
}
 
// ---------------------------------------------------------------
void setup()
{
	Serial.begin(115200);
	delay(1000);
	Serial.println("*** setup start ***");

	timerSemaphore = xSemaphoreCreateBinary(); 
	timer = timerBegin(1000000); 
	timerAttachInterrupt(timer, &onTimer); 
	timerAlarm(timer, 1000000, true, 0);

	Serial.println("*** setup end ***"); 
}
 
// ---------------------------------------------------------------
void loop()
{
	if (xSemaphoreTake(timerSemaphore, 0) == pdTRUE)
	{
	// Read the interrupt count and time
		portENTER_CRITICAL(&timerMux);
		uint32_t isrCount = isrCounter;
		uint32_t isrTime = lastIsrAt;
		portEXIT_CRITICAL(&timerMux);

	// Print it
		Serial.print("onTimer no. ");
		Serial.print(isrCount);
		Serial.print(" at ");
		Serial.print(isrTime);
		Serial.println(" ms");
	}
}

// ---------------------------------------------------------------

実行結果

$ cu -l /dev/ttyUSB0 -s 115200
Connected.
*** setup start ***
*** setup end ***
onTimer no. 1 at 2030 ms
onTimer no. 2 at 3030 ms
onTimer no. 3 at 4030 ms
onTimer no. 4 at 5030 ms
onTimer no. 5 at 6030 ms
onTimer no. 6 at 7030 ms

M5Stack Core2

このプログラムはそのまま M5Stack Core2 で使えます。
ボードの設定は、ESP32 Dev Module のままです。
image.png

2
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?