仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include<reg51.h>
- unsigned int key;
- unsigned int temp;
- void delay(unsigned int x);
- unsigned char code tabll[]={
- 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
- 0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0x00};
- sbit c1=P2^4;
- sbit c2=P2^5;
- sbit c3=P2^6;
- sbit c4=P2^7;
- void main()
- {
- while(1)
- {
- P2=0xff;
- c1=0;
- temp=P2;
- if((temp&0x0f)!=0x0f)
- {
- // delay(10);
- temp=P2;
- if((temp&0x0f)!=0x0f)
- {
- switch(temp)
- {
- case 0xee:key=3;break;
- case 0xed:key=2;break;
- case 0xeb:key=1;break;
- case 0xe7:key=0;break;
- }
- P0=tabll[key];
- }
- }
- P2=0xff;
- c2=0;
- temp=P2;
- if((temp&0x0f)!=0x0f)
- {
- delay(10);
- temp=P2;
- if((temp&0x0f)!=0x0f)
- {
- switch(temp)
- {
- case 0xde:key=7;break;
- case 0xdd:key=6;break;
- case 0xdb:key=5;break;
- case 0xd7:key=4;break;
- }
- P0=tabll[key];
- }
- }
- P2=0xff;
- c3=0;
- temp=P2;
- if((temp&0x0f)!=0x0f)
- {
- delay(10);
- temp=P2;
- if((temp&0x0f)!=0x0f)
- {
- switch(temp)
- {
- case 0xbe:key=11;break;
- case 0xbd:key=10;break;
- case 0xbb:key=9;break;
- case 0xb7:key=8;break;
- }
- P0=tabll[key];
- }
- }
- P2=0xff;
- c4=0;
- temp=P2;
- if((temp&0x0f)!=0x0f)
- {
- delay(10);
- temp=P2;
- if((temp&0x0f)!=0x0f)
- {
- switch(temp)
- {
- case 0x7e:key=15;break;
- case 0x7d:key=14;break;
- case 0x7b:key=13;break;
- case 0x77:key=12;break;
- }
- P0=tabll[key];
- }
- }
- }
- }
- void delay(unsigned int x)
- {
- unsigned int i,j;
- for(i=x;i>0;i--)
- for(j=200;j>0;j--);
- }
复制代码
所有资料51hei提供下载:
4x4矩阵式按键.rar
(51.13 KB, 下载次数: 30)
|