转载:软件定时器

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 }

 

  

 

转载于:https://www.cnblogs.com/miaomiao92/p/9710685.html

你可能感兴趣的:(转载:软件定时器)