官方文档翻译-ESP32-High Resolution Timer

ESP32的esp_timer API提供了一种利用硬件定时器的方法,它支持微秒级的分辨率和64位范围的一次性和周期性定时器。定时器回调由高优先级任务分发,并建议在回调中执行最少的工作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

高分辨率定时器

概述

Although FreeRTOS provides software timers, these timers have a few limitations:
虽然FreeRTOS提供软件定时器,但这些定时器有一些限制:

  • Maximum resolution is equal to RTOS tick period
  • 最大分辨率等于RTOS滴答周期
  • Timer callbacks are dispatched from a low-priority task
  • 定时器回调的任务优先级低

Hardware timers are free from both of the limitations, but often they are less convenient to use. For example, application components may need timer events to fire at certain times in the future, but the hardware timer only contains one “compare” value used for interrupt generation. This means that some facility needs to be built on top of the hardware timer to manage the list of pending events can dispatch the callbacks for these events as corresponding hardware interrupts happen.
硬件定时器不受这两个限制,但通常使用起来不方便。例如,应用程序组件可能需要定时器事件才能在将来的特定时间触发,但硬件定时器只包含一个“比较”值用于产生中断。这意味着需要在硬件定时器的基础上构建一些设施来管理待处理事件列表,以便在相应的硬件中断发生时调度这些事件的回调。

esp_timer set of APIs provide such facility. Internally, esp_timer uses a 32-bit hardware timer (FRC1, “legacy” timer). esp_timer provides one-shot and periodic timers, microsecond time resolution, and 64-bit range.
esp_timerAPI提供这些。其内部,esp_timer使用32位硬件定时器(FRC1,“传统”定时器)。esp_timer提供一次性和周期性定时器,微秒级的分辨率和64位范围。

Timer callbacks are dispatched from a high-priority esp_timer task. Because all the callbacks are dispatched from the same task, it is recommended to only do the minimal possible amount of work from the callback itself, posting an event to a lower priority task using a queue instead.
定时器回调由高优先级的esp_timer任务分派。由于所有回调都是从同一个任务调度的,因此建议仅从回调做最少量的工作,//FIXME:而不是使用队列将事件发布到较低优先级的任务。

使用esp_timerAPI

Single timer is represented by esp_timer_handle_t type. Timer has a callback function associated with it. This callback function is called from the esp_timer task each time the timer elapses.
单个计时器由esp_timer_handle_t类型表示。Timer有一个与之相关的回调函数。这个回调函数在esp_timer任务中定时器超时的时候调用。

The timer can be started in one-shot mode or in periodic mode.
定时器可以在单次模式或周期模式下启动。

  • To start the timer in one-shot mode, call esp_timer_start_once(), passing the time interval after which the callback should be called. When the callback gets called, the timer is considered to be stopped.
  • 要以单发模式启动计时器,请调用esp_timer_start_once(),并设定触发时间。当回调被调用时,定时器被认为是停止的。
  • To start the timer in periodic mode, call esp_timer_start_periodic(), passing the period with which the callback should be called. The timer keeps running until esp_timer_stop() is called.
  • 要以周期模式启动定时器,请调用esp_timer_start_periodic(),并设定触发周期。调用esp_timer_stop()停止定时器。

Note that the timer must not be running when esp_timer_start_once() or esp_timer_start_periodic()is called. To restart a running timer, call esp_timer_stop() first, then call one of the start functions.
请注意,定时器运行前需调用esp_timer_start_once()或者esp_timer_start_periodic()。要重启正在运行的定时器,先调用esp_timer_stop(),然后调用其中一个启动函数。

获取当前时间

esp_timer also provides a convenience function to obtain the time passed since start-up, with microsecond precision: esp_timer_get_time(). This function returns the number of microseconds since esp_timer was initialized, which usually happens shortly before app_main function is called.
esp_timer还提供了一个方便的功能来获得自启动以来的时间,精确到微秒:esp_timer_get_time()。该函数返回从esp_timer初始化完成到函数调用经过的微秒数,esp_timer初始化通常在app_main之前一些完成。

Unlike gettimeofday function, values returned by esp_timer_get_time():
与gettimeofday函数不同,esp_timer_get_time()函数的返回值:

  • Start from zero after the chip wakes up from deep sleep
  • 芯片从深度睡眠中醒来后,从零开始
  • Do not have timezone or DST adjustments applied
  • 没有应用时区或DST调整

API参考

头文件

本文翻译自:https://esp-idf.readthedocs.io/en/latest/api-reference/system/esp_timer.html

翻译水平有限,如有错误欢迎指正

Arduino-ESP32定时器文档提供了ESP32开发板上可用的定时器的详细信息和用法。ESP32开发板有多个定时器,可以用于各种任务,如定期发送数据、控制LED闪烁、测量时间间隔等。 以下是一些常用的ESP32定时器: 1. Timer0:用于系统时钟,不能被用户使用。 2. Timer1:用于WiFi和蓝牙功能,不能被用户使用。 3. Timer2:用于系统任务,不能被用户使用。 4. Timer3和Timer4:可供用户使用,但需注意避免与系统任务冲突。 使用ESP32定时器需要包含头文件“esp32-hal-timer.h”,然后创建一个timer_config_t类型的结构体,设置定时器的参数,如时钟频率、计数器模式、计数器周期等。最后使用esp_timer_create()函数创建定时器,并使用esp_timer_start()函数启动定时器。 例如,以下代码创建了一个1秒钟的定时器,并在定时器超时时执行回调函数: ```c #include "esp32-hal-timer.h" void IRAM_ATTR onTimer(void *arg) { // 定时器超时时执行的代码 } void setup() { timer_config_t timerConfig; timerConfig.divider = 80; // 定时器时钟频率为80MHz timerConfig.counter_dir = TIMER_COUNT_UP; timerConfig.counter_en = TIMER_PAUSE; timerConfig.alarm_en = TIMER_ALARM_EN; timerConfig.intr_type = TIMER_INTR_LEVEL; timerConfig.auto_reload = true; timerConfig.counter_value = 0; timerConfig.alarm_value = 1000000; // 定时器周期为1秒钟 esp_timer_create(&timerConfig, &timerHandle); esp_timer_start_periodic(timerHandle, 1000000); // 启动定时器 } void loop() { // 主循环代码 } ``` 需要注意的是,定时器回调函数应该尽可能简短,避免阻塞主循环。如果需要执行耗时的任务,可以在回调函数中设置标志位,在主循环中检查标志位并执行任务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值