关于单片机矩阵键盘扫描的问题
偶做人间客 2018-12-17 06:24:06 我想实现led灯循环点亮,每个亮一秒,当按下矩阵键盘中任意一个按键,led灯熄灭,在按任意一个键,恢复循环,要求用查询法,我这个程序下到板子上按键识别并不灵敏,按键按下去有时有用有时没用,我想问一下能怎么改,是程序的问题还是其他问题?
#include<reg51.h>
char led[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
char key_scan[]={0xef,0xdf,0xbf,0x7f};
char s=0;
char t=0;
char cnt=0;
void delay(unsigned int a)
{unsigned int j=0;
for(;a>0;a--)
for(j=0;j<100;j++);
}
void initial()
{TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
t++;
if(t==20)
{t=0;s++;}
}
void getkey()
{
char i=0;
for(;i<4;i++)
{
P3=key_scan[i];
if((P3&0x0f)!=0x0f)
{ delay(20);
if((P3&0x0f)!=0x0f)
{ delay(20);
if((P3&0x0f)==0x0f)
{cnt++; TR0=~TR0;}
}
}
}
}
main()
{ initial();
while(1)
{
getkey();
if(cnt%2==0)
P1=led[s%8];
else
P1=0xff;
}
}