您的位置 首页 被动

51单片机行列式键盘扫描程序

includereg51h>includeintrinsh>defineucharunsignedchardefineuintunsignedintunsignedcharcodeseg7

#include

#include
#define uchar unsigned char
#define uint unsigned int
unsigned char code seg7[16]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0x08,0x03,0x46,0x21,0x06,0x0e};//0到f的字型码
unsigned char code hao[4]={0x0e,0x0d,0x0b,0x07};
unsigned char code key_[16]={1,2,3,15,4,5,6,14,7,8,9,13,0,10,11,12}; //决议了键盘按键布局

//======================================
void delay(uint n) //延时程序
{
while(n–)
{_nop_();}
}

//======================================

//======================================
uchar scan() // 键盘扫描程序
{
uchar temp,h,j,i,high,low;
bit find=0; //界说位find标志 并赋值零
P0=0xf0;
temp=P0; //行低电平 列高电平
if(temp!=0xf0) //检测是否有键按下
{
delay(1000); //去抖
if(temp!=0xf0) //确认有键按下
{
find=1; //标志有键按下
high=(temp>>4); //将列的状况存入high
P0=0x0f; //翻转队伍电平
temp=P0; //再次读取P0
low=(temp&0x0f); //将行的状况存入low
for(i=0;i<4;i++) //确认按键的坐标
{
if(hao[i]==high){j=i;}
if(hao[i]==low){h=i;}
}
}
}
if(find==0){return 16;}
else return(4*h+j);
}
//=============================================
void main()
{
uchar a,key=0;
while(1)
{
a=scan();
if(a!=16)
{
key=key_[a];
}
P1=seg7[key];
P2=0xfe;
delay(200);
}
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/ziliao/beidong/259930.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部