/*独立键盘检测装置,按下key1加1,按下key2减一,
按下key3清零,按下key4以定时器自减1,
再按key4数值停止自动减1,保持显示原数*/
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit key1=P0^0;
sbit key2=P0^1;
sbit key3=P0^2;
sbit key4=P0^3;
uchar num,t;
uchar code table[] = {0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void init()
{
TMOD=0x10;
TH1=0x3c;
TL1=0x0B0;
EA=1;
ET1=1;
}
void delay(uint z)
{
uint i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
void main()
{
init();
P1=0x00;
delay(5);
while(1)
{
if(key1==0)
{
delay(5);
if(key1==0)
{
num++;
if(num==10)
num=0;
while(!key1);
delay(5);
while(!key1);
}
}
else if(key2==0)
{
delay(5);
if(key2==0)
{
if(num==0)
num=10;
num--;
while(!key2);
delay(5);
while(!key2);
}
}
else if(key3==0)
{
delay(5);
if(key3==0)
{
num=0;
while(!key3);
}
}
else if(key4==0)
{
delay(5);
if(key4==0)
{
delay(5);
while(1)
{
TR1=1;
if(t==20)
{
t=0;
if(num==0)
num=10;
num--;
P2=table[num];
//while(!key4);
if(key4==0)
{
break;
}
}
}
}
}
P2=table[num];
}
}
void time0() interrupt 3
{
TH1=0x3c;
TL1=0x0B0;
t++;
}
问题出现在key4里,可以自动减数,但是停不下来,怎么回事?应该怎么改呢,求指教哈。
此帖出自
小平头技术问答
一周热门 更多>