独立按键检测程序
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit dula=P2^6;
sbit wela=P2^7;
sbit K1=P3^4;
sbit K2=P3^5;
sbit K3=P3^6;
sbit K4=P3^7;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uint aa;
uchar num1,num2,shi1,ge1,shi2,ge2;
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void display(uchar num1,uchar num2)
{
shi1=num1/10;
ge1=num1%10;
shi2=num2/10;
ge2=num2%10;
dula=1;
P0=table[shi1];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delay(1);
dula=1;
P0=table[ge1];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delay(1);
dula=1;
P0=table[shi2];
dula=0;
P0=0xff;
wela=1;
P0=0xfb;
wela=0;
delay(1);
dula=1;
P0=table[ge2];
dula=0;
P0=0xff;
wela=1;
P0=0xf7;
wela=0;
delay(1);
}
void key()
{
if(K1==0)
{
delay(5);
if(K1==0)
{
TR0=0;
while(!K1);
}
}
if(K2==0)
{
delay(5);
if(K2==0)
{
if(num1==0)
{
num1=60;
}
num1--;
while(!K2);
}
}
if(K3==0)
{
delay(5);
if(K3==0)
{
num1++;
if(num1>=60)
{
num1=num1-60;
}
while(!K3);
}
}
if(K4==0)
{
delay(5);
if(K4==0)
{
TR0=1;
while(!K4);
}
}
}
void inint()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void main()
{
inint();
while(1)
{
key();
display(num1,num2);
}
}
void zhongduan()interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
aa++;
if(aa==20)
{
aa=0;
num2++;
if(num2==60)
{
num2=0;
num1++;
if(num1==60)
num1=0;
}
}
} |