ESP32定时器例程解析

本文详细介绍了如何使用ESP32的定时器组,包括定时器0和定时器1的配置,中断处理函数以及定时器中断事件的处理。通过示例展示了定时器在不同模式下的工作原理,例如不重载和自动重装载,同时提供了中断服务例程的IRAM_ATTR属性以确保中断处理的实时性。

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

/*定时器组例子:有两组定时器,每组有两个

*/


#include <stdio.h>

#include "esp_types.h"

#include "freertos/FreeRTOS.h"

#include "freertos/task.h"

#include "freertos/queue.h"

#include "soc/timer_group_struct.h"

#include "driver/periph_ctrl.h"

#include "driver/timer.h"


#define TIMER_DIVIDER    16 // 分频

#define TIMER_SCALE     (TIMER_BASE_CLK / TIMER_DIVIDER) // 转换为秒计数

#define TIMER_INTERVAL0_SEC (3.4179) // 定时器1时间间隔

#define TIMER_INTERVAL1_SEC (5.78) // 定时器2时间间隔

#define TEST_WITHOUT_RELOAD 0    // 不进行重装载

#define TEST_WITH_RELOAD   1    // 进行重装载


/*

 * A sample structure to pass events

 * from the timer interrupt handler to the main program.

 */

typedef struct {

  int type; // the type of timer's event

  int timer_group;

  int timer_idx;

  uint64_t timer_counter_value;

} timer_event_t;


xQueueHandle timer_queue;


/*

 * A simple helper function to print the raw timer counter value

 * and the counter value converted to seconds

 */

static void inline print_timer_counter(uint64_t counter_value)

{

  printf("Counter: 0x%08x%08x\n", (uint32_t) (counter_value >> 32),

                  (uint32_t) (counter_value));

  printf("Time : %.8f s\n", (double) counter_value / TIMER_SCALE);

}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值