为什么用定时器检测按键不行呢?
#include
#define uchar unsigned char
#define uint unsigned int
uchar timer=0,flag1s=0,flag=0;
uchar led=0x01;
uint buttonflag=0;
void delay(uint z)
{
uint x,y;
for(x=0;x
for(y=0;y<120;y++);
}
void inter()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void key()
{
uchar temp=0;
uchar value=0xff;
if(P1!=0xff)
{
delay(10);
buttonflag=0;
if(P1==0xfe)
{
P0=led;
if(buttonflag>=5&&buttonflag<=20)
{
timer=0;
buttonflag=0;
flag=1;
flag1s=0;
}
if(buttonflag>=20)
{
timer=0;
buttonflag=0;
flag=0;
flag1s=1;
}
}
}
while(P1!=0xff)
{
delay(10);
}
}
void main()
{
inter();
P0=0x00;
while(1)
{
key();
}
}
void t0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
timer++;
if(P1==0xfe)
{
buttonflag++;
}
if(flag&&(timer==2))
{
timer=0;
led=~led&0x01;
P0=led;
}
if(flag1s&&(timer==20))
{
timer=0;
led=~led&0x01;
P0=led;
}
}
先确定定时器有没有问题,设个断点软件仿真一下,再确认按键是不是正确的,protues仿一下吧,感觉是按键的问题