#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);
}
}
}
不知道为什么这样子写不得,麻烦指点一二,小女子在此谢过了
此帖出自
小平头技术问答
P1REN |= BIT4; // Enable P1.4 internal resistance
P1OUT |= BIT4; // Set P1.4 as pull-Up resistance
一周热门 更多>