本帖最后由 wulin 于 2019-3-15 07:38 编辑
你的显示程序和数组写的都有问题,4*8矩阵键盘扫描程序也繁琐,不是很清楚你的用途,
给你改了一下,不影响P1高4位另做他用,直接用P0短暂显示键值1~32,经仿真无误。你可
以按需自行修改。
- #include <reg52.h>
- #include <intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- /*
- uchar code tab[]={0xfe,0xfd,0xfc,0xfb,0xfa,0xf9,0xf8,0xf7,
- 0xf6,0xf5,0xf4,0xf3,0xf2,0xf1,0xf0,0xef,
- 0xee,0xed,0xec,0xeb,0xea,0xfe9,0xe8,0xe7,
- 0xe6,0xe5,0xe4,0xeb3,0xe2,0xe1,0xeff};//输出二进制库
- //输出为11110 11101 到00001
- */
- uint temp=0;//计数变量
- uchar key=0xff;//键值变量
- bit key_yes=0;//
- //void display();
- //void keyscan();
- /*
- void delay(uchar z)
- {
- uchar x,y;
- for(x=z;x>0;x--)
- for(y=100;y>0;y--);
- }*/
- void keyscan() //4*8矩阵键盘扫描程序
- {
- static bit sign=0; //按键自锁标志
- static uint count=0; //消抖计数变量
- uint num,num1;
- P1|=0x0f;//保持P1高4位不变,操作低4位1111
- P1&=0xf0;//保持P1高4位不变,操作低4位0000
- P2=0xff;//P2全高
- num=(P1&0x00)<<8|P2;
- if(num!=0x00ff)//判断有键按下
- {
- if((++count>=250)&&(sign==0))//消抖与判断自锁
- {
- sign=1; //按键自锁标志置1
- P1|=0x0f; //保持P1高4位不变,操作低4位1111
- P2=0x00; //P2全低
- num1=(P1&0x0f)<<8|P2;
- num|=num1;
- switch(num)
- {
- case 0x0efe: key= 1; break;
- case 0x0efd: key= 2; break;
- case 0x0efb: key= 3; break;
- case 0x0ef7: key= 4; break;
- case 0x0eef: key= 5; break;
- case 0x0edf: key= 6; break;
- case 0x0ebf: key= 7; break;
- case 0x0e7f: key= 8; break;
- case 0x0dfe: key= 9; break;
- case 0x0dfd: key=10; break;
- case 0x0dfb: key=11; break;
- case 0x0df7: key=12; break;
- case 0x0def: key=13; break;
- case 0x0ddf: key=14; break;
- case 0x0dbf: key=15; break;
- case 0x0d7f: key=16; break;
- case 0x0bfe: key=17; break;
- case 0x0bfd: key=18; break;
- case 0x0bfb: key=19; break;
- case 0x0bf7: key=20; break;
- case 0x0bef: key=21; break;
- case 0x0bdf: key=22; break;
- case 0x0bbf: key=23; break;
- case 0x0b7f: key=24; break;
- case 0x07fe: key=25; break;
- case 0x07fd: key=26; break;
- case 0x07fb: key=27; break;
- case 0x07f7: key=28; break;
- case 0x07ef: key=29; break;
- case 0x07df: key=30; break;
- case 0x07bf: key=31; break;
- case 0x077f: key=32; break;
- }
- key_yes=1;
- }
- }
- else //键抬起
- {
- sign=0; //按键自锁标志清0
- count=0; //消抖计数清0
- }
- }
- /*
- void display()
- {
- {
- P0=tab[n1];
- }
- }
- */
- void main()
- {
- while(1)
- {
- keyscan();
- if(key_yes==1)
- {
- P0=key;//读键值P0显示
- temp++;
- if(temp>=10000)//延时时间可调
- {
- temp=0;
- key_yes=0;
- P0=0xff;
- }
- }
- }
- }
复制代码
|