#include<reg52.h> #define uint unsigned int #define uchar unsigned char uchar temp,key_num; uchar code table[]={ 0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71,0}; uchar code scan[4] = {0xfe, 0xfd, 0xfb, 0xf7}; void delay(uint z) { uint x,y; for(x=z;x>0;x--) for(y=110;y>0;y--); } uchar keyscan(); void mian() { P2=0x0f; while(1) { keyscan(); delay(10); P0=table[key_num]; } } uchar keyscan() { uint i; for(i=0;i<4;i++) { P1=scan;//按键检测 temp=P1; temp=temp&0xf0; while(temp!=0xf0) { delay(10); P1=scan; temp=P1; temp=temp&0xf0; while(temp!=0xf0) { temp=P1; switch(temp&0xf0) { case 0xe0: key_num=i*4; break; case 0xd0: key_num=i*4+1; break; case 0xb0: key_num=i*4+2; break; case 0x70: key_num=i*4+3; break; } while(temp!=0xf0)//松手检测 { temp=P1; temp=temp&0xf0; } } } } return key_num; }
这是我自己写的一个键盘扫描的程序,p1口接矩阵键盘,p0口接数码管,p2的第四位控制数码管的位选端,把程序写进去的时候,数码管一直都显示不出来,不知道哪里出了问题,请求高手解答 |