Arduino ESP32 指南

Arduino ESP32 指南 > API > Timer 定时器

Timer 定时器

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

Arduino-ESP32 定时器 API

timerBegin  计时器开始

This function is used to configure the timer. After successful setup the timer will automatically start.
此功能用于配置定时器。设置成功后,计时器将自动启动。

hw_timer_t * timerBegin(uint32_t frequency);
  • frequency select timer frequency in Hz. Sets how quickly the timer counter is “ticking”.
    frequency 选择定时器频率(以 Hz 为单位)。设置定时器计数器的“滴答作响”的速度。

This function will return timer structure if configuration is successful. If NULL is returned, error occurs and the timer was not configured.
如果配置成功,此函数将返回 timer 结构。如果返回 NULL,则发生错误,并且未配置计时器。

timerEnd  计时器结束

This function is used to end timer.
此功能用于结束计时器。

void timerEnd(hw_timer_t * timer);
  • timer timer struct.
    timer timer 结构体。

timerStart  计时器开始

This function is used to start counter of the timer.
此功能用于启动计时器的计数器。

void timerStart(hw_timer_t * timer);
  • timer timer struct.
    timer timer 结构体。

timerStop  计时器停止

This function is used to stop counter of the timer.
此功能用于停止计时器的计数器。

void timerStop(hw_timer_t * timer);
  • timer timer struct.
    timer timer 结构体。

timerRestart  计时器重启

This function is used to restart counter of the timer.
此功能用于重新启动计时器的计数器。

void timerRestart(hw_timer_t * timer);
  • timer timer struct.
    timer timer 结构体。

timerWrite 

This function is used to set counter value of the timer.
此函数用于设置计时器的计数器值。

void timerWrite(hw_timer_t * timer, uint64_t val);
  • timer timer struct.
    timer timer 结构体。
  • val counter value to be set.
    val counter 值。

timerRead

This function is used to read counter value of the timer.
此函数用于读取计时器的计数器值。

uint64_t timerRead(hw_timer_t * timer);
  • timer timer struct.
    timer timer 结构体。

This function will return counter value of the timer.
此函数将返回计时器的 counter 值。

timerReadMicros

This function is used to read counter value in microseconds of the timer.
此函数用于读取计时器的 counter 值(以微秒为单位)。

uint64_t timerReadMicros(hw_timer_t * timer);
  • timer timer struct.
    timer timer 结构体。

This function will return counter value of the timer in microseconds.
此函数将返回计时器的计数器值(以微秒为单位)。

timerReadMillis 

This function is used to read counter value in milliseconds of the timer.
此函数用于读取计时器的 counter 值(以毫秒为单位)。

uint64_t timerReadMillis(hw_timer_t * timer);
  • timer timer struct.
    timer timer 结构体。

This function will return counter value of the timer in milliseconds.
此函数将返回计时器的计数器值(以毫秒为单位)。

timerReadSeconds 

This function is used to read counter value in seconds of the timer.
此函数用于读取计时器的秒级计数器值。

double timerReadSeconds(hw_timer_t * timer);
  • timer timer struct.
    timer timer 结构体。

This function will return counter value of the timer in seconds.
此函数将返回计时器的计数器值(以秒为单位)。

timerGetFrequency

This function is used to get resolution in Hz of the timer.
此函数用于获取计时器的 Hz 分辨率。

uint16_t timerGetFrequency(hw_timer_t * timer);
  • timer timer struct.
    timer timer 结构体。

This function will return frequency in Hz of the timer.
此函数将返回计时器的频率(以 Hz 为单位)。

timerAttachInterrupt

This function is used to attach interrupt to timer.
该函数用于将 Interrupt 附加到 timer。

void timerAttachInterrupt(hw_timer_t * timer, void (*userFunc)(void));
  • timer timer struct.
    timer timer 结构体。
  • userFunc function to be called when interrupt is triggered.
    userFunc 函数。

timerAttachInterruptArg

This function is used to attach interrupt to timer using arguments.
此函数用于使用参数将中断附加到计时器。

void timerAttachInterruptArg(hw_timer_t * timer, void (*userFunc)(void*), void * arg);
  • timer timer struct.
    timer timer 结构体。
  • userFunc function to be called when interrupt is triggered.
    userFunc 函数。
  • arg pointer to the interrupt arguments.
    arg 指针指向 interrupt 参数。

timerDetachInterrupt  

This function is used to detach interrupt from timer.
该函数用于将 interrupt 与 timer 分离。

void timerDetachInterrupt(hw_timer_t * timer);
  • timer timer struct.
    timer timer 结构体。

timerAlarm 

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);
  • timer timer struct.
    timer timer 结构体。
  • alarm_value alarm value to generate event.
    alarm_value alarm 值以生成事件。
  • autoreload enabled/disabled autorealod.
  • reload_count number of autoreloads (0 = unlimited). Has no effect if autorealod is disabled.
    reload_count 自动重新加载的次数 (0 = 无限制)。如果禁用 autorealod ,则不起作用。

示例应用

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