2898 views |2 replies
Last login 2018-7-30
Online Time 13 hours
Prestige 26 points
Points 0 points
Matrix keyboard, digital tube display program, please help to simplify the code
[Copy link]
As the title says: The following is a code for matrix keys and digital tube display in sequence. I am a novice and I wrote it by myself. It is very complicated. Please simplify the code for novice learning.... Purpose of the program: For example: Press the 1,2,3,4,5 keys of the matrix keyboard in sequence, the digital tube will display 12345 from the fifth digit.
For example: Press the 1,2,3 keys of the matrix keyboard in sequence, the digital tube will display 123 from the third digit. The program is as follows: #include
//Data and port definition module typedef unsigned char u8; typedef unsigned int u16; #define DATA P0 sbit LSA=P2^2; sbit LSB=P2^3; sbit LSC=P2^4; //Global variable u8 wei,shu,shu1,shu2,shu3,shu4,shu5,shu6,shu7,shu8,temp2,num1; //Delay module void delay(u16 num) { u16 x,y; for(x=num;x>0;x--) for(y=110;y>0;y--); } //Common cathode digital tube_code table array u8 code seg_tab[]={ 0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71 }; //74LS138 decoding void bit_sel(u8 num) { switch(num) { case(1): LSA=0;LSB=0;LSC=0; break; case(2): LSA=1;LSB=0;LSC=0; break; case(3): LSA=0;LSB=1;LSC=0; break; case(4): LSA=1;LSB=1;LSC=0; break; case(5): LSA=0;LSB=0;LSC=1; break; case(6): LSA=1;LSB=0;LSC=1; break; case(7): LSA=0;LSB=1;LSC=1; break; case(8): LSA=1;LSB=1;LSC=1; break; } } //bit,DATA function encapsulation void digdisplay(u8 bits,u8 datas) { bit_sel(bits); DATA=seg_tab[datas]; delay(1); DATA=0x00; } //Key detection u8 anjian(void) { u8 temp; P1=0xf0; delay(2); if(P1!=0xf0) { delay(1); if(P1!=0xf0) { temp=P1; temp2=temp & 0xf0; P1=0x0f; temp=P1; temp2 |= temp; wei++; delay(500); return temp2,wei; } } } //Key code table u8 anjian_biao(u8 shu) { u8 num; switch(shu) { case 0x77:num=0; break; case 0x7b:num=1; break; case 0x7d:num=2; break; case 0x7e:num=3; break; case 0xb7:num=4; break; case 0xbb:num=5; break; case 0xbd:num=6; break; case 0xbe:num=7; break; case 0xd7:num=8; break; case 0xdb:num=9; break; case 0xdd:num=10; break; case 0xde:num=11; break; case 0xe7:num=12; break; case 0xeb:num=13; 0xed:num=14; break; case 0xee:num=15; break; } return num;//Return the value of num} //Into the library void ruku(u8 wei2) { wei2=wei; switch(wei2) { case(1):shu1=temp2; break; case(2):shu2=temp2; break; case(3):shu3=temp2; break; case(4):shu4=temp2; break; case(5):shu5=temp2; break; case(6):shu6=temp2; break; case(7):shu7=temp2; break; case(8):shu8=temp2; break; } } //Display library u8 xianku(u8 wei) { switch(wei) { case(1):shu=shu1; break; case(2):shu=shu2; break; case(3):shu=shu3; break; case(4):shu=shu4; break; case(5):shu=shu5; break; case(6):shu=shu6; break; case(7):shu=shu7; break; case(8):shu=shu8; LSB=0; LSC=0; P0=0x3f; wei=0; while(1) { anjian(); //Key detection ruku(wei); //Storage wei1=wei; wei3=1; while(wei1>=1,wei1<9) { cod=xianku(wei1); num1=anjian_biao(cod); digdisplay(wei3,num1); if(wei1>1) { wei3++; } wei1--; } } } I wish you all a happy life and a successful work.