/*************
按键值
****************/
void keyscan()
{
P1DIR=0xf0;
uchar temp;
P1OUT =0x7f; //选择第一列
if(keyin!=0x0f);//是否有按键按下
{
delay(5);
if(keyin!=0x0f)
{
temp =keyin;
delay(5);
switch(temp)
{
case 0x07:keyz=0;break;
case 0x0b:keyz=1;break;
case 0x0d:keyz=2;break;
case 0x0e:keyz=3;break;
}
while(keyin!=0x0f); //松手检测
}
}
P1OUT =0xbf; //选择第2列
if(keyin!=0x0f);//是否有按键按下
{
delay(5);
if(keyin!=0x0f)
{
temp =keyin;
delay(5);
switch(temp)
{
case 0x07:keyz=4;break;
case 0x0b:keyz=5;break;
case 0x0d:keyz=6;break;
case 0x0e:keyz=7;break;
}
while(keyin!=0x0f); //松手检测
}
}
P1OUT =0xdf; //选择第3列
if(keyin!=0x0f);//是否有按键按下
{
delay(5);
if(keyin!=0x0f)
{
temp =keyin;
delay(5);
switch(temp)
{
case 0x07:keyz=8;break;
case 0x0b:keyz=9;break;
case 0x0d:keyz=10;break;
case 0x0e:keyz=11;break;
}
while(keyin!=0x0f); //松手检测
}
}
P1OUT =0xef; //选择第4列
if(keyin!=0x0f);//是否有按键按下
{
delay(5);
if(keyin!=0x0f)
{
temp =keyin;
delay(5);
switch(temp)
{
case 0x07:keyz=12;break;
case 0x0b:keyz=13;break;
case 0x0d:keyz=14;break;
case 0x0e:keyz=15;break;
}
while(keyin!=0x0f); //松手检测
}
}
}
P1初始化
*******************/
void P1_int()
{
P1DIR=0XF0;//高4位输出,低4位输入
P1OUT =0X0f;//高4位0,低4位1
P1IFG=0;//无中断挂起
P1IES=0;//选择边沿触发中断,有必要先清除中断标志位
P1IE =0;//禁止中断
P1IE |=BIT0;
P1IES |=BIT0;//下降沿时置位P1.0的P1IFG0标志
P1IE |=BIT1;
P1IES |=BIT1;
P1IE |=BIT2;
P1IES |=BIT2;
P1IE |=BIT3;
P1IES |=BIT3;
}
这是用高4位判断是否有按下按键的,可行
|