Design of 4×4 matrix keyboard based on single chip microcomputer

Publisher:shengjuLatest update time:2011-10-21 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
1. Design principles

(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

4×4 matrix keyboard recognition circuit schematic

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]

7. C language source program

#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.

Reference address:Design of 4×4 matrix keyboard based on single chip microcomputer

Previous article:NIOSⅡ implements matrix keyboard and LCD display peripheral components
Next article:Application of SPMC75 and ASM (assembly) mixed programming

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号