// ESP32 Timer
//
// This is just a test and it seems to work.
//
// Documentation:
// https://espressif-docs.readthedocs-hosted.com/projects/arduino-esp32/en/latest/api/timer.html#arduino-esp32-timer-api
//
int ledPin = 5;
hw_timer_t * timer = NULL;
// Note: "volatile" is not required here.
volatile bool ledOn = false;
void ARDUINO_ISR_ATTR onTimer()
{
digitalWrite(ledPin, ledOn ? LOW : HIGH);
ledOn = !ledOn;
}
void setup()
{
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
// The parameter of timerBegin is the resolution
// of the timer (the base clock in Hz).
// Start with a value of 1MHz.
// I don't know the limits, but 1kHz does not work.
timer = timerBegin(1000000);
// Connect to the ISR.
timerAttachInterrupt(timer, &onTimer);
// Set the divider to twice per second.
// Is that a software divider ?
timerAlarm(timer, 500000, true, 0);
// At this point, the timer is already started.
// Calling "timerStart(timer);" again causes
// a warning.
}
void loop()
{
delay(10);
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4