为什么只有key3有用而key1控制数码管加一和key2减一key3清零只有在开关三打开TR0=1;的时候才有用 要他们独立操作该该怎么改进啊!谢谢
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit key1=P1^0;
sbit key2=P1^1;
sbit key3=P1^2;
sbit key4=P1^3;
uchar num,count,ge,shi;
void keyscan( );
void delayxms(uint);
void display(uchar,uchar);
uchar code table[]={
0x02/*0*/,0x9f/*1*/,0x24/*2*/,
0x0c/*3*/,0x98/*4*/,0x48/*5*/,
0xc0/*6*/,0x1e/*7*/,0x00/*8*/,0x18
};/*共阳数码管0 8编码*/
void main()
{
TMOD=0x01;//装初值
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
while(1)
{
void keyscan ();
display(shi,ge);
}
}
void T0_
time() interrupt 1
{
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
count++;
if(count==20)
{
count=0;
num++;
if(num==60)
num=0;
shi=num/10;
ge=num%10;
}
}
void delayxms(uint z)//延时
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void display(uchar shi,uchar ge)//数码管显示
{
P3=0Xe0;
P0=table[ge];
delayxms(5);
P3=0XD0;
P0=table[shi];
delayxms(5);
}
void keyscan ()
{
if(key1==0)
{
delayxms(10);
if(key1==0)
{
num++;
if(num==60)
num=0;
while(!key1);
}
}
if(key2==0)
{
delayxms(10);
if(key2==0)
{
if(num==0)
num=60;
num--;
while(!key2);
}
}
if(key3==0)
{
delayxms(10);
if(key3==0)
{
num=0;
while(!key3);
}
}
if(key4==0)
{
delayxms(10);
if(key4==0)
{
while(!key4);
TR0=~TR0;
}
}
}
一周热门 更多>