Arduino ESP32的新定时器库

ESP32 Arduino最新Timer库

概述

早期的Arduino提供的库使用timerBegin(0, 80, true)形式,现在新版的Arduino库已经发生了变化。库的位置:hardware\esp32\3.0.0\cores\esp32\esp32-hal-timer.h. 库函数的头文件如下

头文件

#pragma once

#include "soc/soc_caps.h"
#if SOC_GPTIMER_SUPPORTED

#include "esp32-hal.h"
#include "driver/gptimer_types.h"

#ifdef __cplusplus
extern "C" {
#endif

struct timer_struct_t;
typedef struct timer_struct_t hw_timer_t;

hw_timer_t *timerBegin(uint32_t frequency);
void timerEnd(hw_timer_t *timer);

void timerStart(hw_timer_t *timer);
void timerStop(hw_timer_t *timer);
void timerRestart(hw_timer_t *timer);
void timerWrite(hw_timer_t *timer, uint64_t val);

uint64_t timerRead(hw_timer_t *timer);
uint64_t timerReadMicros(hw_timer_t *timer);
uint64_t timerReadMilis(hw_timer_t *timer);
double timerReadSeconds(hw_timer_t *timer);

uint32_t timerGetFrequency(hw_timer_t *timer);

void timerAttachInterrupt(hw_timer_t *timer, void (*userFunc)(void));
void timerAttachInterruptArg(hw_timer_t *timer, void (*userFunc)(void *), void *arg);
void timerDetachInterrupt(hw_timer_t *timer);

void timerAlarm(hw_timer_t *timer, uint64_t alarm_value, bool autoreload, uint64_t reload_count);

#ifdef __cplusplus
}
#endif

#endif /* SOC_GPTIMER_SUPPORTED */

从名称上就可以看出如何使用。

使用实例

 void setup()
 {
    // Create semaphore to inform us when the timer has fired
    timerSemaphore = xSemaphoreCreateBinary();
    
    tim1 = timerBegin(1000000);           // 1MHz
    timerAttachInterrupt(tim1, &onTimer);
  // 设置alarm调用onTimer,每10us调用一次。
  //Set alarm to call onTimer function every second (value in microseconds).
  // 重复调用,第四个参数是 = 0
    timerAlarm(tim1, 10, true, 0);      // 10 us 执行一次
}

void ARDUINO_ISR_ATTR onTimer() 
{
  // Increment the counter and set the time of ISR
  portENTER_CRITICAL_ISR(&timerMux);
  if ( tm_Enable) tm_ms++;
  portEXIT_CRITICAL_ISR(&timerMux);
  // Give a semaphore that we can check in the loop
  xSemaphoreGiveFromISR(timerSemaphore, NULL);
  // It is safe to use digitalRead/Write here if you want to toggle an output
}

这个程序在使能了tm_Enable时对tm_ms进行累加。

心得

设置1MHz或者是10MHz,1us无法使用。10us可以正常使用。并且这个新的库没法确定定时器号。2MHz的定时器运行时loop无法访问串口了。用Serial.avialble()函数没法访问串口了。有知道怎么解决的吗?

### Arduino ESP32 定时器使用方法 在Arduino环境中配置和使用ESP32定时器功能涉及初始化硬件定时器并设置回调函数来处理定时事件。下面介绍一种基于`hw_timer`的方法,该允许开发者创建精确的时间间隔触发特定的任务执行。 为了简化操作流程,在程序启动阶段完成必要的设定工作是重要的[^1]: ```cpp #include "esp_timer.h" #define INTERVAL_US 500000 // 设置时间间隔为500,000微秒即每半秒钟一次 // 声明全局变量用于存储一次性或周期性定时器句柄 static esp_timer_handle_t periodic_timer; void timer_callback(void* arg){ Serial.println("Timer triggered!"); } void setup(){ // 初始化串口通信以便打印调试信息 Serial.begin(115200); const esp_timer_create_args_t periodic_timer_args = { .callback = &timer_callback, /* 此处可以传递参数给回调函数 */ .arg = NULL, .name = "periodic", .dispatch_method=ESP_TIMER_TASK }; // 创建一个周期性的计时器实例 ESP_ERROR_CHECK( esp_timer_create(&periodic_timer_args, &periodic_timer) ); } ``` 上述代码展示了如何定义一个名为`timer_callback`的简单回调函数以及通过调用`esp_timer_create()`函数创建了一个定时器对象,并将其指派给了静态变量`periodic_timer`。这里还设置了每次触发时所要运行的具体逻辑——向控制台输出一条消息表示定时器被激活了[^1]。 接下来的部分负责实际开启这个定时机制,这通常是在`setup()`之后由另一个独立的功能模块实现,比如按下按钮或者接收到某些网络指令等场景下才会去启动它;不过在这里直接放在`loop()`里作为演示用途也是完全可以接受的做法[^1]: ```cpp void loop() { static bool started = false; if (!started){ ESP_ERROR_CHECK( esp_timer_start_periodic(periodic_timer,INTERVAL_US)); started=true; } } ``` 这段脚本确保了一旦进入主循环体内部就会立即尝试启动之前建立好的周期型定时装置,并且只做这一次而已因为状态标志位会在成功后立刻翻转过来阻止后续重复动作的发生[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值