lin
2025-07-31 065ea569db06206874bbfa18eb25ff6121aec09b
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
///*****************************************
//  Copyright (C) 2009 - 2019
//  ITE Tech. Inc. All Rights Reserved
//  Proprietary and Confidential
///*****************************************
//   @file   <TimerProcess.c>
//   @author Jau - Chih.Tseng@ite.com.tw
//   @date   2019/01/03
//   @fileversion: ITE_HDMITX_SAMPLE_3.27
//******************************************/
 
#include "TimerProcess.h"
 
USHORT ucTickCount;
USHORT ucPrevTickCount ;
 
#ifdef _MCU_8051_
void init_timer(void)
{
   TMOD&=0xF0;
   TMOD|=0x01;     // Timer 0,Mode 0,16 bit
   TH0 = (65536L - (CLOCK/(1000L*MachineCycle))) >> 8;
   TL0 = (65536L - (CLOCK/(1000L*MachineCycle)));
   ET0 = TRUE;
   TR0 = TRUE;
////////////////////////////////////////////////////////////////
#if 0
   SCON = 0x50;         /* Setup serial port control register */
                       /* Mode 1: 8-bit uart var. baud rate */
                       /* REN: enable receiver */
   PCON&= 0x7F;        /* Clear SMOD bit in power ctrl reg */
   PCON|= 0x80;        /* This bit doubles the baud rate */
   TMOD&= 0xCF;        /* Setup timer/counter mode register */
                       /* Clear M1 and M0 for timer 1 */
   TMOD|= 0x20;        /* Set M1 for 8-bit autoreload timer */
   TH1 = BAUD_SETTING;
   TR1 = 1;             /* Start timer 1 */
   TI = 1;              /* Set TI to indicate ready to xmit */
#endif
#if 1
   TL2 = BAUD_SETTING;
   TH2 = BAUD_SETTING >> 8;
   RCAP2L = BAUD_SETTING;
   RCAP2H = BAUD_SETTING >> 8;
 
   SCON = 0x52;    //  For stdio.h (printf),  TI(SCON[1]) must set to "1"
   T2CON = 0x34;
   TR2 = FALSE;
   TR2 = TRUE;
#endif
////////////////////////////////////////////////////////////////
   EA = TRUE;
}
 
/*
USHORT CalTimer(ULONG SetupCnt)
{
   if (SetupCnt > ucTickCount) {
       return (0xffff - (SetupCnt - ucTickCount));
   }
   else {
       return (ucTickCount - SetupCnt);
   }
}
*/
/*
void timerCountDown(void)
{
 
}
*/
///////////////////////////////////////////////////////////////////////
void int_t0(void) interrupt 1// using 2
{
//             1000                                  CLOCK
//1ms = ------------------------------------ = ---------------------
//        1000000(u sec)                         1000*MachineCycle
//      ---------------- * MachineCycle
//           CLOCK
 
   TH0 = (65536L - (CLOCK/(2000L*MachineCycle))) >> 8;
   TL0 = (65536L - (CLOCK/(2000L*MachineCycle)));
   ucTickCount++;
   //timerCountDown(void);
}
 
#endif
 
 
TimerTask_Description TimerTask[MAX_TimerTaskC];
 
BYTE TimerTaskCheck(void);
BYTE MateTimerTask(System_TimerTask_Type cTimerTask);
BYTE NewTimerTaskAlloca(void);
 
BOOL IsTimeOut(USHORT timeout)
{
   USHORT diff ;
 
   diff = (ucTickCount - ucPrevTickCount) & 0xFFFF ;
   if ( diff > timeout ) {
       ucPrevTickCount = ucTickCount ;
       return TRUE ;
   }
   return FALSE ;
}
 
BOOL initTimerTask(void)
{
   BYTE j = 0;
   for(j = 0; j < MAX_TimerTaskC; j++) {
       TimerTask[j].TimerTask_name = SysTimerTask_non;
   }
   return TRUE;
}
 
 
 
void CreatTimerTask(System_TimerTask_Type cTimerTask,
   USHORT Acount,
   System_TimerTask_Run AutoRun)
{
       TimerTask[cTimerTask].TimerTask_name = cTimerTask;
       TimerTask[cTimerTask].TimerTask_AimCount = Acount;
       TimerTask[cTimerTask].TimerTask_TickCount = ucTickCount;
       TimerTask[cTimerTask].TimerTaskAutoRun = AutoRun;
 
// disable     printf("[%06u]---CreatTimerTask[ %s ] : [%u] : [%u] \n",ucTickCount,
// disable                                             TimeStr[cTimerTask],
// disable                                             TimerTask[cTimerTask].TimerTask_AimCount,
// disable                                             TimerTask[cTimerTask].TimerTask_TickCount);
}
 
USHORT CalTimer(ULONG SetupCnt)
{
   if (SetupCnt > ucTickCount) {
       return (0xffff - (SetupCnt  -  ucTickCount));
   }
   else {
       return (ucTickCount - SetupCnt);
   }
}
 
 
 
BOOL DestructTimerTask(System_TimerTask_Type cTimerTask)
{
// disable     printf("[%06u]DestructTimerTask[ %s ] : [%u] : [%u] \n",ucTickCount,
// disable                                             TimeStr[cTimerTask],
// disable                                             TimerTask[cTimerTask].TimerTask_AimCount,
// disable                                             ucTickCount);
   TimerTask[cTimerTask].TimerTask_name = SysTimerTask_non;
 
       return TRUE;
}
 
BOOL TimeOutCheck(System_TimerTask_Type cTimerTask)
{
   if (TimerTask[cTimerTask].TimerTask_name == SysTimerTask_non)
       return TRUE;
   else
       return FALSE;
 
}
 
void ProcessMultiTimerTask(void)
{
   BYTE j = 0,i = 0;
   System_TimerTask_Type ProcessCnt;
   for(ProcessCnt = 0; ProcessCnt < MAX_TimerTaskC; ProcessCnt++) {
       if (TimerTask[ProcessCnt].TimerTask_name == SysTimerTask_non)
           continue;
 
       if (CalTimer(TimerTask[ProcessCnt].TimerTask_TickCount) >
           TimerTask[ProcessCnt].TimerTask_AimCount )
       {
 
           if (TimerTask[ProcessCnt].TimerTaskAutoRun == SysTimerTask_AutoRun)
               TimerTask[ProcessCnt].TimerTask_TickCount = ucTickCount;
           else
               DestructTimerTask(TimerTask[ProcessCnt].TimerTask_name);
       }
 
   }
}