ESP32 SoC 包含 2 到 4 个硬件定时器。它们都是基于 16 位预分频器的 64 位(ESP32-C3 为 54 位)通用定时器和 64 位(ESP32-C3 为 54 位)上/下计数器,能够自动重新加载。
ESP32 SoC | 计时器数量 |
---|---|
ESP32 | 4 |
ESP32-S2 | 4 |
ESP32-S3 | 4 |
ESP32-C3 | 2 |
ESP32-C6 | 2 |
ESP32-H2 | 2 |
This function is used to configure the timer. After successful setup the timer will automatically start.
此功能用于配置定时器。设置成功后,计时器将自动启动。
hw_timer_t * timerBegin(uint32_t frequency);
This function will return timer structure if configuration is successful. If NULL is returned, error occurs and the timer was not configured.
如果配置成功,此函数将返回 timer 结构。如果返回 NULL,则发生错误,并且未配置计时器。
This function is used to end timer.
此功能用于结束计时器。
void timerEnd(hw_timer_t * timer);
This function is used to start counter of the timer.
此功能用于启动计时器的计数器。
void timerStart(hw_timer_t * timer);
This function is used to stop counter of the timer.
此功能用于停止计时器的计数器。
void timerStop(hw_timer_t * timer);
This function is used to restart counter of the timer.
此功能用于重新启动计时器的计数器。
void timerRestart(hw_timer_t * timer);
This function is used to set counter value of the timer.
此函数用于设置计时器的计数器值。
void timerWrite(hw_timer_t * timer, uint64_t val);
This function is used to read counter value of the timer.
此函数用于读取计时器的计数器值。
uint64_t timerRead(hw_timer_t * timer);
This function will return counter value of the timer.
此函数将返回计时器的 counter 值。
This function is used to read counter value in microseconds of the timer.
此函数用于读取计时器的 counter 值(以微秒为单位)。
uint64_t timerReadMicros(hw_timer_t * timer);
This function will return counter value of the timer in microseconds.
此函数将返回计时器的计数器值(以微秒为单位)。
This function is used to read counter value in milliseconds of the timer.
此函数用于读取计时器的 counter 值(以毫秒为单位)。
uint64_t timerReadMillis(hw_timer_t * timer);
This function will return counter value of the timer in milliseconds.
此函数将返回计时器的计数器值(以毫秒为单位)。
This function is used to read counter value in seconds of the timer.
此函数用于读取计时器的秒级计数器值。
double timerReadSeconds(hw_timer_t * timer);
This function will return counter value of the timer in seconds.
此函数将返回计时器的计数器值(以秒为单位)。
This function is used to get resolution in Hz of the timer.
此函数用于获取计时器的 Hz 分辨率。
uint16_t timerGetFrequency(hw_timer_t * timer);
This function will return frequency in Hz of the timer.
此函数将返回计时器的频率(以 Hz 为单位)。
This function is used to attach interrupt to timer.
该函数用于将 Interrupt 附加到 timer。
void timerAttachInterrupt(hw_timer_t * timer, void (*userFunc)(void));
This function is used to attach interrupt to timer using arguments.
此函数用于使用参数将中断附加到计时器。
void timerAttachInterruptArg(hw_timer_t * timer, void (*userFunc)(void*), void * arg);
This function is used to detach interrupt from timer.
该函数用于将 interrupt 与 timer 分离。
void timerDetachInterrupt(hw_timer_t * timer);
This function is used to configure alarm value and autoreload of the timer. Alarm is automatically enabled.
该功能用于配置定时器的告警值和自动重新加载。警报会自动启用。
void timerAlarm(hw_timer_t * timer, uint64_t alarm_value, bool autoreload, uint64_t reload_count);
There are 2 examples uses of Timer:
Timer 有 2 个示例用法:
Repeat timer example: 重复计时器示例:
examples/Timer/RepeatTimer/RepeatTimer.ino
Watchdog timer example: 看门狗定时器示例:
examples/Timer/WatchdogTimer/WatchdogTimer.ino