4*4键盘(P0接键盘P2输出)无法保留P2原状态 ,
时间:10-02
整理:3721RD
点击:
#define unchar unsigned char
#define unint unsigned int
void main()
{ unint key();
void ms_delay(unint delaytime);
P2=0x20;
a:P1=0xff;
P2=key();
ms_delay(500);
goto a;
}
unint key()
{unint mask=0xfe,temp,k=P2;
unint row=0,line=0;
const unint code numb[4][4]={0xee,0xde,0xbe,0x7e,
0xed,0xdd,0xbd,0x7d,0xeb,0xdb,0xbb,0x7b,0xe7,
0xd7,0xb7,0x77};
for(line=0;line<4;line++)
{P1=mask;
mask=mask*1;
temp=P1;
if(temp != mask) /*key pushed in the line ?*/
{ms_delay(300); /*to confirm a key pushed*/
if((temp&0xf0)==temp) return 0x20;
for(row=0;row<4;row++) /*抓到了行,寻列。并且P口已可读出*/
{if(P1==numb[line][row]) /*反查表,读出行列*/
return line*4+row+1;
}}
else mask=_crol_(mask,1); /*查表*/
}
P1=0x0f;
temp=P1;
while(temp&0x0f==0x0f)
{P1=0xf0;
return p2;}/*欲保留原状态,实际用KEILP2变为0xff*/
}
void ms_delay(unint delaytime)
{unint i=0;
for(;delaytime>0;delaytime--)
{for(i=0;i<124;i++); }}
#define unint unsigned int
void main()
{ unint key();
void ms_delay(unint delaytime);
P2=0x20;
a:P1=0xff;
P2=key();
ms_delay(500);
goto a;
}
unint key()
{unint mask=0xfe,temp,k=P2;
unint row=0,line=0;
const unint code numb[4][4]={0xee,0xde,0xbe,0x7e,
0xed,0xdd,0xbd,0x7d,0xeb,0xdb,0xbb,0x7b,0xe7,
0xd7,0xb7,0x77};
for(line=0;line<4;line++)
{P1=mask;
mask=mask*1;
temp=P1;
if(temp != mask) /*key pushed in the line ?*/
{ms_delay(300); /*to confirm a key pushed*/
if((temp&0xf0)==temp) return 0x20;
for(row=0;row<4;row++) /*抓到了行,寻列。并且P口已可读出*/
{if(P1==numb[line][row]) /*反查表,读出行列*/
return line*4+row+1;
}}
else mask=_crol_(mask,1); /*查表*/
}
P1=0x0f;
temp=P1;
while(temp&0x0f==0x0f)
{P1=0xf0;
return p2;}/*欲保留原状态,实际用KEILP2变为0xff*/
}
void ms_delay(unint delaytime)
{unint i=0;
for(;delaytime>0;delaytime--)
{for(i=0;i<124;i++); }}
你的P2写成小写了吧?