-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
113 lines (93 loc) · 1.63 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* 单片机实现轮询多个任务的方法,不同任务的轮询时间可以设置 */
/* 此代码跑在8710Bn上 */
#include "device.h"
#include "gpio_api.h" // mbed
#include "main.h"
#include "timer_api.h"
#define true (1)
#define false (0)
#define TASKS_MAX (6)
gtimer_t MTimer1;
typedef struct TASK_COMPONENTS
{
bool Run;
unsigned char Timer;
unsigned char ItvTime;
void( *TaskHook )( void );
} TASK_COMPONENTS;
void func0(void)
{
printf("func0.\n");
}
void func1(void)
{
printf("func1.\n");
}
void func2(void)
{
printf("func2.\n");
}
void func3(void)
{
printf("func3.\n");
}
void func4(void)
{
printf("func4.\n");
}
void func5(void)
{
printf("func5.\n");
}
/*设置Timer和ItvTime的大小表示轮询的时间,数值设置大了则轮询的时间间隔大一些 */
TASK_COMPONENTS Task[TASKS_MAX] = {\
{false, 5, 5, func0}, \
{false, 10, 10, func1}, \
{false, 15, 15, func2},\
{false, 20, 20, func3},\
{false, 25, 25, func4},\
{false, 30, 30, func5},
};
void TaskProcess( void )
{
unsigned char i ;
for ( i = 0; i < TASKS_MAX; i++ )
{
if ( Task[i].Run == true )
{
Task[i].TaskHook();
Task[i].Run = false;
}
}
}
void TaskMark( void )
{
unsigned char i ;
for ( i = 0; i < TASKS_MAX; i++ )
{
if ( Task[i].Timer != 0 )
{
Task[i].Timer--;
if ( 0 == Task[i].Timer )
{
Task[i].Timer = Task[i].ItvTime;
Task[i].Run = true;
}
}
}
}
/* 100ms定时器中断 */
void timer1_timeout_handler(unsigned int TimeOut)
{
TaskMark();
}
void main(void)
{
gtimer_init(&MTimer1, TIMER2);
/* 1ms进入一次中断 */
gtimer_start_periodical(&MTimer1, 1000, (void*)timer1_timeout_handler, NULL);
while ( 1 )
{
TaskProcess();//任务处理函数
}
}