-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
libraries/ESP32/examples/Timer/RepeatTimer/RepeatTimer.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
Repeat timer example | ||
This example shows how to use hardware timer in ESP32. The timer calls onTimer | ||
function every second. The timer can be stopped with button attached to PIN 0 | ||
(IO0). | ||
This example code is in the public domain. | ||
*/ | ||
|
||
// Stop button is attached to PIN 0 (IO0) | ||
#define BTN_STOP_ALARM 0 | ||
|
||
hw_timer_t * timer = NULL; | ||
|
||
void onTimer(){ | ||
static unsigned int counter = 1; | ||
|
||
Serial.print("onTimer no. "); | ||
Serial.print(counter); | ||
Serial.print(" at "); | ||
Serial.print(millis()); | ||
Serial.println(" ms"); | ||
|
||
counter++; | ||
} | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
|
||
// Set BTN_STOP_ALARM to input mode | ||
pinMode(BTN_STOP_ALARM, INPUT); | ||
|
||
// Use 1st timer of 4 (counted from zero). | ||
// Set 80 divider for prescaler (see ESP32 Technical Reference Manual for more | ||
// info). | ||
timer = timerBegin(0, 80, true); | ||
|
||
// Attach onTimer function to our timer. | ||
timerAttachInterrupt(timer, &onTimer, true); | ||
|
||
// Set alarm to call onTimer function every second (value in microseconds). | ||
// Repeat the alarm (third parameter) | ||
timerAlarmWrite(timer, 1000000, true); | ||
|
||
// Start an alarm | ||
timerAlarmEnable(timer); | ||
} | ||
|
||
void loop() { | ||
// If button is pressed | ||
if (digitalRead(BTN_STOP_ALARM) == LOW) { | ||
// If timer is still running | ||
if (timer) { | ||
// Stop and free timer | ||
timerEnd(timer); | ||
timer = NULL; | ||
} | ||
} | ||
} |