標(biāo)題: 完全由C語(yǔ)言編寫,高度可移植,超級(jí)牛逼的軟件定時(shí)器! [打印本頁(yè)]
作者: 李牧林 時(shí)間: 2019-9-14 19:57
標(biāo)題: 完全由C語(yǔ)言編寫,高度可移植,超級(jí)牛逼的軟件定時(shí)器!
帖子來(lái)自芯片之家公眾號(hào)。
作為一個(gè)搞電子嵌入式的你來(lái)說(shuō),定時(shí)器相信絕對(duì)不會(huì)陌生,去翻看STM32的官方數(shù)據(jù)手冊(cè),可能有四分之一的篇幅都在講定時(shí)器,可見定時(shí)器的重要性,定時(shí)器定時(shí)器嘛,最重要的功能還是定時(shí)。之前經(jīng)常有師弟跟我抱怨,哎呀,這個(gè)單片機(jī)怎么只有2個(gè)定時(shí)器,我都不夠用了,怎么辦呀,然后一頭霧水,其實(shí),只要有一個(gè)硬件定時(shí)器,在實(shí)際不是要求特別特別精準(zhǔn)的場(chǎng)合,我們可以模擬很多個(gè)軟件定時(shí)器出來(lái),今天,群主給大家分享一個(gè)完全由C語(yǔ)言編寫,高度可移植,超級(jí)牛逼的軟件定時(shí)器!
MultiTimer 是一個(gè)軟件定時(shí)器擴(kuò)展模塊,可無(wú)限擴(kuò)展你所需的定時(shí)器任務(wù),取代傳統(tǒng)的標(biāo)志位判斷方式, 更優(yōu)雅更便捷地管理程序的時(shí)間觸發(fā)時(shí)序。一共有3個(gè)文件:
multi_timer.c--定時(shí)器c源文件;
multi_timer.h--定時(shí)器h頭文件;
main--用戶代碼文件。
我們直接上代碼:
- /*
- * main.c
- *
- * Created on: 20161229
- * @Author : 曉宇
- * @version :V1.0.0
- *
- * ,%%%%%%%%,
- * ,%%/\%%%%/\%%
- * ,%%%\c''''J/%%%
- * %. %%%%/ o o \%%%
- * `%%. %%%% |%%%
- * `%% `%%%%(__Y__)%%'
- * // ;%%%%`\-/%%%'
- * (( / `%%%%%%%'
- * \\ .' |
- * \\ / \ | |
- * \\/ ) | |
- * \ /_ | |__
- * (____________))))))) 攻城獅
- *
- */
- #include "multi_timer.h"
- 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);
- while(1) {
-
- timer_loop();
- }
- }
- void HAL_SYSTICK_Callback(void)
- {
- timer_ticks(); //1ms ticks
- }
復(fù)制代碼
1、先申請(qǐng)一個(gè)定時(shí)器管理handle
2、初始化定時(shí)器對(duì)象,注冊(cè)定時(shí)器回調(diào)處理函數(shù),設(shè)置定時(shí)時(shí)間(ms),循環(huán)定時(shí)觸發(fā)時(shí)間
- timer_init(struct Timer* handle, void(*timeout_cb)(), uint32_t timeout, uint32_t repeat);
復(fù)制代碼
3.啟動(dòng)定時(shí)器
4.設(shè)置1ms的硬件定時(shí)器循環(huán)調(diào)用 *timer_ticks()* 以提供時(shí)間基準(zhǔn)
- void HAL_SYSTICK_Callback(void)
- {
- timer_ticks();
- }
復(fù)制代碼
5.在主循環(huán)調(diào)用定時(shí)器后臺(tái)處理函數(shù)
- int main()
- { ...
- while(1) {
- ...
- timer_loop();
- }
- }
復(fù)制代碼
上面,我們定義了兩個(gè)定時(shí)器,第一個(gè)以每1秒一次進(jìn)行循環(huán)執(zhí)行,第二個(gè)定時(shí)器50ms之后只執(zhí)行一次,執(zhí)行之后,可以通過(guò)相應(yīng)的回調(diào)函數(shù)打印出來(lái),里面就可以添加我們的定時(shí)任務(wù)了。怎么樣,是不是很簡(jiǎn)單,只需要設(shè)置好定時(shí)基準(zhǔn),申請(qǐng)好定時(shí)器,初始化完畢,你想要的效果,是定時(shí)一次還是進(jìn)行循環(huán),都沒(méi)問(wèn)題,多個(gè)定時(shí)器之間使用鏈表來(lái)實(shí)現(xiàn),可以啟動(dòng)暫停每個(gè)定時(shí)器,用起來(lái)之后,是不是很爽?具體的代碼,需要大家仔細(xì)去推敲其中的原理,當(dāng)然直接用也是可以的。
部分源代碼展示:
- /*
- * multi_timer.h
- *
- * Created on: 20161229
- * @Author : 曉宇
- * @version :V1.0.0
- */
- #ifndef _MULTI_TIMER_H_
- #define _MULTI_TIMER_H_
- #include "stdint.h"
- typedef struct Timer {
- uint32_t timeout;
- uint32_t repeat;
- void (*timeout_cb)(void);
- struct Timer* next;
- }Timer;
- #ifdef __cplusplus
- extern "C" {
- #endif
- void timer_init(struct Timer* handle, void(*timeout_cb)(), uint32_t timeout, uint32_t repeat);
- int timer_start(struct Timer* handle);
- void timer_stop(struct Timer* handle);
- void timer_ticks(void);
- void timer_loop(void);
- // void timer_again(struct Timer* handle);
- // void timer_set_repeat(struct Timer* handle, uint32_t repeat);
- #ifdef __cplusplus
- }
- #endif
- #endif/*
- * multi_timer.c
- *
- * Created on: 20161229
- * @Author : 曉宇
- * @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++;
- }
復(fù)制代碼
全部資料51hei下載地址:
MultiTimer.zip
(2.26 KB, 下載次數(shù): 47)
2019-9-14 20:02 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
作者: leo2002zhang 時(shí)間: 2019-9-16 10:30
學(xué)習(xí)一下,給樓主點(diǎn)個(gè)贊。軟定時(shí)還是經(jīng)常用到的。
作者: 李牧林 時(shí)間: 2019-9-23 08:08
32用的比較方便,51直接用標(biāo)志位去完成軟定時(shí)器功能了
作者: jizhongbiao 時(shí)間: 2020-9-10 00:13
把github地址貼出來(lái)好一點(diǎn),這一個(gè)是有開源協(xié)議的。
歡迎光臨 (http://www.zg4o1577.cn/bbs/) |
Powered by Discuz! X3.1 |
主站蜘蛛池模板:
欧美男人亚洲天堂
|
美女天天操
|
日韩精品免费在线
|
国产亚洲一区精品
|
欧美区日韩区
|
国产成人精品免费
|
亚洲精品视频在线看
|
久久久久久综合
|
成人av片在线观看
|
亚洲一区自拍
|
欧美成人a
|
91久久精品日日躁夜夜躁欧美
|
久久精品国产一区二区电影
|
99视频免费在线
|
嫩草视频在线免费观看
|
成人国产在线观看
|
欧美日韩黄色一级片
|
久久99精品久久久久久国产越南
|
久久中文一区二区
|
精品一区av
|
亚洲日韩中文字幕一区
|
久久精品在线播放
|
色狠狠一区|
久久网站免费视频
|
青青草av网站
|
91精品国产美女在线观看
|
一区二区三区免费
|
欧美日韩91
|
www国产成人|
在线婷婷|
免费精品
|
羞羞视频网站
|
日本黄视频在线观看
|
日本不卡一区二区三区在线观看
|
亚洲欧美bt
|
午夜a级理论片915影院
|
久久精品亚洲一区二区三区浴池
|
欧美激情欧美激情在线五月
|
成人在线观看免费
|
天堂在线91
|
操操网站
|