新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 輕松搞定獨(dú)立鍵盤控制與4*4行列式矩陣鍵盤掃描

輕松搞定獨(dú)立鍵盤控制與4*4行列式矩陣鍵盤掃描

作者: 時(shí)間:2016-11-29 來源:網(wǎng)絡(luò) 收藏
電路圖很簡單就不畫了,電路的接法是p1口接矩陣鍵盤,p3口接獨(dú)立鍵盤,8個(gè)led燈接在p0口上的.
#include#define uchar unsigned char#define uint unsigned int#define on 0#define off 1sbit 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 intvoid 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));}elsescan_code=(scan_code<<1)|0x01;}}}return 0;}//*//*uchar s_scan(void){uchar scanh,scanl;P1=0xf0;if((P1&0xf0)!=0xf0){P1=0xf0;delay_ms(30);if((P1&0xf0)!=0xf0){ P1=0xfe ;//P1=scanh;if((P1&0xf0)!=0xf0){P1&=0xf0;if(P1==0xe0)return(1);if(P1==0xd0)return(2);if(P1==0xb0)return(3);if(P1==0x70)return(4);}P1=0xfd;if((P1&0xf0)!=0xf0){P1&=0xf0;if(P1==0xe0)return(5);if(P1==0xd0)return(6);if(P1==0xb0)return(7);if(P1==0x70)return(8);}P1=0xfb;if((P1&0xf0)!=0xf0){P1&=0xf0;if(P1==0xe0)return(9);if(P1==0xd0)return(10);if(P1==0xb0)return(11);if(P1==0x70)return(12);}  P1=0xf7;if((P1&0xf0)!=0xf0){P1&=0xf0;if(P1==0xe0)return(13);if(P1==0xd0)return(14);if(P1==0xb0)return(15);if(P1==0x70)return(16);}}}return 0;}*/void main(void){ uchar key_code;while(1){key_code=s_scan();if(key_code)P0=key_code;/*    delay_ms(300);led1=~led1;delay_ms(300);switch(key_code){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;}*/}}


評(píng)論


技術(shù)專區(qū)

關(guān)閉