单片机 键盘

#include unsigned char table[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8, 0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E}; void delay(void) { unsigned char i,j; for(i=0;i<20;i++) for(j=0;j<250;j++); } void display(unsigned char i) { P2=0xfe; P0=table[i]; } void keyscan(void) { unsigned char n; //扫描第一行 P3=0xfe; n=P3; n&=0xf0; if(n!=0xf0) { delay(); P3=0xfe; n=P3; n&=0xf0; if(n!=0xf0) { switch(n) { case(0xd0):display(1);break; case(0xb0):display(2);break; case(0x70):display(3);break; } } } //扫描第二行 P3=0xfd; n=P3; n&=0xf0; if(n!=0xf0) { delay(); P3=0xfd; n=P3; n&=0xf0; if(n!=0xf0) { switch(n) { case(0xd0):display(4);break; case(0xb0):display(5);break; case(0x70):display(6);break; } } } //扫描第三行 P3=0xfb; n=P3; n&=0xf0; if(n!=0xf0) { delay(); P3=0xfb; n=P3; n&=0xf0; if(n!=0xf0) { switch(n) { case(0xd0):display(7);break; case(0xb0):display(8);break; case(0x70):display(9);break; } } } //扫描第四行 P3=0xf7; n=P3; n&=0xf0; if(n!=0xf0) { delay(); P3=0xf7; n=P3; n&=0xf0; if(n!=0xf0) { switch(n) { case(0xd0):display(0);break; } } } } void main(void) { while(1) { keyscan(); } }

你可能感兴趣的:(嵌入式)