-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathesp32-freertos-09-demo-timer-interrupt.ino
55 lines (41 loc) · 1.3 KB
/
esp32-freertos-09-demo-timer-interrupt.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* ESP32 Timer Interrupt Demo
*
* Blink LED with hardware timer interrupt.
*
* Date: February 3, 2021
* Author: Shawn Hymel
* License: 0BSD
*/
// Settings
static const uint16_t timer_divider = 80;
static const uint64_t timer_max_count = 1000000;
// Pins (change this if your Arduino board does not have LED_BUILTIN defined)
static const int led_pin = LED_BUILTIN;
// Globals
static hw_timer_t *timer = NULL;
//*****************************************************************************
// Interrupt Service Routines (ISRs)
// This function executes when timer reaches max (and resets)
void IRAM_ATTR onTimer() {
// Toggle LED
int pin_state = digitalRead(led_pin);
digitalWrite(led_pin, !pin_state);
}
//*****************************************************************************
// Main (runs as its own task with priority 1 on core 1)
void setup() {
// Configure LED pin
pinMode(led_pin, OUTPUT);
// Create and start timer (num, divider, countUp)
timer = timerBegin(0, timer_divider, true);
// Provide ISR to timer (timer, function, edge)
timerAttachInterrupt(timer, &onTimer, true);
// At what count should ISR trigger (timer, count, autoreload)
timerAlarmWrite(timer, timer_max_count, true);
// Allow ISR to trigger
timerAlarmEnable(timer);
}
void loop() {
// Do nothing
}