(1) As shown in Figure 14.2, the 4×4 matrix keyboard is connected to the parallel port P3 of the single-chip microcomputer, and the P3.0-P3.3 pins of the single-chip microcomputer are used as input lines, and the P3.4-P3.7 pins of the single-chip microcomputer are used as output lines. The serial number of each key "0-F" is displayed on the digital tube.
(2) The serial number arrangement of the corresponding keys on the keyboard is shown in Figure 14.1.
2. Reference circuit
Figure 14.2 Schematic diagram of 4×4 matrix keyboard recognition circuit
3. Circuit hardware description
(1) In the "MCU System" area, connect the P3.0-P3.7 ports of the MCU to the M1-M4 and N1-N4 ports in the "4×4 Column Keyboard" area through the 8-link DIP switch JP3.
(2) In the "MCU System" area, connect the P0.0-P0.7 ports of the MCU to any ah port in the "Static Digital Display Module" area; requirements: P0.0 corresponds to a, P0.1 corresponds to b, ..., P0.7 corresponds to h.
4. Programming content
(1) 4×4 matrix keyboard recognition processing.
(2) Each key has its row value and column value. The combination of the row value and the column value is the code to identify the key. The row and column lines of the matrix communicate with the CPU through two parallel interfaces respectively. One end of the keyboard (column line) is connected to VCC through a resistor, and grounding is achieved by the program outputting the number "0". The task of the keyboard processing program is to determine whether a key is pressed, determine which key is pressed, and what is the function of the key? It is also necessary to eliminate the jitter of the key when it is closed or open. Among the two parallel ports, one outputs the scan code to dynamically ground the key row by row; the other parallel port inputs the key status, and the key is identified by forming a key code together with the row scan value and the feedback signal. The function of the key is found out through software table lookup.
5. Program flow chart (as shown in Figure 14.3)
[page]
6. Assembly source program
;;;;;;;;;;;Define unit;;;;;;;;;;;;
COUNT EQU 30H
;;;;;;;;;;;Entrance address;;;;;;;;;;;
ORG 0000H
LJMP START
ORG 0003H
RARELY
ORG 000BH
RARELY
ORG 0013H
RARELY
ORG 001BH
RARELY
ORG 0023H
RARELY
ORG 002BH
RARELY
;;;;;;;;;;;Main program entry;;;;;;;;;;;;
ORG 0100H
START: LCALL CHUSHIHUA
LCALL GUIDE
LCALL XIANSHI
LJMP START
;;;;;;;;;;;Initialization procedure;;;;;;;;;;;;
CHUSHIHUA: MOV COUNT,#00H
RIGHT
;;;;;;;;;;Determine which button is pressed in the program;;;;;;;;;;;
PANDUAN: MOV P3,#0FFH
CLR P3.4
MOV A,P3
ANL A,#0FH
XRL A,#0FH
JZ SW1
LCALL DELAY10MS
JZ SW1
MOV A,P3
ANL A,#0FH
CJNE A,#0EH,K1
MOV COUNT,#0
LJMP DK
K1: CJNE A,#0DH,K2
MOV COUNT,#4
LJMP DK
K2: CJNE A,#0BH,K3
MOV COUNT,#8
LJMP DK
K3: CJNE A,#07H,K4
MOV COUNT,#12
K4: NOP
LJMP DK
SW1: MOV P3,#0FFH
CLR P3.5
MOV A,P3
ANL A,#0FH
XRL A,#0FH
JZ SW2
LCALL DELAY10MS
JZ SW2
MOV A,P3
ANL A,#0FH
CJNE A,#0EH,K5
MOV COUNT,#1
LJMP DK
K5: CJNE A,#0DH,K6
MOV COUNT,#5
LJMP DK
K6: CJNE A,#0BH,K7
MOV COUNT,#9
LJMP DK
K7: CJNE A,#07H,K8
MOV COUNT,#13
K8: NOP
LJMP DK
SW2: MOV P3,#0FFH
CLR P3.6
MOV A,P3
ANL A,#0FH
XRL A,#0FH
SW SW3
LCALL DELAY10MS
SW SW3
MOV A,P3
ANL A,#0FH
CJNE A,#0EH,K9
MOV COUNT,#2
LJMP DK
K9: CJNE A,#0DH,KA
MOV COUNT,#6
LJMP DK
TO: CJNE A,#0BH,KB
MOV COUNT,#10
LJMP DK
KB: CJNE A,#07H,KC
MOV COUNT,#14
KC: NOP
LJMP DK
SW3: MOV P3,#0FFH
CLR P3.7
MOV A,P3
ANL A,#0FH
XRL A,#0FH
JZ SW4
LCALL DELAY10MS
JZ SW4
MOV A,P3
ANL A,#0FH
CJNE A,#0EH,KD
MOV COUNT,#3
LJMP DK
KD: CJNE A,#0DH,KE
MOV COUNT,#7
LJMP DK
KE: CJNE A,#0BH,KF
MOV COUNT,#11
LJMP DK
KF: CJNE A,#07H,KG
MOV COUNT,#15
KG: NOP
LJMP DK
SW4: LJMP GUIDE
DK: RIGHT
;;;;;;;;;;;Show program;;;;;;;;;;;
XIANSHI: MOV A,COUNT
MOV DPTR,#TABLE
MOVC A,@A+DPTR
MOV P0,A
LCALL DELAY
SK: MOV A,P3
ANL A,#0FH
XRL A,#0FH
JNZ SK
RIGHT
;;;;;;;;;;;10ms delay program;;;;;;;;;;;;
DELAY10MS: MOV R6,#20
D1: MOV R7,#248
DJNZ R7,$
DJNZ R6,D1
RIGHT
;;;;;;;;;;;200ms delay program;;;;;;;;;;;;
DELAY: MOV R5,#20
LOOP: LCALL DELAY10MS
DJNZ R5,LOOP
RIGHT
;;;;;;;;;;;Common Yin code table;;;;;;;;;;;;
TABLE: DB 3FH,06H,5BH,4FH,66H,6DH,7DH,07H
DB 7FH,6FH,77H,7CH,39H,5EH,79H,71H
;;;;;;;;;;;End sign;;;;;;;;;;;;
END
[page]
#include
unsigned char code table[]={0x3f,0x66,0x7f,0x39,
0x06,0x6d,0x6f,0x5e,
0x5b,0x7d,0x77,0x79,
0x4f,0x07,0x7c,0x71};
void main(void)
{ unsigned char i,j,k,key;
while(1)
{ P3=0xff; //Set P3 port to 1//
P3_4=0; //Send 0 to the P3.4 line//
i=P3;
i=i&0x0f; //shield the lower four bits//
if(i!=0x0f) //Check if any key is pressed//
{ for(j=50;j>0;j--) //delay//
for(k=200;k>0;k--);
if(i!=0x0f) //Judge again whether the button is pressed//
{ switch(i) //See which of the four buttons is connected to P3.4//
{ case 0x0e:
key=0;
break;
case 0x0d:
key=1;
break;
case 0x0b:
key=2;
break;
case 0x07:
key=3;
break;
}
P0=table[key]; //Send data to P0 port for display//
}
}
P3=0xff;
P3_5=0; //Read the P3.5 line//
i=P3;
i=i&0x0f; //Shield the lower four bits of P3 port//
if(i!=0x0f) //Read P3.5 line to see if any button is pressed//
{ for(j=50;j>0;j--) //delay//
for(k=200;k>0;k--);
i=P3; //Check if any button is actually pressed//
i=i&0x0f;
if(i!=0x0f)
{ switch(i) //If yes, display the corresponding button//
{ case 0x0e:
key=4;
break;
case 0x0d:
key=5;
break;
case 0x0b:
key=6;
break;
case 0x07:
key=7;
break;
}
P0=table[key]; //Send to P0 port for display//
}
}
P3=0xff;
P3_6=0; //Read whether there is a button pressed on the P3.6 line//
i=P3;
i=i&0x0f;
if(i!=0x0f)
{ for(j=50;j>0;j--)
for(k=200;k>0;k--);
i=P3;
i=i&0x0f;
if(i!=0x0f)
{ switch(i)
{ case 0x0e:
key=8;
break;
case 0x0d:
key=9;
break;
case 0x0b:
key=10;
break;
case 0x07:
key=11;
break;
}
P0=table[key];
}
}
P3=0xff;
P3_7=0; //Read whether there is a button pressed on the P3.7 line//
i=P3;
i=i&0x0f;
if(i!=0x0f)
{ for(j=50;j>0;j--)
for(k=200;k>0;k--);
i=P3;
i=i&0x0f;
if(i!=0x0f)
{ switch(i)
{ case 0x0e:
key=12;
break;
case 0x0d:
key=13;
break;
case 0x0b:
key=14;
break;
case 0x07:
key=15;
break;
}
P0=table[key];
}
}
}
}
8. Notes
In the hardware circuit, turn down the 8-link DIP switch JP2 and turn up the 8-link DIP switch JP3.
Previous article:NIOSⅡ implements matrix keyboard and LCD display peripheral components
Next article:Application of SPMC75 and ASM (assembly) mixed programming
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- How much do you know about 5G antennas?
- Please help me with this circuit
- Understanding the energy storage function of decoupling capacitors
- Design of infrared communication circuit for single chip microcomputer
- An SI engineer who does not understand processing cannot be called Mr. High Speed
- EEWORLD University Hall----Live Replay: Use ModusToolbox? to build a system to flexibly respond to IoT design challenges
- Why is the square wave voltage generated by the inverter circuit measured using the AC voltage range of a digital multimeter much smaller than the theoretical value?
- Design of three-coordinated distributed control system based on TMS320F2812 DSP
- Several ways to accurately delay the microcontroller
- Bluetooth tester function details!