#include#define uchar unsigned char #define uint unsigned int #define on 0 #define off 1 sbit led1=P0^0; sbit led2=P0^1; sbit led3=P0^2; sbit led4=P0^3; sbit led5=P0^4; sbit led6=P0^5; sbit led7=P0^6; sbit led8=P0^7; sbit key1=P3^0; sbit key2=P3^1; sbit key3=P3^2; sbit key4=P3^3; sbit key5=P3^4; sbit key6=P3^5; sbit key7=P3^6; sbit key8=P3^7; void delay_ms(uchar time) { uchar i,j; for(i=time;i>0;i--) { for(j=85;j>0;j++) {;} } } uchar scan_key() { //uchar i,j; //P3=0xff; if((P3&0xff)!=0xff) { delay_ms(30); if((P3&0xff)!=0xff) { if(key1==0) return(1); if(key2==0) return(2); if(key3==0) return(3); if(key4==0) return(4); if(key5==0) return(5); if(key6==0) return(6); if(key7==0) return(7); if(key8==0) return(8); } } return 0; } void main(void) { uchar key_value; key_value=scan_key(); switch(key_value) { case 1:led1=on;break; case 2:led2=on;break; case 3:led3=on;break; case 4:led4=on;break; case 5:led5=on;break; case 6:led6=on;break; case 7:led7=on;break; case 8:led8=on;break; default:P0=0xff; } } 4*4行列式矩阵键盘 #includesbit led1 =P0^0 ; sbit led2 =P0^1 ; sbit led3 =P0^2 ; sbit led4 =P0^3 ; sbit led5 =P0^4 ; sbit led6 =P0^5 ; sbit led7 =P0^6 ; sbit led8 =P0^7 ; #define on 0 #define off 1 #define uchar unsigned char #define uint unsigned int void delay_ms(uint time) { uint i,j; for(i=time;i>0;i--) for(j=112;j>0;j--) {} } //d/* uchar s_scan(void) { uchar scan_code,temp; P1=0xf0; if((P1&0xf0)!=0x0f) { delay_ms(30); if((P1&0xf0)!=0xf0) { scan_code=0xfe; while((scan_code&0x10)!=0) { P1=scan_code; if((P1&0xf0)!=0xf0) { temp=(P1&0xf0)|0x0f; while((P1&0xf0)!=0xf0) return ((~scan_code)+(~temp)); } else scan_code=(scan_code