2023-04-11 08:51:37 +08:00

282 lines
13 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 04/11/2023 08:44:51 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 "PWM.h"
21 #include "GPIO.h"
22 u8 KILL_time = 0;
23 u8 pwm_direction = 0;
24
25 /********************* Timer0中断函数************************/
26 void timer0_int(void) interrupt TIMER0_VECTOR
27 {
28 1 if (KILL_time)
29 1 KILL_time--;
30 1 if (!power3_gpio)
31 1 {
32 2 if (pwm_direction)
33 2 {
34 3 PWMB_Duty.PWM5_Duty++;
35 3 if (PWMB_Duty.PWM5_Duty >= 1500)
36 3 pwm_direction = 0;
37 3 }
38 2 else
39 2 {
40 3 PWMB_Duty.PWM5_Duty--;
41 3 if (!PWMB_Duty.PWM5_Duty)
42 3 pwm_direction = 1;
43 3 }
44 2 }
45 1 else
46 1 {
47 2 PWMB_Duty.PWM5_Duty = 1500;
48 2 }
49 1 }
50
51 /********************* Timer1中断函数************************/
52 void timer1_int(void) interrupt TIMER1_VECTOR
53 {
54 1 }
C51 COMPILER V9.60.0.0 TIMER 04/11/2023 08:44:51 PAGE 2
55
56 /********************* Timer2中断函数************************/
57 void timer2_int(void) interrupt TIMER2_VECTOR
58 {
59 1 }
60
61 /********************* Timer3中断函数************************/
62 void timer3_int(void) interrupt TIMER3_VECTOR
63 {
64 1 }
65
66 /********************* Timer4中断函数************************/
67 void timer4_int(void) interrupt TIMER4_VECTOR
68 {
69 1 }
70
71 //========================================================================
72 // 函数: u8 Timer_Inilize(u8 TIM, TIM_InitTypeDef *TIMx)
73 // 描述: 定时器初始化程序.
74 // 参数: TIMx: 结构参数,请参考timer.h里的定义.
75 // 返回: 成功返回0, 空操作返回1,错误返回2.
76 // 版本: V1.0, 2012-10-22
77 //========================================================================
78 u8 Timer_Inilize(u8 TIM, TIM_InitTypeDef *TIMx)
79 {
80 1 if (TIM > Timer4)
81 1 return 1; // 空操作
82 1
83 1 if (TIM == Timer0)
84 1 {
85 2 Timer0_Stop(); // 停止计数
86 2 if (TIMx->TIM_Interrupt == ENABLE)
87 2 Timer0_InterruptEnable(); // 允许中断
88 2 else
89 2 Timer0_InterruptDisable(); // 禁止中断
90 2 if (TIMx->TIM_Priority > Priority_3)
91 2 return 2; // 错误
92 2 Timer0_Priority(TIMx->TIM_Priority); // 指定中断优先级(低到高) Priority_0,Priority_1,Priority_2,Pr
-iority_3
93 2
94 2 if (TIMx->TIM_Mode >= TIM_16BitAutoReloadNoMask)
95 2 return 2; // 错误
96 2 TMOD = (TMOD & ~0x03) | TIMx->TIM_Mode; // 工作模式,0: 16位自动重装, 1: 16位定时/计数, 2: 8位自动<E887AA>
-刈<>, 3: 不可屏蔽16位自动重装
97 2 if (TIMx->TIM_ClkSource == TIM_CLOCK_12T)
98 2 Timer0_12T(); // 12T
99 2 if (TIMx->TIM_ClkSource == TIM_CLOCK_1T)
100 2 Timer0_1T(); // 1T
101 2 if (TIMx->TIM_ClkSource == TIM_CLOCK_Ext)
102 2 Timer0_AsCounter(); // 对外计数或分频
103 2 else
104 2 Timer0_AsTimer(); // 定时
105 2 if (TIMx->TIM_ClkOut == ENABLE)
106 2 Timer0_CLKO_Enable(); // 输出时钟
107 2 else
108 2 Timer0_CLKO_Disable(); // 不输出时钟
109 2
110 2 T0_Load(TIMx->TIM_Value);
111 2 if (TIMx->TIM_Run == ENABLE)
112 2 Timer0_Run(); // 开始运行
113 2 return 0; // 成功
114 2 }
C51 COMPILER V9.60.0.0 TIMER 04/11/2023 08:44:51 PAGE 3
115 1
116 1 if (TIM == Timer1)
117 1 {
118 2 Timer1_Stop(); // 停止计数
119 2 if (TIMx->TIM_Interrupt == ENABLE)
120 2 Timer1_InterruptEnable(); // 允许中断
121 2 else
122 2 Timer1_InterruptDisable(); // 禁止中断
123 2 if (TIMx->TIM_Priority > Priority_3)
124 2 return 2; // 错误
125 2 Timer1_Priority(TIMx->TIM_Priority); // 指定中断优先级(低到高) Priority_0,Priority_1,Priority_2,Pr
-iority_3
126 2 if (TIMx->TIM_Mode >= TIM_16BitAutoReloadNoMask)
127 2 return 2; // 错误
128 2 TMOD = (TMOD & ~0x30) | (TIMx->TIM_Mode << 4); // 工作模式,0: 16位自动重装, 1: 16位定时/计数, 2: 8
-位自动重装, 3: 停止工作
129 2 if (TIMx->TIM_ClkSource == TIM_CLOCK_12T)
130 2 Timer1_12T(); // 12T
131 2 if (TIMx->TIM_ClkSource == TIM_CLOCK_1T)
132 2 Timer1_1T(); // 1T
133 2 if (TIMx->TIM_ClkSource == TIM_CLOCK_Ext)
134 2 Timer1_AsCounter(); // 对外计数或分频
135 2 else
136 2 Timer1_AsTimer(); // 定时
137 2 if (TIMx->TIM_ClkOut == ENABLE)
138 2 Timer1_CLKO_Enable(); // 输出时钟
139 2 else
140 2 Timer1_CLKO_Disable(); // 不输出时钟
141 2
142 2 T1_Load(TIMx->TIM_Value);
143 2 if (TIMx->TIM_Run == ENABLE)
144 2 Timer1_Run(); // 开始运行
145 2 return 0; // 成功
146 2 }
147 1
148 1 if (TIM == Timer2) // Timer2,固定为16位自动重装, 中断无优先级
149 1 {
150 2 Timer2_Stop(); // 停止计数
151 2 if (TIMx->TIM_Interrupt == ENABLE)
152 2 Timer2_InterruptEnable(); // 允许中断
153 2 else
154 2 Timer2_InterruptDisable(); // 禁止中断
155 2 if (TIMx->TIM_ClkSource > TIM_CLOCK_Ext)
156 2 return 2;
157 2 if (TIMx->TIM_ClkSource == TIM_CLOCK_12T)
158 2 Timer2_12T(); // 12T
159 2 if (TIMx->TIM_ClkSource == TIM_CLOCK_1T)
160 2 Timer2_1T(); // 1T
161 2 if (TIMx->TIM_ClkSource == TIM_CLOCK_Ext)
162 2 Timer2_AsCounter(); // 对外计数或分频
163 2 else
164 2 Timer2_AsTimer(); // 定时
165 2 if (TIMx->TIM_ClkOut == ENABLE)
166 2 Timer2_CLKO_Enable(); // 输出时钟
167 2 else
168 2 Timer2_CLKO_Disable(); // 不输出时钟
169 2
170 2 T2_Load(TIMx->TIM_Value);
171 2 if (TIMx->TIM_Run == ENABLE)
172 2 Timer2_Run(); // 开始运行
173 2 return 0; // 成功
174 2 }
C51 COMPILER V9.60.0.0 TIMER 04/11/2023 08:44:51 PAGE 4
175 1
176 1 if (TIM == Timer3) // Timer3,固定为16位自动重装, 中断无优先级
177 1 {
178 2 Timer3_Stop(); // 停止计数
179 2 if (TIMx->TIM_Interrupt == ENABLE)
180 2 Timer3_InterruptEnable(); // 允许中断
181 2 else
182 2 Timer3_InterruptDisable(); // 禁止中断
183 2 if (TIMx->TIM_ClkSource > TIM_CLOCK_Ext)
184 2 return 2;
185 2 if (TIMx->TIM_ClkSource == TIM_CLOCK_12T)
186 2 Timer3_12T(); // 12T
187 2 if (TIMx->TIM_ClkSource == TIM_CLOCK_1T)
188 2 Timer3_1T(); // 1T
189 2 if (TIMx->TIM_ClkSource == TIM_CLOCK_Ext)
190 2 Timer3_AsCounter(); // 对外计数或分频
191 2 else
192 2 Timer3_AsTimer(); // 定时
193 2 if (TIMx->TIM_ClkOut == ENABLE)
194 2 Timer3_CLKO_Enable(); // 输出时钟
195 2 else
196 2 Timer3_CLKO_Disable(); // 不输出时钟
197 2
198 2 T3_Load(TIMx->TIM_Value);
199 2 if (TIMx->TIM_Run == ENABLE)
200 2 Timer3_Run(); // 开始运行
201 2 return 0; // 成功
202 2 }
203 1
204 1 if (TIM == Timer4) // Timer3,固定为16位自动重装, 中断无优先级
205 1 {
206 2 Timer4_Stop(); // 停止计数
207 2 if (TIMx->TIM_Interrupt == ENABLE)
208 2 Timer4_InterruptEnable(); // 允许中断
209 2 else
210 2 Timer4_InterruptDisable(); // 禁止中断
211 2 if (TIMx->TIM_ClkSource > TIM_CLOCK_Ext)
212 2 return 2;
213 2 if (TIMx->TIM_ClkSource == TIM_CLOCK_12T)
214 2 Timer4_12T(); // 12T
215 2 if (TIMx->TIM_ClkSource == TIM_CLOCK_1T)
216 2 Timer4_1T(); // 1T
217 2 if (TIMx->TIM_ClkSource == TIM_CLOCK_Ext)
218 2 Timer4_AsCounter(); // 对外计数或分频
219 2 else
220 2 Timer4_AsTimer(); // 定时
221 2 if (TIMx->TIM_ClkOut == ENABLE)
222 2 Timer4_CLKO_Enable(); // 输出时钟
223 2 else
224 2 Timer4_CLKO_Disable(); // 不输出时钟
225 2
226 2 T4_Load(TIMx->TIM_Value);
227 2 if (TIMx->TIM_Run == ENABLE)
228 2 Timer4_Run(); // 开始运行
229 2 return 0; // 成功
230 2 }
231 1 return 2; // 错误
232 1 }
233 /************************ ?¨?±?÷???? ****************************/
234 void Timer_config(void)
235 {
236 1 TIM_InitTypeDef TIM_InitStructure;
C51 COMPILER V9.60.0.0 TIMER 04/11/2023 08:44:51 PAGE 5
237 1 TIM_InitStructure.TIM_Mode = TIM_16BitAutoReload;
238 1 TIM_InitStructure.TIM_Priority = Priority_0;
239 1 TIM_InitStructure.TIM_Interrupt = ENABLE;
240 1 TIM_InitStructure.TIM_ClkSource = TIM_CLOCK_1T;
241 1 TIM_InitStructure.TIM_ClkOut = DISABLE;
242 1 TIM_InitStructure.TIM_Value = 65536UL - (MAIN_Fosc / 1000UL);
243 1 TIM_InitStructure.TIM_Run = ENABLE;
244 1 Timer_Inilize(Timer0, &TIM_InitStructure);
245 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 920 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 2 8
IDATA SIZE = ---- ----
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)