/*定时器组例子:有两组定时器,每组有两个
*/
#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);
}
/*

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

被折叠的 条评论
为什么被折叠?



