#include<reg52.h> //宏定义
#define uchar unsigned char
#define uint unsigned int
sbit smg1=P2^0; //位定义
sbit smg2=P2^1;
sbit smg3=P2^2;
sbit smg4=P2^3;
sbit smg5=P2^4;
sbit smg6=P2^5;
sbit smg7=P2^6;
sbit smg8=P2^7;
sbit key1=P1^0;
sbit key2=P1^1;
sbit key3=P1^2;
sbit key4=P1^3;
sbit feng=P1^4;
code unsigned char table[]= //查表0-9、- 共阳极
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf};
uchar a,miao,fen,shi,miao1,miao2,fen1,fen2,shi1,shi2;
void delay1(uint x) //短延时
{
while(x--);
}
void init() //初始化
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
miao=0;
fen=00;
shi=00;
feng=0;
}
void delay(uint z) //长延时
{
uint x,y;
for(x=z;x>0;x++)
for(y=110;y>0;y++);
}
void display() //显示子函数
{
P2=0;
smg8=1;
P0=table[miao1];
delay1(200);
P2=0;
smg7=1;
P0=table[miao2];
delay1(200);
P2=0;
smg6=1;
P0=table[10];
delay1(200);
P2=0;
smg5=1;
P0=table[fen1];
delay1(200);
P2=0;
smg4=1;
P0=table[fen2];
delay1(200);
P2=0;
smg3=1;
P0=table[10];
delay1(200);
P2=0;
smg2=1;
P0=table[shi1];
delay1(200);
P2=0;
smg1=1;
P0=table[shi2];
delay1(200);
}
void timer0() interrupt 1 //定时器中断0
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
a++;
if(a==20)
{
a=0;
miao++;
}
}
void keyscan() //键盘子程序
{
if(!key1)
{
delay1(2000);
if(!key1)
{
fen++;
if(fen==60)
fen=0;
while(!key1)
display();
}
}
if(!key2)
{
delay1(2000);
if(!key2)
{
fen--;
if(fen==255)
fen=59;
while(!key2)
display();
}
}
if(!key3)
{
delay1(2000);
if(!key3)
{
shi++;
if(shi==24)
shi=0;
while(!key3)
display();
}
}
if(!key4)
{
delay1(2000);
if(!key4)
{
shi--;
if(shi==255)
shi=23;
while(!key4)
display();
}
}
}
void main() //主函数
{
init();
while(1)
{
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
feng=1;
if(shi==24)
shi=0;
delay1(10000);
feng=0;
}
}
miao1=miao%10;
miao2=miao/10;
fen1=fen%10;
fen2=fen/10;
shi1=shi%10;
shi2=shi/10;
keyscan();
display();
}
} |