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