2023-03-23 17:00:05 +08:00

313 lines
15 KiB
Plaintext
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

C51 COMPILER V9.60.0.0 TIMER 03/23/2023 16:31:25 PAGE 1
C51 COMPILER V9.60.0.0, COMPILATION OF MODULE TIMER
OBJECT MODULE PLACED IN .\Objects\timer.obj
COMPILER INVOKED BY: d:\Keil_v5\C51\BIN\C51.EXE mycode\timer.c OPTIMIZE(8,SPEED) BROWSE INCDIR(.\myinc) DEBUG OBJECTEXTE
-ND PRINT(.\Listings\timer.lst) TABS(2) OBJECT(.\Objects\timer.obj)
line level source
1 /*---------------------------------------------------------------------*/
2 /* --- STC MCU Limited ------------------------------------------------*/
3 /* --- STC 1T Series MCU Demo Programme -------------------------------*/
4 /* --- Mobile: (86)13922805190 ----------------------------------------*/
5 /* --- Fax: 86-0513-55012956,55012947,55012969 ------------------------*/
6 /* --- Tel: 86-0513-55012928,55012929,55012966 ------------------------*/
7 /* --- Web: www.STCMCU.com --------------------------------------------*/
8 /* --- Web: www.STCMCUDATA.com ---------------------------------------*/
9 /* --- QQ: 800003751 -------------------------------------------------*/
10 /* 如果要在程序中使用此代码,请在程序中注明使用了STC的资料及程序 */
11 /*---------------------------------------------------------------------*/
12
13 /************* 功能说明 **************
14
15 本文件为STC8系列的定时器初始化和中断程序,用户可以在这个文件中修改自己需要的中断程序.
16
17 ******************************************/
18
19 #include "timer.h"
20 #include "GPIO.h"
21 #include "common.h"
22 u32 timex;
23 static u16 displaytime = 0;
24 u8 displaydata = 0;
25 /********************* Timer0中断函数************************/
26 void timer0_int(void) interrupt TIMER0_VECTOR
27 {
28 1 if (sunmode == 3)
29 1 {
30 2 if (timex <= 400 << subdivision3)
31 2 DIR = 0;
32 2 }
33 1 if (timex <= 0)
34 1 timex = 0;
35 1 else
36 1 {
37 2
38 2 if (pog == 1)
39 2 {
40 3 timex--;
41 3 PUL = ~PUL;
42 3 }
43 2 }
44 1 }
45
46 /********************* Timer1中断函数************************/
47 void timer1_int(void) interrupt TIMER1_VECTOR
48 {
49 1 }
50
51 /********************* Timer2中断函数************************/
52 void timer2_int(void) interrupt TIMER2_VECTOR
53 {
54 1 displaytime++;
C51 COMPILER V9.60.0.0 TIMER 03/23/2023 16:31:25 PAGE 2
55 1 if (displaytime >= 200)
56 1 {
57 2 if (DIR)
58 2 {
59 3 if (displaydata >= 7)
60 3 displaydata = 0;
61 3 else
62 3 displaydata++;
63 3 }
64 2 else
65 2 {
66 3 if (displaydata <= 0)
67 3 displaydata = 7;
68 3 else
69 3 displaydata--;
70 3 }
71 2 displaytime = 0;
72 2 }
73 1 if (keyflag)
74 1 keytime++;
75 1 else
76 1 keytime = 0;
77 1 }
78
79 /********************* Timer3中断函数************************/
80 void timer3_int(void) interrupt TIMER3_VECTOR
81 {
82 1 }
83
84 /********************* Timer4中断函数************************/
85 void timer4_int(void) interrupt TIMER4_VECTOR
86 {
87 1 }
88
89 //========================================================================
90 // 函数: u8 Timer_Inilize(u8 TIM, TIM_InitTypeDef *TIMx)
91 // 描述: 定时器初始化程序.
92 // 参数: TIMx: 结构参数,请参考timer.h里的定义.
93 // 返回: 成功返回0, 空操作返回1,错误返回2.
94 // 版本: V1.0, 2012-10-22
95 //========================================================================
96 u8 Timer_Inilize(u8 TIM, TIM_InitTypeDef *TIMx)
97 {
98 1 if (TIM > Timer4)
99 1 return 1; // 空操作
100 1
101 1 if (TIM == Timer0)
102 1 {
103 2 Timer0_Stop(); // 停止计数
104 2 if (TIMx->TIM_Interrupt == ENABLE)
105 2 Timer0_InterruptEnable(); // 允许中断
106 2 else
107 2 Timer0_InterruptDisable(); // 禁止中断
108 2 if (TIMx->TIM_Priority > Priority_3)
109 2 return 2; // 错误
110 2 Timer0_Priority(TIMx->TIM_Priority); // 指定中断优先级(低到高) Priority_0,Priority_1,Priority_2,Pr
-iority_3
111 2
112 2 if (TIMx->TIM_Mode >= TIM_16BitAutoReloadNoMask)
113 2 return 2; // 错误
114 2 TMOD = (TMOD & ~0x03) | TIMx->TIM_Mode; // 工作模式,0: 16位自动重装, 1: 16位定时/计数, 2: 8位自动<E887AA>
-刈<>, 3: 不可屏蔽16位自动重装
C51 COMPILER V9.60.0.0 TIMER 03/23/2023 16:31:25 PAGE 3
115 2 if (TIMx->TIM_ClkSource == TIM_CLOCK_12T)
116 2 Timer0_12T(); // 12T
117 2 if (TIMx->TIM_ClkSource == TIM_CLOCK_1T)
118 2 Timer0_1T(); // 1T
119 2 if (TIMx->TIM_ClkSource == TIM_CLOCK_Ext)
120 2 Timer0_AsCounter(); // 对外计数或分频
121 2 else
122 2 Timer0_AsTimer(); // 定时
123 2 if (TIMx->TIM_ClkOut == ENABLE)
124 2 Timer0_CLKO_Enable(); // 输出时钟
125 2 else
126 2 Timer0_CLKO_Disable(); // 不输出时钟
127 2
128 2 T0_Load(TIMx->TIM_Value);
129 2 if (TIMx->TIM_Run == ENABLE)
130 2 Timer0_Run(); // 开始运行
131 2 return 0; // 成功
132 2 }
133 1
134 1 if (TIM == Timer1)
135 1 {
136 2 Timer1_Stop(); // 停止计数
137 2 if (TIMx->TIM_Interrupt == ENABLE)
138 2 Timer1_InterruptEnable(); // 允许中断
139 2 else
140 2 Timer1_InterruptDisable(); // 禁止中断
141 2 if (TIMx->TIM_Priority > Priority_3)
142 2 return 2; // 错误
143 2 Timer1_Priority(TIMx->TIM_Priority); // 指定中断优先级(低到高) Priority_0,Priority_1,Priority_2,Pr
-iority_3
144 2 if (TIMx->TIM_Mode >= TIM_16BitAutoReloadNoMask)
145 2 return 2; // 错误
146 2 TMOD = (TMOD & ~0x30) | (TIMx->TIM_Mode << 4); // 工作模式,0: 16位自动重装, 1: 16位定时/计数, 2: 8
-位自动重装, 3: 停止工作
147 2 if (TIMx->TIM_ClkSource == TIM_CLOCK_12T)
148 2 Timer1_12T(); // 12T
149 2 if (TIMx->TIM_ClkSource == TIM_CLOCK_1T)
150 2 Timer1_1T(); // 1T
151 2 if (TIMx->TIM_ClkSource == TIM_CLOCK_Ext)
152 2 Timer1_AsCounter(); // 对外计数或分频
153 2 else
154 2 Timer1_AsTimer(); // 定时
155 2 if (TIMx->TIM_ClkOut == ENABLE)
156 2 Timer1_CLKO_Enable(); // 输出时钟
157 2 else
158 2 Timer1_CLKO_Disable(); // 不输出时钟
159 2
160 2 T1_Load(TIMx->TIM_Value);
161 2 if (TIMx->TIM_Run == ENABLE)
162 2 Timer1_Run(); // 开始运行
163 2 return 0; // 成功
164 2 }
165 1
166 1 if (TIM == Timer2) // Timer2,固定为16位自动重装, 中断无优先级
167 1 {
168 2 Timer2_Stop(); // 停止计数
169 2 if (TIMx->TIM_Interrupt == ENABLE)
170 2 Timer2_InterruptEnable(); // 允许中断
171 2 else
172 2 Timer2_InterruptDisable(); // 禁止中断
173 2 if (TIMx->TIM_ClkSource > TIM_CLOCK_Ext)
174 2 return 2;
C51 COMPILER V9.60.0.0 TIMER 03/23/2023 16:31:25 PAGE 4
175 2 if (TIMx->TIM_ClkSource == TIM_CLOCK_12T)
176 2 Timer2_12T(); // 12T
177 2 if (TIMx->TIM_ClkSource == TIM_CLOCK_1T)
178 2 Timer2_1T(); // 1T
179 2 if (TIMx->TIM_ClkSource == TIM_CLOCK_Ext)
180 2 Timer2_AsCounter(); // 对外计数或分频
181 2 else
182 2 Timer2_AsTimer(); // 定时
183 2 if (TIMx->TIM_ClkOut == ENABLE)
184 2 Timer2_CLKO_Enable(); // 输出时钟
185 2 else
186 2 Timer2_CLKO_Disable(); // 不输出时钟
187 2
188 2 T2_Load(TIMx->TIM_Value);
189 2 if (TIMx->TIM_Run == ENABLE)
190 2 Timer2_Run(); // 开始运行
191 2 return 0; // 成功
192 2 }
193 1
194 1 if (TIM == Timer3) // Timer3,固定为16位自动重装, 中断无优先级
195 1 {
196 2 Timer3_Stop(); // 停止计数
197 2 if (TIMx->TIM_Interrupt == ENABLE)
198 2 Timer3_InterruptEnable(); // 允许中断
199 2 else
200 2 Timer3_InterruptDisable(); // 禁止中断
201 2 if (TIMx->TIM_ClkSource > TIM_CLOCK_Ext)
202 2 return 2;
203 2 if (TIMx->TIM_ClkSource == TIM_CLOCK_12T)
204 2 Timer3_12T(); // 12T
205 2 if (TIMx->TIM_ClkSource == TIM_CLOCK_1T)
206 2 Timer3_1T(); // 1T
207 2 if (TIMx->TIM_ClkSource == TIM_CLOCK_Ext)
208 2 Timer3_AsCounter(); // 对外计数或分频
209 2 else
210 2 Timer3_AsTimer(); // 定时
211 2 if (TIMx->TIM_ClkOut == ENABLE)
212 2 Timer3_CLKO_Enable(); // 输出时钟
213 2 else
214 2 Timer3_CLKO_Disable(); // 不输出时钟
215 2
216 2 T3_Load(TIMx->TIM_Value);
217 2 if (TIMx->TIM_Run == ENABLE)
218 2 Timer3_Run(); // 开始运行
219 2 return 0; // 成功
220 2 }
221 1
222 1 if (TIM == Timer4) // Timer3,固定为16位自动重装, 中断无优先级
223 1 {
224 2 Timer4_Stop(); // 停止计数
225 2 if (TIMx->TIM_Interrupt == ENABLE)
226 2 Timer4_InterruptEnable(); // 允许中断
227 2 else
228 2 Timer4_InterruptDisable(); // 禁止中断
229 2 if (TIMx->TIM_ClkSource > TIM_CLOCK_Ext)
230 2 return 2;
231 2 if (TIMx->TIM_ClkSource == TIM_CLOCK_12T)
232 2 Timer4_12T(); // 12T
233 2 if (TIMx->TIM_ClkSource == TIM_CLOCK_1T)
234 2 Timer4_1T(); // 1T
235 2 if (TIMx->TIM_ClkSource == TIM_CLOCK_Ext)
236 2 Timer4_AsCounter(); // 对外计数或分频
C51 COMPILER V9.60.0.0 TIMER 03/23/2023 16:31:25 PAGE 5
237 2 else
238 2 Timer4_AsTimer(); // 定时
239 2 if (TIMx->TIM_ClkOut == ENABLE)
240 2 Timer4_CLKO_Enable(); // 输出时钟
241 2 else
242 2 Timer4_CLKO_Disable(); // 不输出时钟
243 2
244 2 T4_Load(TIMx->TIM_Value);
245 2 if (TIMx->TIM_Run == ENABLE)
246 2 Timer4_Run(); // 开始运行
247 2 return 0; // 成功
248 2 }
249 1 return 2; // 错误
250 1 }
251 /************************ ?¨?±?÷???? ****************************/
252 void Timer_config(void)
253 {
254 1 TIM_InitTypeDef TIM_InitStructure; //?á???¨??
255 1 TIM_InitStructure.TIM_Mode = TIM_16BitAutoReload; //???¨?¤×÷????, TIM_16BitAutoRel
-oad,TIM_16Bit,TIM_8BitAutoReload,TIM_16BitAutoReloadNoMask
256 1 TIM_InitStructure.TIM_Priority = Priority_0; //???¨??????????(??????) Priority_0,P
-riority_1,Priority_2,Priority_3
257 1 TIM_InitStructure.TIM_Interrupt = ENABLE; //??????·????í, ENABLE?òDISABLE
258 1 TIM_InitStructure.TIM_ClkSource = TIM_CLOCK_1T; //???¨?±????, TIM_CLOCK_1T,TIM_C
-LOCK_12T,TIM_CLOCK_Ext
259 1 TIM_InitStructure.TIM_ClkOut = DISABLE; //??·?????????????, ENABLE?òDISABLE
260 1 TIM_InitStructure.TIM_Value = 65536UL - (MAIN_Fosc / 100000UL); //????,
261 1 TIM_InitStructure.TIM_Run = ENABLE; //??·????????ó?????¨?±?÷, ENABLE?
-òDISABLE
262 1 Timer_Inilize(Timer0, &TIM_InitStructure); //??????Timer0 Timer0,Timer1,Timer2,
-Timer3,Timer4
263 1
264 1 TIM_InitStructure.TIM_ClkSource = TIM_CLOCK_1T; //???¨?±????, TIM_CLOCK_1T,TIM_CLO
-CK_12T,TIM_CLOCK_Ext
265 1 TIM_InitStructure.TIM_ClkOut = DISABLE; //??·?????????????, ENABLE?òDISABLE
266 1 TIM_InitStructure.TIM_Value = 65536UL - (MAIN_Fosc / 1000UL); //????,
267 1 TIM_InitStructure.TIM_Run = ENABLE; //??·????????ó?????¨?±?÷, ENABLE?ò
-DISABLE
268 1 Timer_Inilize(Timer2, &TIM_InitStructure);
269 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 1127 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 7 8
IDATA SIZE = ---- ----
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)