单片机练习 - 定时器
相关的特殊寄存器, 请查看有关资料
程序代码如下:
定时器
1
#include <reg52.H>
2
//使发光二极管按1000ms亮, 300ms暗, 300ms亮, 1000ms暗, 1000ms亮
.这样循环下去
.
3
/*
4
测试定时器的4种工作模式
5
单片机时钟为11.0592MHz, 1个机器周期为1.085us.
6
计算初值:又定时器0为例, 计算值为N us
7
模式0: TH0 = (2^13 - (N/1.085)) / 2^5 = (8192 - N/1.085) / 32, TL0 = (8192 - N/1.085) % 32;
8
模式1: TH0 = (2^16 - (N/1.085)) / 2^8 = (65536 - N/1.085) / 256, TL0 = (65536 - N/1.085) % 256;
9
模式2: TH0 = TL0 = 2^8 - N/1.085 = 256 - N/1.085;
10
模式3: 对于定时器0, 分成2个8位计数器; 对于定时器1, 停止计数
11
*/
12![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
unsigned char t0Count, t1Count; //存放计时器中断次数
14
unsigned char t0H, t0L, t1H, t1L; //存放初值
15![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
16
//T0模式0, 计时5ms
17
void time0() interrupt 1
18
{
19
t0Count++;
20
}
21![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
22
//T1模式1, 计时50ms
23
void time1() interrupt 3
24
{
25
t1Count++;
26
}
27![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
28
void main()
29
{
30
t0Count = 0;
31
t1Count = 0;
32
t0H = (8192 - 5000 / 1.085) / 32;
33
t0L = (8192 - 5000 / 1.085);
34
t0L %= 32;
35
t1H = (65536 - 50000 / 1.085) / 256;
36
t1L = (65536 - 50000 / 1.085);
37
t1L %= 256;
38![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
TH0 = t0H; TL0 = t0L; //初值, 计时5ms
40
TH1 = t1H; TL1 = t1L; // 计时50ms
41![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
EA = 1; //允许中断
43
ET0 = 1; //允许定时器0, 定时器1中断
44
ET1 = 1;
45
TMOD = 0x10; //T1模式1, T0模式0: TMOD = 0001 0000B
46
P1 = 0;
47
TR1 = 1; //先让T1开始计时, T0停止
48
TR0 = 0;
49
while(1)
50
{
51
if(t1Count==20) //够1000ms
52
{
53
if(!P1) //二极管是亮的, 则换频率
54
{
55
TR1 = 0; //让T0开始计时, T1停止
56
TR0 = 1;
57
}
58
TH1 = t1H; TL1 = t1L; // 计时50ms
59
t1Count = 0;
60
P1 = ~P1;
61
}
62
else if(t0Count==60) //够300ms
63
{
64
if(!P1) //二极管是亮的, 则换频率
65
{
66
TR1 = 1; //让T1开始计时, T0停止
67
TR0 = 0;
68
}
69
TH0 = t0H; TL0 = t0L; // 计时5ms
70
t0Count = 0;
71
P1 = ~P1;
72
}
73
}
74
}
程序代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
1
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/dot.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
58
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
59
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
62
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
63
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
64
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
65
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
66
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
67
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
68
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
69
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
70
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
72
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
73
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
74
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)