用单片机定时中断完成矩阵键盘的扫描,但是总是不对。下面是我的程序。求,高手指证!!!!
(键盘我只用了前四个)
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar code table[]={
0xfe,0xfd,0xfb,0xf7,0xef,
0xdf,0xbf,0x7f};
sbit dula=P2^6;
sbit wela=P2^7;
void delay(uint z) //延时函数
{
uint x,y;
for(x=z;x>0;x--)
for(y=114;y>0;y--);
}
void display(uchar num)
{
dula=1;
P0=table[num];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delay(5);
}
void main()
{
uchar num;
TMOD=0x01;
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
EA=1;
ET0=1;
TR0=1;
while(1)
{
display(num);
}
}
void IT_0()interrupt 1
{
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
if((P1&0xf0)!=0xf0)
{
uchar temp,key;
P3=0xfe;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xee:
key=0;
break;
case 0xde:
key=1;
break;
case 0xbe:
key=2;
break;
case 0x7e:
key=3;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
display(key);
}
}
}
}
|