51单片机定时器程序

自己写了一个简单的数码管显示函数,定义好要显示好长时间,数码管显示会逐次递减,利用定时器计时。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit duan=P2^6;
sbit wei=P2^7;
uchar temp,t0;
uint count;
uint t0_1,t0_count, m,n,num[6];
uchar code tableduan[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uchar code tablewei[]={
0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
void ();
void display(unsigned long int input);
void main()
{
init();
while(1)
{
for(count=60;count&gt;0;count--)
{
display(count);
t0_1=0;
t0_count=0;
}
TR0=0;
P1=0x00;
duan=1;
P0=0x00;
duan=0;

}

}

void ()
{
temp=0xfe;
P1=temp;
TMOD=0x11;
TH0=(655360-5000)/256;
TL0=(655360-5000)%256;
EA=1;
ET0=1;
TR0=1;

t0=0;
t0_1=0;
t0_count=0;
}
void display(unsigned long int input)
{
uchar m,n;
//变量初始化
m=0;
n=0;
//数位分离
if(input==0)
{
num[0]=0;
n=1;
}
else
for(n=0;input!=0;n++)
{
num[n]=input%10;
input=input/10;
}
n--;
//时间判断并输出
while(t0_count!=200)//数数的时间间隔,在此设置间隔并在主函数for循环中清零。
{
if(t0_1==1)//屏幕刷新周期
{
t0_1=0;
wei=1;
P0=tablewei[m];
wei=0;
duan=1;
P0=tableduan[num[n-m]];
duan=0;
m++;
if(m==n+1)
m=0;
}

}
}
void timer0() interrupt 1
{
TH0=(655360-5000)/256;
TL0=(655360-5000)%256;
t0++;
t0_1++;
t0_count++;
if(t0==100)
{
t0=0;
temp=_crol_(temp,1);
P1=temp;
}
}