#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "freertos/queue.h"
#include "driver/spi_master.h"
#include "driver/gpio.h"
#include "esp_timer.h"
#include "driver/gptimer.h"
static const char *TAG = "example";
int testFlag = 1;
bool test_alarm_cb_t(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_ctx)
{
//printf("Current testFlag = %d\n", !testFlag); // printf in the callback will cause the program to crash
testFlag++;
gptimer_stop(timer);
return true;
}
void timer_init()
{
gptimer_handle_t gptimer_handle = {0};
gptimer_config_t config = {
.clk_src = GPTIMER_CLK_SRC_DEFAULT,
.direction = GPTIMER_COUNT_UP,
.resolution_hz = 1000000,
};
gptimer_new_timer(&config, &gptimer_handle);
gptimer_alarm_config_t alarm_config = {
.alarm_count = 1000000,
.reload_count = 0,
.flags.auto_reload_on_alarm = true,
};
gptimer_set_alarm_action(gptimer_handle, &alarm_config);
const gptimer_event_callbacks_t cbs = {
.on_alarm = test_alarm_cb_t,
};
gptimer_register_event_callbacks(gptimer_handle, &cbs, NULL);
gptimer_enable(gptimer_handle);
gptimer_start(gptimer_handle);
}
//Main application
void app_main(void)
{
timer_init();
while(1) {
vTaskDelay(1000 / portTICK_PERIOD_MS);
printf("testFlag = %d\n", testFlag);
ESP_LOGI(TAG, "Timer count value=%d", testFlag);
}
}
07-30
1309
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
12-16
01-09
1万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交