1. 程式人生 > >轉載:軟件定時器

轉載:軟件定時器

軟件定時器 def plus fine loop 兩個 `` hand handle

C編寫的,高度可移植,軟件定時器

通過模擬多個軟件定時器來實現硬件的一些功能。

MultiTimer 是一個軟件定時器擴展模塊,可無限擴展你所需的定時器任務,取代傳統的標誌位判斷方式, 更優雅更便捷地管理程序的時間觸發時序。一共有3個文件:

multi_timer.c--定時器c源文件;

multi_timer.h--定時器h頭文件;

main--用戶代碼文件。

 1 #include"multi_timer.h"
 2 
 3 struct Timer timer1;
 4 
 5 struct Timer timer2;
 6 
 7 voidtimer1_callback()
 8
9 { 10 11 printf( "timer1 timeout!rn"); 12 13 } 14 15 voidtimer2_callback() 16 17 { 18 19 printf( "timer2 timeout!rn"); 20 21 }
1、先申請一個定時器管理handle

```

struct Timer timer1;

```

2、初始化定時器對象,註冊定時器回調處理函數,設置定時時間(ms),循環定時觸發時間

```

timer_init(struct Timer* handle, void(*timeout_cb)(), uint32_t timeout, uint32_t repeat);

```

3.啟動定時器 ``` timer_start(&timer1); ``` 4.設置1ms的硬件定時器循環調用 *timer_ticks()* 以提供時間基準 ``` void HAL_SYSTICK_Callback(void) { timer_ticks(); } ``` 5.在主循環調用定時器後臺處理函數 ``` int main() { ... while(1) { ... timer_loop(); } } ```

上面,我們定義了兩個定時器,第一個以每1秒一次進行循環執行,第二個定時器50ms之後只執行一次,執行之後,可以通過相應的回調函數打印出來,裏面就可以添加我們的定時任務了。

  1 /*
  2 
  3 * multi_timer.h
  4 
  5 */
  6 
  7 #ifndef _MULTI_TIMER_H_
  8 
  9 #define_MULTI_TIMER_H_
 10 
 11 #include "stdint.h"
 12 
 13 typedef structTimer {
 14 
 15 uint32_t timeout;
 16 
 17 uint32_t repeat;
 18 
 19 void(*timeout_cb)( void);
 20 
 21 structTimer* next;
 22 
 23 }Timer;
 24 
 25 #ifdef __cplusplus
 26 
 27 extern"C"{
 28 
 29 #endif
 30 
 31 voidtimer_init(structTimer* handle, void(*timeout_cb)(), uint32_t timeout, uint32_t repeat);
 32 
 33 inttimer_start(structTimer* handle);
 34 
 35 voidtimer_stop(structTimer* handle);
 36 
 37 voidtimer_ticks(void);
 38 
 39 voidtimer_loop(void);
 40 
 41 // void timer_again(struct Timer* handle);
 42 
 43 // void timer_set_repeat(struct Timer* handle, uint32_t repeat);
 44 
 45 #ifdef __cplusplus
 46 
 47 }
 48 
 49 #endif
 50 
 51 #endif
 52 
 53 /*
 54 
 55 * multi_timer.c
 56 
 57 */
 58 
 59 #include "multi_timer.h"
 60 
 61 //timer handle list head.
 62 
 63 staticstruct Timer* head_handle = NULL;
 64 
 65 //Timer ticks
 66 
 67 staticuint32_t _timer_ticks = 0;
 68 
 69 /**
 70 
 71 * @briefInitializes the timer struct handle.
 72 
 73 * @paramhandle: the timer handle strcut.
 74 
 75 * @paramtimeout_cb: timeout callback.
 76 
 77 * @paramrepeat: repeat interval time.
 78 
 79 * @retvalNone
 80 
 81 */
 82 
 83 void timer_init(struct Timer* handle, void(*timeout_cb)(), uint32_t timeout, uint32_t repeat)
 84 
 85 {
 86 
 87 // memset(handle, sizeof(struct Timer), 0);
 88 
 89 handle->timeout_cb = timeout_cb;
 90 
 91 handle->timeout = _timer_ticks + timeout;
 92 
 93 handle->repeat = repeat;
 94 
 95 }
 96 
 97 /**
 98 
 99 * @briefStart the timer work, add the handle into work list.
100 
101 * @parambtn: target handle strcut.
102 
103 * @retval0: succeed. -1: already exist.
104 
105 */
106 
107 int timer_start(struct Timer* handle)
108 
109 {
110 
111 struct Timer* target = head_handle;
112 
113 while(target) {
114 
115 if(target == handle) return-1; //already exist.
116 
117 target = target->next;
118 
119 }
120 
121 handle->next = head_handle;
122 
123 head_handle = handle;
124 
125 return0;
126 
127 }
128 
129 /**
130 
131 * @briefStop the timer work, remove the handle off work list.
132 
133 * @paramhandle: target handle strcut.
134 
135 * @retvalNone
136 
137 */
138 
139 void timer_stop(struct Timer* handle)
140 
141 {
142 
143 struct Timer** curr;
144 
145 for(curr = &head_handle; *curr; ) {
146 
147 struct Timer* entry = *curr;
148 
149 if(entry == handle) {
150 
151 *curr = entry->next;
152 
153 // free(entry);
154 
155 } else
156 
157 curr = &entry->next;
158 
159 }
160 
161 }
162 
163 /**
164 
165 * @briefmain loop.
166 
167 * @paramNone.
168 
169 * @retvalNone
170 
171 */
172 
173 void timer_loop()
174 
175 {
176 
177 struct Timer* target;
178 
179 for(target=head_handle; target; target=target->next) {
180 
181 if(_timer_ticks >= target->timeout) {
182 
183 if(target->repeat == 0) {
184 
185 timer_stop(target);
186 
187 } else{
188 
189 target->timeout = _timer_ticks + target->repeat;
190 
191 }
192 
193 target->timeout_cb();
194 
195 }
196 
197 }
198 
199 }
200 
201 /**
202 
203 * @briefbackground ticks, timer repeat invoking interval 1ms.
204 
205 * @paramNone.
206 
207 * @retvalNone.
208 
209 */
210 
211 void timer_ticks()
212 
213 {
214 
215 _timer_ticks++;
216 
217 }

  

轉載:軟件定時器