60S计时器
#include<reg52.h>
#include<intrins.h>
#define uc unsigned char
#define ui unsigned int
sbit W = P2^7;
sbit D = P2^6;
uc leddate[]={
0x3F, //"0"
0x06, //"1"
0x5B, //"2"
0x4F, //"3"
0x66, //"4"
0x6D, //"5"
0x7D, //"6"
0x07, //"7"
0x7F, //"8"
0x6F, //"9"
0x77, //"A"
0x7C, //"B"
0x39, //"C"
0x5E, //"D"
0x79, //"E"
0x71, //"F"
0x76, //"H"
0x38, //"L"
0x37, //"n"
0x3E, //"u"
0x73, //"P"
0x5C, //"o"
0x40, //"-"
0x00, //熄灭
0x00 //自定义
}; //数码管显示数组
void delay(ui z)
{
ui x, y;
for (x=z; x>0 ;x--)
for (y=120 ; y>0 ;y--); //做120 * X循环,即延时 X 毫秒
}
void display(uc time)
{
uc g,sh;
g = time%10;
sh = time/10;
W = 1; //打开位选,允许信号进入
P0 = 0x7f;
W = 0; //锁存位选信号
D = 1; //打开段选
P0 = leddate[g];
D = 0; // 锁存段选信号
delay(1);
W = 1;
P0 = 0xbf;
W = 0;
D = 1;
P0 = leddate[sh];
D = 0;
delay(1);
} //数码管显示函数
void main()
{
uc a,second=0;
TMOD = 0x01; //定时器0,模式1,16位定时器
TH0=0x4b;
TL0=0xfe; //定时50ms
TR0 =1; //启动定时器0
while(1) /* 为什么循环放在这,放在其他地方就错了?,下面的两个if 语句不是可以让计时器一直工作吗?
假如if语句可以让定时器工作,那我把" display(second); " 死循环之后数码管却只显示 “00” */
{
if (TF0 == 1)
{
TF0 =0; //无中断时,软件清零
TH0=0x4b;
TL0=0xfe;
a++;
}
if(a== 20)
{
a = 0;
second++;
}
if (second==60)
second = 0;
display(second);
}
}
|