单片机练习 - 定时器
相关的特殊寄存器, 请查看有关资料
程序代码如下:
程序代码如下:
定时器
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
13unsigned char t0Count, t1Count; //存放计时器中断次数
14unsigned char t0H, t0L, t1H, t1L; //存放初值
15
16//T0模式0, 计时5ms
17void time0() interrupt 1
18{
19 t0Count++;
20}
21
22//T1模式1, 计时50ms
23void time1() interrupt 3
24{
25 t1Count++;
26}
27
28void 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
39 TH0 = t0H; TL0 = t0L; //初值, 计时5ms
40 TH1 = t1H; TL1 = t1L; // 计时50ms
41
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}
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
13unsigned char t0Count, t1Count; //存放计时器中断次数
14unsigned char t0H, t0L, t1H, t1L; //存放初值
15
16//T0模式0, 计时5ms
17void time0() interrupt 1
18{
19 t0Count++;
20}
21
22//T1模式1, 计时50ms
23void time1() interrupt 3
24{
25 t1Count++;
26}
27
28void 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
39 TH0 = t0H; TL0 = t0L; //初值, 计时5ms
40 TH1 = t1H; TL1 = t1L; // 计时50ms
41
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}