Working Principle and Programming of 4×4 Matrix Keyboard

Publisher:RadiantSerenityLatest update time:2015-10-13 Source: eefocusKeywords:4×4 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
This article introduces how to use digital tubes to display the key values ​​of a 4×4 matrix keyboard on the ME300B 51/AVR single-chip computer learning and development system.

 

1. A brief introduction to the working principle of the hardware

This experiment uses the 8-bit digital tube display circuit and 4×4 matrix keyboard circuit on ME300B. The working principles of these two parts are briefly introduced below:

 

1. Working principle of 4×4 matrix keyboard

The matrix keyboard is also called a determinant keyboard. It is a keyboard composed of 4 I/O lines as row lines and 4 I/O lines as column lines. A key is set at each intersection of the row and column lines. In this way, the number of keys in the keyboard is 4×4. This determinant keyboard structure can effectively improve the utilization rate of the I/O port in the single-chip microcomputer system.

Figure 1 is the circuit diagram of the ME300B matrix keyboard, with row lines connected to P1.4-P1.7 and column lines connected to P1.0-P1.3.

【Transfer】Working principle and programming of 4×4 matrix keyboard

Figure 1   Matrix keyboard circuit

 

 

【Transfer】Working principle and programming of 4×4 matrix keyboard

Figure 2   Button arrangement

 

2. Digital tube dynamic scanning display circuit

  In the ME300B development system, an 8-bit digital tube dynamic scanning display is used. The 8 segment lines of all digital tubes are connected together accordingly and connected to the P0 port of AT89S51, and the field output is controlled by the P0 port. The common anode of each digital tube is controlled by the P2 port of AT89S51 to control Q20-Q27 to realize the bit output control of the 8-bit digital tube.

In this way, the dynamic scanning display of a group of digital tubes needs to be controlled by two groups of signals: one group is the glyph code output by the field output port, which is used to control the displayed glyph, called the segment code; the other group is the control signal output by the bit output port, which is used to select which number of digital tubes to work, called the bit code.

Since the segment lines of each digital tube are connected in parallel, the output of the segment code is the same for each digital tube. Therefore, if the bit selection lines of each digital tube are in the strobe state at the same time, the 8-bit digital tube will display the same character. If each digital tube is to display the character corresponding to the current position, a scanning display method must be used. That is, at a certain moment, only the bit selection line of a certain position is in the on state, while the bit selection lines of other positions are in the off state. At the same time, the font code of the character to be displayed in the corresponding position is output on the segment line. In this way, at the same time, only the selected position displays the character, while the other positions are off. In this way, each digital tube can display the character to be displayed.

Although these characters appear at different times, and at the same time, only one digit is displayed while the others are off, due to the afterglow characteristics of the digital tube and the persistence of vision of the human eye, as long as the display interval of each digital tube is short enough, the visual impression given to the human eye will be a continuous and stable display.

【Transfer】Working principle and programming of 4×4 matrix keyboard

Figure 3   Digital tube circuit 

The time interval of different digits of the digital tube can be achieved by adjusting the delay length of the delay program. The time interval of the digital tube display can also determine the brightness of the digital tube display. If the display time interval is long, the brightness of the digital tube will be brighter when displayed. If the display time interval is short, the brightness of the digital tube will be darker when displayed. If the display time interval is too long, the digital tube will flicker when displayed. Therefore, when adjusting the display time interval, it is necessary to consider the brightness of the digital tube during display and to prevent the digital tube from flickering when displayed.

When using digital tube to display information in ME300B MCU development system, short-circuit the 2nd and 3rd terminals of JP2. See Figure 3

 

2. Programming method of demonstration program 

1. Programming method of 4×4 matrix keyboard:

1.1. First read the status of the keyboard and get the key feature code.

First, output low level from the upper four bits of P1 port, and output high level from the lower four bits, and read the keyboard status from the lower four bits of P1 port. Then output low level from the lower four bits of P1 port, and output high level from the upper four bits, and read the keyboard status from the upper four bits of P1 port. Combining the results of the two reads can get the characteristic code of the current key. Using the above method, we get the characteristic codes of 16 keys.

Here is an example of how to get the key feature code:

Assume that the "1" key is pressed, find the key's characteristic code.

The high four bits of P1 output low level, that is, P1.4-P1.7 are output ports. The low four bits output high level, that is, P1.0-P1.3 are input ports. The state of the low four bits of P1 is "1101", and its value is "0DH".

Then the high four bits of P1 port output high level, that is, P1.4-P1.7 are input ports. The low four bits output low level, that is, P10-P13 are output ports. The state of the high four bits of P1 port is "1110", and its value is "E0H".

The characteristic code of the key is obtained by performing a logical OR operation on the P0 port status values ​​read twice, which is "EDH".

The same method can be used to obtain the characteristic codes of the other 15 keys.

 

1.2. According to the characteristic code of the key, look up the table to get the sequence code of the key.

The feature codes of the 16 keys obtained by the above method are arranged in the order of the keys in Figure 2 to form a correspondence table between feature codes and sequence codes, and then the currently read feature code is used to look up the table. When the feature code appears in the table, its position is the corresponding sequence code.

 

1.3. Specific programming of matrix keyboard key value search program

     The main functions of this demo program are:

1. Identify whether any key is pressed on the keyboard. If no key is pressed, return.

2. If a key is pressed, find out the specific key value (sequence code).

 [page]

=====================================================

Matrix keyboard key value lookup program

The key value is stored in unit 30H

=====================================================

KEY_SCAN:                          ; Identify whether a key is pressed on the keyboard subroutine

    MOV   P1,# 0F0H            ; Set the column line to 0 and the row line to 1

           MOV   A,P1                ; read P1 port

           ANL   A,#0F0H             ; take out the upper four bits

           MOV   B,A                 ; Temporarily save to B

           MOV   P1,#0FH             ; Set the column line to 1 and the row line to 0

           MOV   A,P1                ; read P1 port

           ANL   A,#0FH              ; take out the lower four bits

           ORL   A,B                 ; logical OR operation of the upper four bits and the lower four bits to recombine

           CJNE   A,#0FFH,KEY_IN1    ; 0FFH means no key is pressed

           RIGHT

 

KEY_IN1:                           ; Identify the specific key value subroutine

 MOV   B,A                 ; Temporarily store the key feature code in B

           MOV  DPTR,#KEYTABLE

           MOV  R3,#0FFH          

KEY_IN2: 

 INC   R3                   ; sequence code plus 1

           MOV   A,R3

           MOVC   A,@A+DPTR          ; Look up the table

           CJNE   A,B,KEY_IN3        ; Compare, if they are the same, find the key's feature code.

           MOV   A, R3                ; After finding the feature code, take the sequence code

           MOV   30H,A               ; store in unit 30H

           RIGHT

KEY_IN3:

  CJNE   A,#00H,KEY_IN2     ; Not finished yet, continue to check

           RET                       ; 00H is the end code

;Correspondence table between feature coding and sequence coding

KEY_TABLE:         

    DB   0EEH,0EDH,0EBH,0E7H,0DEH          ; 0,1,2,3,4,       sequence code

    DB   0DDH,0DBH,0D7H,0BEH,0BDH          ; 5,6,7,8,9,       sequence code

    DB   0BBH,0B7H,07EH,07DH,07BH, 077H    ; A,B,C,D,E,F      sequence code

    DB   00H                               ;                 End code

 

2. Programming method of 8-bit digital tube display program

 

Determine the scanning initial value and scanning direction according to the specific position of the digital tube to be used.

The number of scanned digits is determined by the number of digital tubes used.

Prepare the data to be displayed and put it into the corresponding display unit.

===========================================================

 8-bit digital tube display subroutine

; Light up 8 digital tubes from right to left

===========================================================

DISPLAY:

            MOV R1,#07FH           ; Scan the initial value and send it to R1

            MOV R2,#08H            ;Send the number of scan bits to R2

            MOV R0,#30H            ; Start filling the display unit

 

DISP1:       MOV A,@R0              ; Display content is sent to A

            MOV DPTR,#TABLE        ;Get the table header

            MOVC A,@A+DPTR         ; Look up the table to get the display data

            MOV P0,A               ; Display unit data

            MOV P2, R1              ; Start to display the current position

            MOV A,R1               ; prepare to display the next digit

            RR A                  

            MOV R1,A             

            INC R0                 ; get the next unit address

            LCALL DELAY2MS         ; Delay 2 MS

            DJNZ R2,DISP1          ; Repeat to display the next

            RET                    ; Display completed, return

 

Since only one digital tube is needed for key value display, the above display program can be optimized to obtain the following display program.

====================================================

Key value display subroutine

====================================================

KEY_PLAER:

           MOV   A,30H            ; key value data is sent to A

           MOV   DPTR,#TABLE      ;Get the segment code table address

           MOVC   A,@A+DPTR       ; Check the corresponding segment code of the displayed data

           MOV   P0,A             ; send segment code to port P0

           CLR   P2.7             ; the first digital tube display

           CALL  DELAY2MS

           SETB   P2.7

           RIGHT

TABLE:

 DB  0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H 

 DB  80H,90H,88H,83H,0C6H,0A1H,86H,8EH          ;0-F

 

3. Demonstration Program Functions

When you press any key on the matrix keyboard, the buzzer sounds and the corresponding key value is displayed on the digital tube. Figure 4 is a practical demonstration picture of the ME300B development system.

 

【Transfer】Working principle and programming of 4×4 matrix keyboard

Figure 4   ME300B development system displays key value "E"

Keywords:4×4 Reference address:Working Principle and Programming of 4×4 Matrix Keyboard

Previous article:Simulation of Ultrasonic Distance Measurement Using Single Chip Microcomputer
Next article:A Brief Analysis of KEIL C51 Reentrant Functions and Simulated Stacks

Recommended ReadingLatest update time:2024-11-16 15:55

Xiaomi's first Dimensity 8100 smartphone! Xiaomi POCO X4 GT revealed
     On May 18, according to xiaomiui.net, a new Xiaomi POCO phone with model number 22041216G appeared in the IMEI database. It was revealed that the phone will be named Xiaomi POCO X4 GT and will be released overseas in the near future.   According to leaks, there are two models in the Xiaomi POCO X4 GT series, name
[Mobile phone portable]
Xiaomi's first Dimensity 8100 smartphone! Xiaomi POCO X4 GT revealed
In April, the domestic mobile phone market shipments reached 41.728 million units, of which 5G mobile phones accounted for 39.3%.
Today (12), the latest report released by the China Academy of Information and Communications Technology showed that in April 2020, the total shipments of the domestic mobile phone market were 41.728 million units, a year-on-year increase of 14.2%, of which 5G mobile phones were 16.382 million units, accounting for 39
[Mobile phone portable]
In April, the domestic mobile phone market shipments reached 41.728 million units, of which 5G mobile phones accounted for 39.3%.
Steradian and Astra partner to produce 4D mmWave radar modules for traffic management systems
Steradian Semiconductors, an innovator of 4D radar sensing chips for autonomous driving, said recently that it has partnered with Astra Microwave Products to produce radar modules for traffic management systems. Steradian co-founder Ashish Lachhwani said Astra will provide expertise in manufacturing these radar unit
[Automotive Electronics]
MathWorks Simulink Products Now Support Infineon’s Newest AURIX™ TC4x Family of Automotive Microcontrollers
MathWorks Simulink Products Now Support Infineon’s Newest AURIX™ TC4x Family of Automotive Microcontrollers Beijing, China, December 13, 2022 - MathWorks and Infineon Technologies AG today announced the launch of a hardware support package for MathWorks Simulink® products, providing su
[Automotive Electronics]
MathWorks Simulink Products Now Support Infineon’s Newest AURIX™ TC4x Family of Automotive Microcontrollers
STC15 serial port 1\serial port 2\serial port 3 and serial port 4 source code
STC15 standard procedure, you can refer to it if necessary. The microcontroller source program is as follows: //The function of this program is to receive data from serial port 1, serial port 2, serial port 3 and serial port 4 independently, and then send the received data out #include STC15W4K32S4.H #include
[Microcontroller]
L4 autonomous driving Robobus is launched globally and will be put into operation on bus lines
  Recently, EV Vision learned from Xiamen King Long United Automotive Industry Co., Ltd. that the launch conference of the Western Autonomous Driving Open Road Test and Demonstration Operation Base with the theme of "Smart Yongchuan Travels the World" was held in Chongqing Yungu Yongchuan Big Data Industrial Park. At
[Embedded]
How to easily make a pointer thermometer using 4 components
Temperature measurement is often required in daily life, such as adjusting the thickness of clothes according to temperature changes, determining whether to turn on the air conditioner for heating or cooling according to the temperature, etc. Based on the principle of temperature sensor, this pointer thermometer is sim
[Industrial Control]
How to easily make a pointer thermometer using 4 components
Delixi inverter em60g7r5t4b parameter setting
Delixi inverter EM60G7R5T4B is a high-performance inverter, widely used in industrial automation, electric power, metallurgy, chemical industry, building materials and other industries. This article will introduce the parameter settings of Delixi inverter EM60G7R5T4B in detail, including basic parameters, control mo
[Embedded]
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号