# xTimer **Repository Path**: eric_pyt/xTimer ## Basic Information - **Project Name**: xTimer - **Description**: 纯软件定时器,支持裸机、RTOS - **Primary Language**: C - **License**: MIT - **Default Branch**: master - **Homepage**: https://gitee.com/eric_pyt - **GVP Project**: No ## Statistics - **Stars**: 3 - **Forks**: 1 - **Created**: 2022-04-16 - **Last Updated**: 2024-04-28 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # xTimer(softTimer) ## 1. 介绍📃 - 环形链表实现,纯软件定时器 - 支持单次、循环定时 - 不限制timer个数 - 轻便,占用资源少 - 使用灵活,移植方便 - 适用于裸机、RTOS等 ## 2. 使用方法🏃 ### 2.1 配置计数器⏰ xtimer.h中配置计数器来源,例如HAL_GetTick(); ```c #define xtimer_get_tick() HAL_GetTick(); ``` ### 2.2 使用示例🔨 ```c #include "xtimer.h" uint32_t cnt = 0; void timer1_cb(void *arg); void timer2_cb(void *arg); xtimer_t timer1 = { .arg = &cnt, .callback = timer1_cb, .name = "timer1", .opt = XTIMER_SINGLE, .timout = 1000, }; xtimer_t timer2 = { .arg = &cnt, .callback = timer2_cb, .name = "timer2", .opt = XTIMER_REPEAT, .timout = 2000, }; void timer1_cb(void *arg) { xtimer_t *timer = (xtimer_t *)arg; uint32_t cnt = *(uint32_t *)timer->arg; printf("name=%s, cnt=%d\r\n",timer->name,cnt); } void timer2_cb(void *arg) { xtimer_t *timer = (xtimer_t *)arg; uint32_t cnt = *(uint32_t *)timer->arg; printf("name=%s, cnt=%d\r\n",timer->name,cnt); } void main(void) { ... xtimer_start(&timer1); xtimer_start(&timer2); ... while(1) { ... xtimer_loop(); ... } } ``` ---