微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 轻松搞定独立键盘控制与4*4行列式矩阵键盘扫描

轻松搞定独立键盘控制与4*4行列式矩阵键盘扫描

时间:11-29 来源:互联网 点击:
电路图很简单就不画了,电路的接法是p1口接矩阵键盘,p3口接独立键盘,8个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;}*/}}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top