我的键盘扫描 方法一 但是有个小提问
时间:10-02
整理:3721RD
点击:
在图中的那里问为什么不是红色?

程序::忘了。在这里:/br
- #include <reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- //子函数集
- void delay(int i) //延时函数
- {
- while(i--);
- }
- uchar keyscanf(void) //键盘调用函数
- {
- uchar l,h;
- P3=0xf0;
- l=P3;
- l=l&0xf0;
- if (l!=0xf0)
- {
- delay(50);
- if (l!=0xf0)
- {
- l=P3&0xf0;
- l=l|0x0f;
- P3=l;
- h=P3;
- h=h&0x0f;
- l=l&0xf0;
- return(h+l); //输出键盘值
- }
- }
- return(0xff);
- }
- void main () //主函数显示 用74ls48驱动7段共阴极数码管
- {
- uchar key;
- key=keyscanf(); //调用键盘值
- switch (key)
- {
- case 0xee:P0=0x01;delay(20000);break;
- case 0xde:P0=0x02;delay(20000);break;
- case 0xbe:P0=0x03;delay(20000);break;
- case 0x7e:P0=0x0c;delay(20000);break;
- case 0xed:P0=0x04;delay(20000);break;
- case 0xdd:P0=0x05;delay(20000);break;
- case 0xbd:P0=0x06;delay(20000);break;
- case 0x7d:P0=0x0d;delay(20000);break;
- case 0xeb:P0=0x07;delay(20000);break;
- case 0xdb:P0=0x08;delay(20000);break;
- case 0xbb:P0=0x09;delay(20000);break;
- case 0x7b:P0=0x0e;delay(20000);break;
- case 0xe7:P0=0x0a;delay(20000);break;
- case 0xd7:P0=0x00;delay(20000);break;
- case 0xb7:P0=0x0b;delay(20000);break;
- case 0x77:P0=0x0f;delay(20000);break;
-
- }
- }
还有个复杂点的,,,,
- #include <reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
-
- //子函数
- void delay(int i) //延时函数
- {
- while(i--);
- }
- uchar key(void)
- {
- uchar h,numl;
- h=P2;
- while(1){
- P2=0xfe;
- {
- if(h!=0xfe)
- delay(20);
- h=P2;
- if(h!=0xfe)
- {
- switch(h)
- {
- case 0xee:numl=1;break;
- case 0xde:numl=2;break;
- case 0xbe:numl=3;break;
- case 0x7e:numl=4;break;
- }
- return(numl);
- }
- }
- P2=0xfd;
- {
- h=P2;
- if(h!=0xfd)
- delay(20);
- h=P2;
- if(h!=0xfd)
- {
- switch(h)
- {
- case 0xed:numl=5;break;
- case 0xdd:numl=6;break;
- case 0xbd:numl=7;break;
- case 0x7d:numl=8;break;
- }
- return(numl);
- }
- }
- P2=0xfb;
- {
- h=P2;
- if(h!=0xfb)
- delay(20);
- h=P2;
- if(h!=0xfb)
- {
- switch(h)
- {
- case 0xeb:numl=9;break;
- case 0xdb:numl=10;break;
- case 0xbb:numl=11;break;
- case 0x7b:numl=12;break;
- }
- return(numl);
- }
- }
- P2=0xf7;
- {
- h=P2;
- if(h!=0xf7)
- delay(20);
- h=P2;
- if(h!=0xf7)
- {
- switch(h)
- {
- case 0xe7:numl=13;break;
- case 0xd7:numl=14;break;
- case 0xb7:numl=15;break;
- case 0x77:numl=16;break;
- }
- return(numl);
- }
- }
- }
- }
- void main()
- {
- uchar a;
- a=key();
- switch (a)
- {
- case 1:P0=0x01;delay(20000);break;
- case 2:P0=0x02;delay(20000);break;
- case 3:P0=0x03;delay(20000);break;
- case 4:P0=0x0c;delay(20000);break;
- case 5:P0=0x04;delay(20000);break;
- case 6:P0=0x05;delay(20000);break;
- case 7:P0=0x06;delay(20000);break;
- case 8:P0=0x0d;delay(20000);break;
- case 9:P0=0x07;delay(20000);break;
- case 10:P0=0x08;delay(20000);break;
- case 11:P0=0x09;delay(20000);break;
- case 12:P0=0x0e;delay(20000);break;
- case 13:P0=0x0a;delay(20000);break;
- case 14:P0=0x00;delay(20000);break;
- case 15:P0=0x0b;delay(20000);break;
- case 16:P0=0x0f;delay(20000);break;
- }
- }
尽量避免p0做IO口
return(0xff)什么意思
谢谢分享。
返回,,重新开始 里面的0xff没有多少用,,,但是里面必须要有个东西,,,不然不能编译,,我试了的。也可以不用这个用while也行
学习了
写的挺乱的,我感觉小编还是再想下程序运行的过程吧,,上面的程序,在逻辑上有点不通
运行了的,,,就是在仿真中运行,,,,都可以的,,,,,,
这貌似像郭天祥矩阵键盘操作思想,你可以去参考想下
程序有点多,有点乱!看不懂!
