键盘分为编码键盘和非编码键盘。
编码键盘:键盘上闭合键的识别由专用的硬件编码器实现,并产生键编码号或键值 (如计算机键盘)
非编码键盘:靠软件编程来识别 (分为独立键盘和矩阵键盘)
在单片机组成的各种系统中,用的最多的是非编码键盘。
独立键盘控制数码管的数值
代码示例:
#include
#define uchar unsigned char
#define uint unsigned int
sbit we = P2^7;
sbit du = P2^6;
sbit S2 = P3^0;
sbit S3 = P3^1;
uchar counter,time_counter;
uchar code leddata[]={
0x3F,//0
0x06,//1
0x5B,//2
0x4F,//3
0x66,//4
0x6D,//5
0x7D,//6
0x07,//7
0x7F,//8
0x6F,//9
0x77,//A
0x7C,//B
0x39,//C
0x5E,//D
0x79,//E
0x71,//F
0x76,//H
0x38,//L
0x37,//n
0x3E, //u
0x73,//P
0x5C,//o
0x40,//_
0x00,//熄灭
0x00//自定义
}; //数字字母的对应数组
void delay(uint z)//延时
{
uint x,y;
for(x = z;x > 0;x --)
for(y = 114; y > 0; y -- );
}
void display(uchar i)//数码管显示
{
uchar shi,ge;
shi = i / 10; //求模(十位)
ge = i % 10; //求余(个位)
P0 = 0xff;
we = 1;
P0 = 0xbf; //1011 1111点亮第七位数码管
we = 0;
du = 1;
P0 = leddata[shi];
du = 0;
delay(1);
P0 = 0xff;
we = 1;
P0 = 0x7f; //0111 1111点亮第八位数码管
we = 0;
du = 1;
P0 = leddata[ge];
du = 0;
delay(1);
}
void main()
{
while(1)
{
display(time_counter);
if(S2 == 0)
{
delay(5);//软件消抖
if(S2 == 0)
{
time_counter ++;
while(!S2);//松手检测
}
}
if(S3 == 0)
{
delay(5);//软件消抖
if(S3 == 0 && time_counter != 0)
{
time_counter --;
while(!S3);
}
}
}
}
按下按键,八个数码管显示对应的按键号
代码示例:
#include
#define uchar unsigned char
#define uint unsigned int
sbit we = P2^7;
sbit du = P2^6;
uchar code leddata[]={
0x3F,//0
0x06,//1
0x5B,//2
0x4F,//3
0x66,//4
0x6D,//5
0x7D,//6
0x07,//7
0x7F,//8
0x6F,//9
0x77,//A
0x7C,//B
0x39,//C
0x5E,//D
0x79,//E
0x71,//F
0x76,//H
0x38,//L
0x37,//n
0x3E, //u
0x73,//P
0x5C,//o
0x40,//_
0x00,//熄灭
0x00//自定义
}; //数字字母的对应数组
void delay(uint z)//延时
{
uint x,y;
for(x = z;x > 0;x --)
for(y = 114; y > 0; y -- );
}
uchar KeyScan() //带返回值的函数
{
uchar cord_l,cord_h; //声明列线和行线的值的储存变量
P3 = 0xf0;// 1111 0000
if((P3 & 0xf0) != 0xf0) //判断是否有按键按下
{
delay(5);//软件消抖
if((P3 & 0xf0) != 0xf0)//判断是否有按键按下
{
cord_l = P3 & 0xf0; //储存列线值
P3 = cord_l | 0x0f;
cord_h = P3 & 0x0f; //储存行线值
while(P3 & 0x0f != 0x0f);//松手检测
return (cord_l + cord_h); //返回键值码
}
}
}
void KeyPro()
{
switch(KeyScan())
{
//第一行键值码
case 0xee: P0 = leddata[0]; break;
case 0xde: P0 = leddata[1]; break;
case 0xbe: P0 = leddata[2]; break;
case 0xee: P0 = leddata[3]; break;
//第二行键值码
case 0xed: P0 = leddata[4]; break;
case 0xdd: P0 = leddata[5]; break;
case 0xbd: P0 = leddata[6]; break;
case 0xed: P0 = leddata[7]; break;
//第三行键值码
case 0xeb: P0 = leddata[8]; break;
case 0xdb: P0 = leddata[9]; break;
case 0xbb: P0 = leddata[10]; break;
case 0xeb: P0 = leddata[11]; break;
//第四行键值码
case 0xe7: P0 = leddata[12]; break;
case 0xd7: P0 = leddata[13]; break;
case 0xb7: P0 = leddata[14]; break;
case 0xe7: P0 = leddata[15]; break;
}
}
void mian()
{
we = 1;//打开位选
P0 = 0;//八位数码管全显示
we = 0;//锁存位选
du = 1;//打开段选
while(1)
{
KeyPro(); //提取键值码并且送不同数值给数码管显示
}
}