/* Hello World Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_spi_flash.h"
#include "esp_log.h"
#include "driver/timer.h"
#include "freertos/queue.h"
#include "driver/periph_ctrl.h"
#include "esp_types.h"
#define TIMER_DIVIDER 16 // Hardware timer clock divider
#define TIMER_SCALE (TIMER_BASE_CLK / TIMER_DIVIDER/1000) // convert counter value to ms seconds
#define TIMER_INTERVAL0_SEC (10) // sample test interval for the first timer
#define TEST_WITH_RELOAD 1 // testing will be done with auto reload
typedef struct {
uint64_t timer_minute_count;
uint64_t timer_second_count;
} timer_event_t;
timer_event_t g_timer_event;
xQueueHandle timer_queue;
void IRAM_ATTR timer_group0_isr(void *para)
{
timer_spinlock_take(TIMER_GROUP_0);
int timer_idx = (int) para;
timer_event_t evt;
timer_group_clr_intr_status_in_isr(TIMER_GROUP_0, TIMER_0);
timer_group_enable_alarm_in_isr(TIMER_GROUP_0, timer_idx);
xQueueSendFromISR(timer_queue, &evt, NULL);
timer_spinlock_give(TIMER_GROUP_0);
}
static void example_tg0_timer_init(int timer_idx,
bool auto_reload, double timer_interval_sec)
{
/* Select and initialize basic parameters of the timer */
timer_config_t config = {
.divider = TIMER_DIVIDER,
.counter_dir = TIMER_COUNT_UP,
.counter_en = TIMER_PAUSE,
.alarm_en = TIMER_ALARM_EN,
.auto_reload = auto_reload,
}; // default clock source is APB
timer_init(TIMER_GROUP_0, timer_idx, &config);
/* Timer's counter will initially start from value below.
Also, if auto_reload is set, this value will be automatically reload on alarm */
timer_set_counter_value(TIMER_GROUP_0, timer_idx, 0x00000000ULL);
/* Configure the alarm value and the interrupt on alarm. */
timer_set_alarm_value(TIMER_GROUP_0, timer_idx, timer_interval_sec * TIMER_SCALE);
timer_enable_intr(TIMER_GROUP_0, timer_idx);
timer_isr_register(TIMER_GROUP_0, timer_idx, timer_group0_isr,
(void *) timer_idx, ESP_INTR_FLAG_IRAM, NULL);
timer_start(TIMER_GROUP_0, timer_idx);
}
static void timer_example_evt_task(void *arg)
{
while (1) {
timer_event_t evt;
xQueueReceive(timer_queue, &evt, portMAX_DELAY);
g_timer_event.timer_minute_count ++;
//60s 计算一次 刷新时间
if(g_timer_event.timer_minute_count >= 6000){
g_timer_event.timer_minute_count = 0;
}
g_timer_event.timer_second_count ++;
//1s计算一次
if(g_timer_event.timer_second_count >= 100){
g_timer_event.timer_second_count = 0;
ESP_LOGI("TIMER APP","run\r\n");
}
}
}
void ds_timer_init(void)
{
g_timer_event.timer_minute_count = 0;
g_timer_event.timer_second_count = 0;
timer_queue = xQueueCreate(10, sizeof(timer_event_t));
example_tg0_timer_init(TIMER_0, TEST_WITH_RELOAD, TIMER_INTERVAL0_SEC);
xTaskCreate(timer_example_evt_task, "timer_evt_task", 2048, NULL, 5, NULL);
}
static const char *TAG="MAIN.APP";
static void test_task_example(void*arg)
{
for(;;)
{
vTaskDelay(1000/portTICK_PERIOD_MS);
printf("task run\r\n");
}
}
void app_main(void)
{
printf("Hello world cwcwcw!\n");
/* Print chip information */
esp_chip_info_t chip_info;
esp_chip_info(&chip_info);
printf("This is %s chip with %d CPU core(s), WiFi%s%s, ",
CONFIG_IDF_TARGET,
chip_info.cores,
(chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "",
(chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "");
printf("silicon revision %d, ", chip_info.revision);
printf("%dMB %s flash\n", spi_flash_get_chip_size() / (1024 * 1024),
(chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");
printf("Minimum free heap size: %d bytes\n", esp_get_minimum_free_heap_size());
ESP_LOGI(TAG,"hello there is hello world\r\n");
xTaskCreate(test_task_example,"test_task_example",2048,NULL,10,NULL);
ds_timer_init();
while (1)
{
printf("sysyem run...\r\n");
vTaskDelay(1000/portTICK_PERIOD_MS);
}
}
创建消息队列 初始化定时器和中断 创建任务 在任务中执行事件
前面是默认配置,设置定时器的闹钟(多久进一次中断),使能定时器,注册中断函数,开启定时器
在这里发送消息队列 是个结构体
任务里面就接受到消息 就让初始化的结构体中秒加1 如果秒大于了100 就log一个sign
文章来源:https://www.toymoban.com/news/detail-677719.html
头文件+结构体文章来源地址https://www.toymoban.com/news/detail-677719.html
到了这里,关于ESP32---定时器+消息队列的使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!