本文包含原理图、PCB、源代码、封装库、中英文PDF等资源
您需要 登录 才可以下载或查看,没有账号?注册会员
×
法一:
#include<reg51.h> //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义
#define uchar unsigned char
#define uint unsigned int
unsigned char const dofly[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,
0x77,0x7c,0x39,0x5e,0x79,0x71};//0-F
/*------------------------------------------------
函数声明
------------------------------------------------*/
uchar keyscan(void);//键盘扫描
void delay(uint i); //演示程序
/*------------------------------------------------
主函数
------------------------------------------------*/
void main()
{
uchar key;
P2=0x00; //1数码管亮 按相应的按键,会显示按键上的字符
while(1)
{
key=keyscan(); //调用键盘扫描,
switch(key)
{
case 0x7e:P0=dofly[0];break;//0 按下相应的键显示相对应的码值
case 0x7d:P0=dofly[1];break;//1
case 0x7b:P0=dofly[2];break;//2
case 0x77:P0=dofly[3];break;//3
case 0xbe:P0=dofly[4];break;//4
case 0xbd:P0=dofly[5];break;//5
case 0xbb:P0=dofly[6];break;//6
case 0xb7:P0=dofly[7];break;//7
case 0xde:P0=dofly[8];break;//8
case 0xdd:P0=dofly[9];break;//9
case 0xdb:P0=dofly[10];break;//a
case 0xd7:P0=dofly[11];break;//b
case 0xee:P0=dofly[12];break;//c
case 0xed:P0=dofly[13];break;//d
case 0xeb:P0=dofly[14];break;//e
case 0xe7:P0=dofly[15];break;//f
}
}
}
/*------------------------------------------------
键盘扫描程序
------------------------------------------------*/
uchar keyscan(void) //键盘扫描函数,使用行列反转扫描法
{
uchar cord_h,cord_l;//行列值中间变量
P3=0x0f; //行线输出全为0
cord_h=P3&0x0f; //读入列线值
if(cord_h!=0x0f) //先检测有无按键按下
{
delay(100); //去抖
if(cord_h!=0x0f)
{
cord_h=P3&0x0f; //读入列线值
P3=cord_h|0xf0; //输出当前列线值
cord_l=P3&0xf0; //读入行线值
return(cord_h+cord_l);//键盘最后组合码值
}
}return(0xff); //返回该值
}
/*------------------------------------------------
延时程序
------------------------------------------------*/
void delay(uint i) //延时函数
{
while(i--);
}
法二:
/P1接4*4键盘
unsigned char keyscan1() //行列法1
{
unsigned char row,rol;
P1=0x0f;
if(P1!=0x0f)
{
delay(10); //延时消抖
if (P1_0==0) row=0;
else if(P1_1==0) row=1;
else if(P1_2==0) row=2;
else if(P1_3==0) row=3;
else return(255);
P1=0xf0;
if (P1_4==0) rol=0;
else if(P1_5==0) rol=1;
else if(P1_6==0) rol=2;
else if(P1_7==0) rol=3;
else return(255);
while(P1!=0xf0); //等待释放
return(row*4+rol);
}
else return(255);
}
法三:
unsigned char keyscan2() //线反转法
{
unsigned char row,col,i,x;
unsigned char code keyt[]={238,222,190,126,237,221,189,125,235,219,187,123,231,215,183,119};
P1 = 0x0f;
row = P1&0x0f;
if(row!=0x0f)
{
delay(10);
P1 = 0xf0;
col = P1&0xf0;
for(x=row|col,i=0;i<16;i++)
{
if(x==keyt[i]) break;
}
if(i==16) return(255);
else return(i);
}
else return(255);
}
|