📄 key.c
字号:
/* t10.c 按任意键显示其扫描码 */
#include <reg51.h>
unsigned char led[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
0x88,0x83,0xc6,0xa1,0x86,0x8e}; /* 0-f 编码 */
unsigned int i,j;
unsigned char key,sccode,recode;
/* 延时 10ms */
void dlms(void)
{ for(i=0;i<765;i++);}
/* 键盘扫描函数 */
unsigned char kbscan(void)
{ P1=0xf0; /* 列置 "1" 行置 "0" */
if((P1&0xf0)!=0xf0) /* 若有键按下 */
{ dlms(); /* 延时 10ms 去抖动 */
if((P1&0xf0)!=0xf0) /* 按键有效 */
{ sccode=0xfe; /* 逐行扫描初值 */
while((sccode&0x10)!=0) /* 若扫描未出界 */
{ P1=sccode; /* 输出行扫描码 */
if((P1&0xf0)!=0xf0) /* 本行有按键 */
{ recode=(P1&0xf0)|0x0f; /* 取列扫描码 */
return((~sccode)+(~recode)); /* 返回行列扫描组合值 */
}
else
sccode=(sccode<<1)|0x01; /* 给出下一行扫描码 */
}
}
}
return(0); /* 无键按下,返回值为0 */
}
void main(void)
{ while(1)
{ key=kbscan();
if(key!=0)
{ P2=led[key&0x0f];
P0=led[key>>4];
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -