#define keyin (P1IN&0xf0)//宏定义 判断按键是否按下,按下为keyin,否则为0xff
uchar keyz;
/****************
用到的函数
*********************/
void delay(uchar t);
void key_int();
void keyscan();
/*****************
延 时
***********************/
void delay(uchar t)
{
uint i,j;
for(i=0;i<t;i++)
for(j=0;j<1000;j++);
}
/********************
键盘初始化
配置P1引脚,
关闭中断,
使用边沿触发中断的模式,用下降沿
************************/
void key_int()
{
P1DIR=0x0f;//高位输入,地位输出
P1OUT=0xf0;//高四位输出为1,第四位输出为低
P1IE=0;
P1IFG=0;
P1IES=0;
P1IE |=BIT4;//允许中断
P1IES |=BIT4;//下降沿触发
P1IE |=BIT5;
P1IES |=BIT5;
P1IE |=BIT6;
P1IES |=BIT6;
P1IE |=BIT7;
P1IES |=BIT7;
}
/**********************
键 值
通过高四位判断键位,低四位判断行
**************************/
void keyscan()
{
uchar temp;
P1DIR=0X0f;
P1OUT=0xfe;
if(keyin!=0xf0)
{
delay(5);
if(keyin!=0xf0)
{
temp=keyin;
delay(5);
switch(temp)
{
case 0xee:keyz=1;break;
case 0xde:keyz=2;break;
case 0xbe:keyz=3;break;
case 0x7e:keyz=4;break;
}
while(keyin!=0xf0);
}
}
/**************第二行****************/
P1OUT=0Xfd;
if(keyin!=0xf0)
{
delay(5);
if(keyin!=0xf0)
{ temp=keyin;
delay(5);
switch(temp)
{
case 0xed:keyz=5;break;
case 0xdd:keyz=6;break;
case 0xbd:keyz=7;break;
case 0x7d:keyz=8;break;
}
while(keyin!=0xf0);
}
}
/**************第3行****************/
P1OUT=0Xfb;
if(keyin!=0xf0)
{
delay(5);
if(keyin!=0xf0)
{
temp=keyin;
delay(5);
switch(temp)
{
case 0xeb:keyz=9;break;
case 0xdb:keyz=10;break;
case 0xbb:keyz=11;break;
case 0x7b:keyz=12;break;
}
while(keyin!=0xf0);
}
}
/**************第4行****************/
P1OUT=0Xf7;
if(keyin!=0xf0)
{
delay(5);
if(keyin!=0xf0)
{ delay(5);
temp=keyin;
switch(temp)
{
case 0xe7:keyz=13;break;
case 0xd7:keyz=14;break;
case 0xb7:keyz=15;break;
case 0x77:keyz=16;break;
}
while(keyin!=0xf0);
}
}
}
不知道为什么这样子写不得,麻烦指点一二,小女子在此谢过了
此帖出自
小平头技术问答
另外一点细节:每行P1低位输出后,未加任何延迟立刻读回高位,会不会读漏?当然这要看电路了。
P1REN |= BIT4; // Enable P1.4 internal resistance
P1OUT |= BIT4; // Set P1.4 as pull-Up resistance
可是我用另一种方法也是可以的,不需要上拉电阻呢
/*************
按键值
****************/
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位判断是否有按下按键的,可行
一周热门 更多>