首先,给P1赋值0xf0,这时候P1^4,P1^5,P1^6,P1^7为高电平,P1^0,P1^1,P1^2,P1^3为低电平。如果这时候候有按键按下那么P1^4,P1^5,P1^6,P1^7就有一个会变为低电平。因而P1的值就不等于0xf0,这是就能够判断有按键按下。
然后延时一段时间去抖动,然后给P1赋值0xfe,也就是P1^0为低电平,其他为高电平,这时候如果有在P1^0线上的P1^4,P1^5,P1^6,P1^7有按键按下,那么就会呈现低电平,从而判断哪一个按键按下;如果没有那么就给P1赋值0xfd,也就是P1^1为低电平,其他为高电平.,相同办法判断是不是有按键按下;如果没有那么就给P1赋值0xfb·····如斯类推,一共四次检测。文章源自微观生活(93wg.com)微观生活-https://93wg.com/22170.html
unsigned int scan()文章源自微观生活(93wg.com)微观生活-https://93wg.com/22170.html
{文章源自微观生活(93wg.com)微观生活-https://93wg.com/22170.html
int temp1,temp2,hang,lie;文章源自微观生活(93wg.com)微观生活-https://93wg.com/22170.html
P1=0XF0;文章源自微观生活(93wg.com)微观生活-https://93wg.com/22170.html
temp1=P1&0xf0;文章源自微观生活(93wg.com)微观生活-https://93wg.com/22170.html
if(temp1!=0xf0)文章源自微观生活(93wg.com)微观生活-https://93wg.com/22170.html
{文章源自微观生活(93wg.com)微观生活-https://93wg.com/22170.html
delay_ms(10);文章源自微观生活(93wg.com)微观生活-https://93wg.com/22170.html
if(temp1!=0xf0)文章源自微观生活(93wg.com)微观生活-https://93wg.com/22170.html
{
temp1=P1&0xf0;
TEST=1;
switch(temp1)
{
case 0xe0:
lie=1;
break;
case 0xd0:
lie=2;
break;
case 0xb0:
lie=3;
break;
case 0x70:
lie=4;
break;
}
P1=0X0f;
temp2=P1&0x0f;
if(temp2!=0x0f)
{
delay_ms(10);
if(temp2!=0x0f)
{
temp2=P1&0x0F;
switch(temp2)
{
case 0x0e:
hang=1;
break;
case 0x0d:
hang=2;
break;
case 0x0b:
hang=3;
break;
case 0x07:
hang=4;
break;
}
}
}
}
}
if(hang==1&&lie==1)num=1;
if(hang==1&&lie==2)num=2;
if(hang==1&&lie==3)num=3;
if(hang==2&&lie==1)num=4;
if(hang==2&&lie==2)num=5;
if(hang==2&&lie==3)num=6;
if(hang==3&&lie==1)num=7;
if(hang==3&&lie==2)num=8;
if(hang==3&&lie==3)num=9;
if(hang==4&&lie==2)num=0;
if(hang==4&&lie==1)num=\'A\';
return num;
}
以上就是微观生活(93wg.com)关于“51单片机教程4x4矩阵键盘扫描”的详细内容,希望对大家有所帮助!
评论