#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}; //決定了鍵盤按鍵布局
本文引用地址:http://cafeforensic.com/article/201611/320656.htm//======================================
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); //將列的狀態(tài)存入high
P0=0x0f; //翻轉(zhuǎn)行列電平
temp=P0; //再次讀取P0
low=(temp&0x0f); //將行的狀態(tài)存入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);
}
}
評論