资源描述:
《基于ats的矩阵键盘(matrix keyboard based on ats)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、基于at89s52的矩阵键盘(MatrixkeyboardbasedonAT89S52)/******************************************Function:matrixkeyboard(pressthecorrespondingbutton,digitaltubefrom0~Fdisplay)Singlechipmicrocomputer:AT89S52******************************************/#include#d
2、efineuintunsignedint#defineucharunsignedcharSbitDAT=P0^3;SbitCLK=P0^2;Uchar,temp,h;Voiddelay(uint);//delayproceduresVoidsendbyte(uchar);//digitaldisplayVoid(keyscan);//keyscanUcharcodetab[]={0xed,0x09,0xbc,0x9d,0x59,0xd5,0xf5,0x0d,0xfd,0xdd,0x7d,0xf1,0xe4,0x
3、b9,0xf4,0x74,0x00},//0-F,totaleliminationVoidmain(void){Sendbyte(16);//initialdigitaltubedisplayWhile(1){(keyscan);//keyscan}}Voiddelay(uintz){Uint,x,y;For(x=z;x>0;x--)For(y=100;y>0;y--);}Voidsendbyte(ucharbyte){Uchar,num,c;Num=tab[byte];For(c=0;c<8;c++){CLK
4、=0;DAT=num&0x01;CLK=1;Num>>=1;//rightshiftassignment}}Void,keyscan(){Thefirstlineofthekeyscan/*P2=0xfe;//determinethefirstlineofthekeyiseffectiveTemp=P2;//assignittoavariable(I/Oport,thefirstgeneralassignmenttoavariable,andthenthroughtheprocessingvariablesto
5、handletheI/Oport)Temp=temp&0xf0;//forwhichthefirstlineofthepressbuttondetectionWhile(temp!=0xf0)/*thispartaslongasisusedtoeliminatethejitter/press{Delay(5);Temp=P2;Temp=temp&0xf0;While(temp!=0xf0)//thisplacehasbeeneliminated,pressthejitter,P2valueshavebeende
6、termined{Temp=P2;//P2portvalueassignedtothevariableSwitch(Temp)//theswitchstatementisusedtodeterminewhichbuttonispressed,thedigitaltubedisplayvalue{Case0x7e:h=0;Breakbreak;//thisisveryimportant,ifthereisamatchingvalue,jumpoutoftheswitchstatement,topreventthe
7、programjumpout.Case0xbe:h=1;Break;Case0xde:h=2;Break;Case0xee:h=3;Break;Default:h=16;Break;}While(temp!=0xf0)/*thispartaslongasisusedtoeliminatethereleasejitter.{Temp=P2;Temp=temp&0xf0;}Sendbyte(H);//tothedigitaltubedisplay}}Secondrowsofkeys/*scanP2=0xfd;Tem
8、p=P2;Temp=temp&0xf0;While(temp,=0xf0){Delay(5);Temp=P2;Temp=temp&0xf0;While(temp,=0xf0){Temp=P2;switch(temp){case0x7d:h=4;break;case0xbd:h=5;break;case0xdd:h=6;break;case0xed:h=7;break;default:h