欢迎来到天天文库
浏览记录
ID:51817111
大小:38.53 KB
页数:5页
时间:2020-03-16
《单片机键盘扫描.doc》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、本文给出单片机键盘扫描的两种程序。供大家学习参考。第一种:#include#defineucharunsignedchar#defineuintunsignedintuinttemp,num,num1;uchartable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0xff};ucharscan();//定义一个键盘扫描函数voiddelay(uintz){uintx,y;for(x=0;x2、voidmain(){P2=0;P0=0x00;while(1){temp=scan();switch(temp){case0x7e:P0=table[0];break;//0按下相应的键显示相对应的码值case0x7d:P0=table[1];break;//1case0x7b:P0=table[2];break;//2case0x77:P0=table[3];break;//3case0xbe:P0=table[4];break;//4case0xbd:P0=table[5];break;//5case0xbb:P0=table[6];break;//6case0xb7:P0=t3、able[7];break;//7case0xde:P0=table[8];break;//8case0xdd:P0=table[9];break;//9case0xdb:P0=table[10];break;//acase0xd7:P0=table[11];break;//bcase0xee:P0=table[12];break;//ccase0xed:P0=table[13];break;//dcase0xeb:P0=table[14];break;//ecase0xe7:P0=table[15];break;//f}}}ucharscan(){P3=0x0f;num=P3&0x4、0f;if(num!=0x0f){delay(10);if(num!=0x0f){num=P3&0x0f;P3=num5、0xf0;num1=P3&0xf0;}return(num+num1);}}第二种:#include"reg52.h"#defineucharunsignedchar#defineuintunsignedintunsignedcharcodedofly[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//0-Fvoiddelay(uinti)//延6、时函数{while(i--);}voidmain(){P2=0xf8;//1数码管亮按相应的按键,会显示按键上的字符P0=dofly[0];while(1){ucharkey,num;P3=0xef;//第四列key=P3;key=key&0x0f;while(key!=0x0f){delay(500);key=P3;key=key&0x0f;while(key!=0x0f){key=P3;switch(key){case0xee:num=12;break;case0xed:num=13;break;case0xeb:num=14;break;case0xe7:num=15;brea7、k;}while(key!=0x0f){key=P3;key=key&0x0f;}}P0=dofly[num];}P3=0xdf;//第三列key=P3;key=key&0x0f;while(key!=0x0f){delay(500);key=P3;key=key&0x0f;while(key!=0x0f){key=P3;switch(key){case0xde:num=8;break;case0xdd:num=9;break;case0xdb:num=10;break;case0xd7:num=11;break;}while(key!=0x0f){key=P3;key=key&0x8、0f;}}P0=dofly[num];}P3=0xbf;//第二列key=P3;key=key&0x0f;while(key!=0x0f){delay(500);key=P3;key=key&0x0f;while(key!=0x0f){key=P3;switch(key){case0xbe:num=4;break;case0xbd:num=5;break;case0xbb:num=6;break;case0xb7:num=7;break;}while(ke
2、voidmain(){P2=0;P0=0x00;while(1){temp=scan();switch(temp){case0x7e:P0=table[0];break;//0按下相应的键显示相对应的码值case0x7d:P0=table[1];break;//1case0x7b:P0=table[2];break;//2case0x77:P0=table[3];break;//3case0xbe:P0=table[4];break;//4case0xbd:P0=table[5];break;//5case0xbb:P0=table[6];break;//6case0xb7:P0=t
3、able[7];break;//7case0xde:P0=table[8];break;//8case0xdd:P0=table[9];break;//9case0xdb:P0=table[10];break;//acase0xd7:P0=table[11];break;//bcase0xee:P0=table[12];break;//ccase0xed:P0=table[13];break;//dcase0xeb:P0=table[14];break;//ecase0xe7:P0=table[15];break;//f}}}ucharscan(){P3=0x0f;num=P3&0x
4、0f;if(num!=0x0f){delay(10);if(num!=0x0f){num=P3&0x0f;P3=num
5、0xf0;num1=P3&0xf0;}return(num+num1);}}第二种:#include"reg52.h"#defineucharunsignedchar#defineuintunsignedintunsignedcharcodedofly[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//0-Fvoiddelay(uinti)//延
6、时函数{while(i--);}voidmain(){P2=0xf8;//1数码管亮按相应的按键,会显示按键上的字符P0=dofly[0];while(1){ucharkey,num;P3=0xef;//第四列key=P3;key=key&0x0f;while(key!=0x0f){delay(500);key=P3;key=key&0x0f;while(key!=0x0f){key=P3;switch(key){case0xee:num=12;break;case0xed:num=13;break;case0xeb:num=14;break;case0xe7:num=15;brea
7、k;}while(key!=0x0f){key=P3;key=key&0x0f;}}P0=dofly[num];}P3=0xdf;//第三列key=P3;key=key&0x0f;while(key!=0x0f){delay(500);key=P3;key=key&0x0f;while(key!=0x0f){key=P3;switch(key){case0xde:num=8;break;case0xdd:num=9;break;case0xdb:num=10;break;case0xd7:num=11;break;}while(key!=0x0f){key=P3;key=key&0x
8、0f;}}P0=dofly[num];}P3=0xbf;//第二列key=P3;key=key&0x0f;while(key!=0x0f){delay(500);key=P3;key=key&0x0f;while(key!=0x0f){key=P3;switch(key){case0xbe:num=4;break;case0xbd:num=5;break;case0xbb:num=6;break;case0xb7:num=7;break;}while(ke
此文档下载收益归作者所有