4*4矩阵键盘的实现原理图以及代码
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include<reg51.h>
- unsigned char a[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7f,0x39,0x3f,0x79,0x71};
- void delay(unsigned int xsm);
- unsigned char keyscan(){
- unsigned char key,Row,Col;
- P3=0x0f;
- if(P3!=0x0f){
- delay(10);//去抖
- if(P3!=0x0f){
- Row=P3&0x0f;//确保端口值正确(行的值)
- P3=0xf0;
- Col=P3&0xf0;//列值
- }
- while((P3&0xf0)!=0xf0);//判断键是否抬起
- }
- switch(Row+Col){
- case 0xee:key=0;break;
- case 0xde:key=1;break;
- case 0xbe:key=2;break;
- case 0x7e:key=3;break;
- case 0xed:key=4;break;
- case 0xdd:key=5;break;
- case 0xbd:key=6;break;
- case 0x7d:key=7;break;
- case 0xeb:key=8;break;
- case 0xdb:key=9;break;
- case 0xbb:key=10;break;
- case 0x7b:key=11;break;
- case 0xe7:key=12;break;
- case 0xd7:key=13;break;
- case 0xb7:key=14;break;
- case 0x77:key=15;break;
- }
- return key;
- }
- void main(){
- unsigned key1;
- while(1){
- key1=keyscan();
- P2=a[key1];
- }
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
矩阵键盘.zip
(58.98 KB, 下载次数: 102)
|