4 x 4 matrix keyboard recognition example analysis

Publisher:草木知秋Latest update time:2015-12-24 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
   In the single-chip microcomputer application system, the keyboard is one of the indispensable components of human-computer dialogue. When there are relatively few keys, we can connect one key to one single-chip microcomputer I/O port, but when many keys are needed and I/O resources are relatively tight, using a matrix keyboard is undoubtedly the best choice. The
    4 x 4 matrix keyboard is the most commonly used keyboard form, and it is also a keyboard recognition technology that must be mastered when getting started with single-chip microcomputers. Below we will use an example to illustrate the recognition method of the 4 x 4 matrix keyboard. As shown in the figure below, we connect the keys in the form of a matrix, so that 16 keys can be recognized using 8 I/O ports, saving I/O port resources.

    Our recognition idea is this: when we first use it, we first let the lower four bits of the P1 port output a low level and the upper four bits output a high level, that is, let the P1 port output 0xF0. When scanning the keyboard, we read P1 port to see if P1 is still 0xF0. If it is still 0xF0, it means that no key is pressed; if it is not 0xF0, we wait for about 10ms, then read P1 port again, and confirm whether it is 0xF0 again. This is to prevent misidentification caused by jitter interference. If not, it means that a key is really pressed. We can read the key code to identify which key is pressed.
    Take the 0 key as an example. When it is initially used, P1 outputs 0xF0. When the 0 key is pressed, the state of the upper four bits should be 1110, that is, P1 is 0xE0, and then let P1 output 0x0F, and the state of the lower four bits should be 0111, that is, P1 is 0x07, and the two readings are ANDed to get 0xE7.
    Now we connect a two-common-anode digital tube to P2 port and P3 port respectively to display the key value of the key we press. P2 port is connected to data output, and P30 and P31 ports are connected to bit selection. For example, when the 0 key is pressed, 00 is displayed; when the 1 key is pressed, 01 is displayed; when the 15 key is pressed, 15 is displayed... The implemented simulation circuit and program are as follows: #include  // header file unsigned char num[]={ 0xc0,0xf9,0xa4,0xb0,0x99,  // digital tube common anode type code 0x92,0x82,0xf8,0x80,0x90 }; unsigned char Mykey=0; // save the current key value void delay_nms(unsigned int t)  // 12M crystal oscillator has a delay of about 1MS {  unsigned int i,j; for(i=t;i>0;i--)  for(j=112;j>0;j--)  ; } void GetKey(void)  // scan the key and store the key value in  Mykey { unsigned char X,Y,Z  ; P1=0xf0; if(P1!=0xf0) { delay_nms(10); P1=0xf0; if(P1!=0xf0) { X=P1; //Save the first read number  0xe0 P1=0x0f; Y=P1;  //Save the second read number   0x07 Z=X|Y;  //0xe7 P1=0xff ; while(P1!=0XFF);//Wait for the button to be released switch(Z) { case 0xe7:  Mykey=1;break; case 0xd7:  Mykey=2;break; case 0xb7:  Mykey=3;break;  case 0x77:  Mykey=4;break; case 0xeb:  Mykey=5;break; case 0xdb:  Mykey=6;break; case 0xbb:  Mykey=7;break; case 0x7b:  Mykey=8;break; case 0xed:  Mykey=9;break; case 0xdd:  Mykey=10;break; case 0xbd:  Mykey=11;break; case 0x7d:  Mykey=12;break; case 0xee:  Mykey=13;break; case 0xde:  Mykey=14;break; case 0xbe:  Mykey=15;break; case 0x7e:  Mykey=16;break; default :  Mykey=0; break; } } } } //Main function  void main() { unsigned char m,n  ; delay_nms(1); while(1)  //Continuous loop { GetKey()  ;  //Scan the keyboard m=Mykey/10;  //Get the high bit of the key value
4 x 4 matrix keyboard recognition example analysis




 
               
   

  
 
 
  
  
 
 
  

 
 
 
 
  
    
   
 
 
 
 
   
 
 
    
 
  
  
   
  
  
  
  
  
  
  
  
  
  
  
  
  
   
 
 
 
 





   
    
 
    
    
  n=Mykey;    //Get the low bit of the key value
  P3=0X00;   //The digital tube is blanked
  P2=num[m]; P3=0X01; //Display the high bit of the key value
  P3=0X00;     //The digital tube is blanked
  P2=num[n]; P3=0X02;   //Display the high bit of the key value
 }
}
Reference address:4 x 4 matrix keyboard recognition example analysis

Previous article:Design of STC single-chip computer serial port hardware circuit
Next article:STC MCU Best Chip Selection for Beginners

Recommended ReadingLatest update time:2024-11-15 17:52

Intel 4 process officially announced that EUV extreme ultraviolet lithography will be used for mass production for the first time
Intel officially announced that it has begun using EUV extreme ultraviolet lithography technology to mass-produce Intel 4 manufacturing processes. This is Intel's first process node produced using EUV. Compared with the previous generation, it has achieved significant improvements in performance, energy efficiency, an
[Semiconductor design/manufacturing]
Intel 4 process officially announced that EUV extreme ultraviolet lithography will be used for mass production for the first time
The first 7.56-inch Snapdragon 8 true full screen! Samsung Galaxy Z Fold4 industrial design revealed
      On March 8, whistleblower @WaqarKhanHD exposed the renderings of Samsung Galaxy Z Fold4 folding screen flagship.   As shown in the picture, the Samsung Galaxy Z Fold4 continues the industrial design language of the Galaxy Z Fold3. The outer screen is slender, and it adopts a centrally placed hole-punch screen so
[Mobile phone portable]
4K screen 865 flagship! Sony Xperia 1 Ⅱ US version officially announced: on sale from June 1st
      On May 25, Sony officially announced the release date and price of the US version of Xperia 1 Ⅱ. The world's first 4K screen 865 flagship Sony Xperia 1 Ⅱ will start pre-sale in the United States on June 1 and will be officially shipped on July 24.   As for the price, there is only one version, 8+256GB, priced at
[Mobile phone portable]
ON Semiconductor Announces Fourth Quarter and Fiscal 2021 Results
Achieved record annual and quarterly revenue, gross margin, profit and cash flow February 8, 2022 - ON Semiconductor (NASDAQ: ON) announced its fourth quarter and fiscal year 2021 results on February 7, U.S. time. The highlights are as follows: • Record fiscal year revenue of $6.74 billion, up 28.3% year-over-yea
[Semiconductor design/manufacturing]
ON Semiconductor Announces Fourth Quarter and Fiscal 2021 Results
Mainline Technology AiTrucker L4 autonomous driving system technology and application
Technical Description: For the mass production application of commercial vehicle intelligent driving in logistics and transportation scenarios, Mainline Technology has created a full-stack self-developed, controllable, software-hardware integrated "AiTrucker" L4 level autonomous driving based on a strict and complet
[Automotive Electronics]
[51 MCU Experiment] 4-Application of MCU Timer/Counter
1. Experimental Purpose 1. Master the application design of 51 single-chip microcomputer timer/counter 2. Master the design and programming of query mode and interrupt mode timing 3. Be familiar with the debugging of interrupt applications and the methods of joint debugging of software and hardware 2. Experiment
[Microcontroller]
[51 MCU Experiment] 4-Application of MCU Timer/Counter
s3c2440 bare metal-memory controller (3-4, implementation of norflash programming)
1. Identify norflash We know that to identify the norflash attributes, we need to put norflash into cfi mode, and then send a series of commands according to the table in the manual to obtain the norflash attributes. 1) Send command Then we need to implement a function nor_cmd() that allows the CPU to send command
[Microcontroller]
s3c2440 bare metal-memory controller (3-4, implementation of norflash programming)
Building an AVR RTOS (4) - A cooperative kernel with only delayed services
Part 4: A collaborative kernel with only delayed services Cooperative Multitasking What is the difference between front-end and back-end systems, collaborative kernel systems, and preemptive kernel systems? I remember seeing this analogy on 21IC: "You (the worker) are using the toilet, the manager is the first in
[Microcontroller]
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号