先在P3口输出
p3 00001111
低四位 行会有变化
cord_h =00001111&00001110 =00001110
if !=00001111
延时0.1us
cord_h=00001110&00001111=00001110
if !=00001111
P3再输出11111110
P3=00001110|11110000=11111110
输出高四位
cord_l=P3&0xf0 //此时P3口就是输入值01111110 而不是上面的11111110
cord_l=01111110&11110000=01110000
cord_h+cord_l=00001110+01110000=01111110=0x7e //此编码即为S1的编码
#include <reg52.h>//包含头文件
#define uchar unsigned char
#define uint unsigned int
unsigned char const table[]={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=table[0]break//0 按下相应的键显示相对应的码值
case 0x7d:P0=table[1]break//1
case 0x7b:P0=table[2]break//2
case 0x77:P0=table[3]break//3
case 0xbe:P0=table[4]break//4
case 0xbd:P0=table[5]break//5
case 0xbb:P0=table[6]break//6
case 0xb7:P0=table[7]break//7
case 0xde:P0=table[8]break//8
case 0xdd:P0=table[9]break//9
case 0xdb:P0=table[10]break//a
case 0xd7:P0=table[11]break//b
case 0xee:P0=table[12]break//c
case 0xed:P0=table[13]break//d
case 0xeb:P0=table[14]break//e
case 0xe7:P0=table[15]break//f
}
}
}
uchar keyscan(void)//键盘扫描函数,使用行列反转扫描法
{
uchar cord_h,cord_l//行列值
P3=0x0f //行线输出全为0
cord_h=P3&0x0f//读入列线值
if(cord_h!=0x0f)//先检测有无按键按下
{
delay(100) //去抖
cord_h=P3&0x0f //读入列线值
if(cord_h!=0x0f)
{
P3=cord_h|0xf0 //输出当前列线值
cord_l=P3&0xf0 //读入行线值
return(cord_h+cord_l)//键盘最后组合码值
}
}return(0xff)//返回该值
}
void delay(uint i)//延时函数
{
while(i--)
}
给一个键盘扫描程序参考:void delay(unsigned int a)
{
unsigned int i,j
for(i=0i<ai++)
for(j=0j<120j++)
}
uchar kbscan(void)
{
unsigned char sccode,recode
P1=0x0f //发0扫描,列线输入
if ((P2 & 0x0f) != 0x0f) //有键按下
{
delay(20) //延时去抖动
if ((P1&0x0f)!= 0x0f)
{
sccode = 0xef //逐行扫描初值
while((sccode&0x01)!=0)
{
P1=sccode
if((P1&0x0f)!=0x0f)
{
recode=(P1&0x0f)|0xf0
while((P1&0x0f)!=0x0f)//等待键抬起
return((~sccode)+(~recode))
}
else
sccode=(sccode<<1)|0x01
}
}
}
return 0 //无键按下,返回0
}
uchar getkey(void)
{
unsigned char key,keyval=0xff
key=kbscan()
switch(key)
{
case 0x11:keyval=1break
case 0x12:keyval=2break
case 0x14:keyval=3break
case 0x18:keyval=10break //+
case 0x21:keyval=4break
case 0x22:keyval=5break
case 0x24:keyval=6break
case 0x28:keyval=11break //-
case 0x41:keyval=7break
case 0x42:keyval=8break
case 0x44:keyval=9break
case 0x48:keyval=12break //清除
case 0x81:keyval=13break
case 0x82:keyval=0break
case 0x84:keyval=14break //小数点
case 0x88:keyval=15break //确认
default:keyval=16break
}
return(keyval)
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)