當定時器不夠用或者不想浪費資源時。我們可以利用定時器複用功能。使用一個硬件定時器就可以實現多個時間的定時。類似於軟件定時器。
優點:可以多個定時器複用要給硬件,節約資源
缺點:它是在某個任務或者mian中一直循環查詢。優先級沒有定時器中斷優先級高
/*
* Copyright (c) 2016 Zibin Zheng <[email protected]>
* All rights reserved
*/
#include "multi_timer.h"
#include "tim.h"
//timer handle list head.
static struct Timer* head_handle = NULL;
//Timer ticks
static uint32_t _timer_ticks = 0;
/**
* @brief Initializes the timer struct handle.
* @param handle: the timer handle strcut.
* @param timeout_cb: timeout callback.
* @param repeat: repeat interval time.
* @retval None
*/
void timer_init(struct Timer* handle, void(*timeout_cb)(), uint32_t timeout, uint32_t repeat)
{
// memset(handle, sizeof(struct Timer), 0);
handle->timeout_cb = timeout_cb;
handle->timeout = _timer_ticks + timeout;
handle->repeat = repeat;
}
/**
* @brief Start the timer work, add the handle into work list.
* @param btn: target handle strcut.
* @retval 0: succeed. -1: already exist.
*/
int timer_start(struct Timer* handle)
{
struct Timer* target = head_handle;
while(target) {
if(target == handle) return -1; //already exist.
target = target->next;
}
handle->next = head_handle;
head_handle = handle;
return 0;
}
/**
* @brief Stop the timer work, remove the handle off work list.
* @param handle: target handle strcut.
* @retval None
*/
void timer_stop(struct Timer* handle)
{
struct Timer** curr;
for(curr = &head_handle; *curr; ) {
struct Timer* entry = *curr;
if (entry == handle) {
*curr = entry->next;
// free(entry);
} else
curr = &entry->next;
}
}
主程序:
/**
* @brief main loop.
* @param None.
* @retval None
*/
void timer_loop()
{
struct Timer* target;
for(target=head_handle; target; target=target->next) {
if(_timer_ticks >= target->timeout) {
if(target->repeat == 0) {
timer_stop(target);
} else {
target->timeout = _timer_ticks + target->repeat;
}
target->timeout_cb();
}
}
}
/**
* @brief background ticks, timer repeat invoking interval 1ms.
* @param None.
* @retval None.
*/
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM4)
{
_timer_ticks++;//1ms++
}
}
void bps_InitTime(void){
HAL_TIM_Base_Start_IT(&htim4); //使用定時器的時候調用這個函數啓動
}
struct Timer timer1;
struct Timer timer2;
void timer1_callback()
{
printf("timer1 timeout!\r\n");
}
void timer2_callback()
{
printf("timer2 timeout!\r\n");
}
int main()
{
timer_init(&timer1, timer1_callback, 1000, 1000); //1s loop
timer_start(&timer1);
timer_init(&timer2, timer2_callback, 50, 0); //50ms delay
timer_start(&timer2);
bps_InitTime();
while(1) {
timer_loop();
}
}