欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136
標題:
C語言寫的單片機定時器回調函數
[打印本頁]
作者:
ddk2018
時間:
2018-7-15 16:18
標題:
C語言寫的單片機定時器回調函數
C語言寫的定時器回調函數,可以移植到任何有定時器的單片機上,只要設置單片機定時器1ms中斷,這樣在主循環中定時時間到了就會調用相應的函數,這樣我們就有了無數個軟件定時器使用,軟件定時器可以啟動,停止,一次性的。從別的網站下載的,分享給大家。具體參考附件程序。
0.png
(37.9 KB, 下載次數: 36)
下載附件
2018-7-15 16:51 上傳
單片機源程序如下:
/*
* multi_timer.c
*
* Created on: 20161229
* @Author : 曉宇 <karaxiaoyu@gmail.com>
* @id : 芯片之家
* @version :V1.0.0
*/
#include "multi_timer.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 timer_ticks()
{
_timer_ticks++;
}
復制代碼
所有資料51hei提供下載:
MultiTimer.rar
(2.14 KB, 下載次數: 64)
2018-7-15 16:18 上傳
點擊文件名下載附件
定時器回掉函數
下載積分: 黑幣 -5
作者:
Jodelin
時間:
2020-4-20 08:35
時鐘_timer_ticks不清零,timeout一直累加,這樣會出問題的
作者:
Jodelin
時間:
2020-4-20 09:30
這個程序里面會出現一個問題,就是數據溢出的問題,把判斷條件改下就行了
if(_timer_ticks >= target->timeout)改為 if(_timer_ticks == target->timeout)
這樣就不會在數據溢出后從零開始出現的誤判問題
歡迎光臨 (http://m.raoushi.com/bbs/)
Powered by Discuz! X3.1