Learn 51 MCU with me (IV): Independent-matrix keyboard application and design

Publisher:不见南师久Latest update time:2013-02-06 Source: dzsc Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
  The previous lecture introduced and applied the serial port communication of the microcontroller, and gave examples. From this lecture, we will introduce the peripheral circuits of the microcontroller. This lecture introduces the circuit of the microcontroller external keyboard. Through this lecture, readers can understand the working principle of the microcontroller external keyboard and realize circuit and program design.

  1. Principle Introduction

  The keyboard interface circuit is a very important part of the single-chip system design. As the most commonly used input device in the human-computer interaction interface, we can input data or commands through the keyboard to achieve simple human-computer communication. Before designing the keyboard circuit and program, we need to understand some knowledge about the keyboard and the keys that make up the keyboard.

  1. Classification of buttons

  Generally speaking, buttons can be divided into two categories according to their structural principles. One is contact switch buttons, such as mechanical switches, conductive rubber switches, etc.; the other is non-contact switch buttons, such as electrical buttons, magnetic induction buttons, etc. The former is low in cost, and the latter has a long life. At present, the most common type of switch button in the microcomputer system is the contact switch button (such as the button used in this learning board).

  According to the interface principle, keys can be divided into two categories: coded keyboards and non-coded keyboards. The main difference between these two types of keyboards is the method of identifying key symbols and giving corresponding key codes. Coded keyboards mainly use hardware to realize key recognition, while non-coded keyboards mainly use software to realize keyboard recognition.

  The fully coded keyboard uses a special chip to recognize keys and output the corresponding codes. It generally has de-jitter and multi-key and key-channel protection circuits. This keyboard is easy to use, but has high hardware costs. It is rarely used in general small embedded application systems. Non-coded keyboards can be divided into independent and matrix types according to the connection method. Other work is mainly completed by software. Because it is economical and practical, it is widely used in single-chip microcomputer systems (this learning board also uses non-coded keyboards).

  2. Key input principle

  In the single-chip microcomputer application system, mechanical contact key switches are usually used, and their main function is to convert mechanical on and off into electrical logical relationships. In other words, it can provide standard TTL logic levels to be compatible with the logic levels of general digital systems. In addition, except for the reset button, which has a dedicated reset circuit and a dedicated reset function, other buttons are used to set control functions or input data in switch states. When the set function key or number key is pressed, the computer application system should complete the function set by the key. Therefore, key information input is a process closely related to the software structure. For a group of keys or a keyboard, it is connected to the single-chip microcomputer through an interface circuit. The single-chip microcomputer can use query or interrupt mode to understand whether there is key input and check which key is pressed. If a key is pressed, it jumps to the corresponding keyboard processing program to execute, and if no key is pressed, it continues to execute other programs.

  3. Key features and debounce

  When a mechanical key is pressed or released again, due to the influence of mechanical elasticity, it is usually accompanied by a certain period of mechanical contact jitter before the contact stabilizes. The jitter process is shown in Figure 1 (a). The length of the jitter time is related to the mechanical characteristics of the switch, generally 5 to 10 ms. As can be seen from the figure, detecting the on and off state of the key during the contact jitter may lead to misjudgment. That is, a single press or release of the key is mistakenly regarded as multiple operations, which is not allowed. In order to overcome the misjudgment caused by the mechanical jitter of the key contact, de-jitter measures must be taken, which can be considered from both hardware and software aspects. Generally speaking, when the number of keys is small, hardware de-jitter can be used, and when the number of keys is large, software de-jitter can be used. (This learning board uses software de-jitter). The flowchart of software de-jitter is shown in Figure 1 (b).

Figure 1

  From the debounce flow chart of the key, we can know that when a key is detected to be pressed, it should be delayed for a period of time (a 5ms~10ms delay subroutine can be called), and then it is judged again whether the key is pressed. If it is judged that the key is still pressed at this time, it is considered that the key is valid. If it is judged that the key is not pressed at this time, it means that the key is jittering or interfering, and it should return to re-judgment. The corresponding processing procedure can only be performed when the keyboard is actually pressed. At this time, the key input is basically realized, and further, it can be judged whether the key is released. [page]

    2. Circuit Detail

  The circuit diagram is shown in Figure 2.

Figure 2

  As shown in Figure 2, the independent key adopts a structure in which each key occupies an I/O port line. When the key is pressed and released, the level input to the microcontroller I/O port is different, so it is possible to determine whether a key is pressed and which key is pressed based on the changes in the levels of different ports. As can be seen from Figure 2 (a), the key and the microcontroller pin are connected and a pull-up resistor is added, so that when no key is pressed, the I/O input level is high, and when a key is pressed, the I/O input level is low.

  Although the independent key circuit configuration is flexible and the software structure is simple, each key must occupy an I/O port line. Therefore, when there are many keys, the I/O port line is wasted. For more complex systems or occasions with more keys, a matrix keyboard can be used. The one shown in Figure 2 (b) is a 4×4 matrix keyboard. The design methods of other matrix keyboards are similar.

  The 4×4 matrix keyboard is composed of 4 row lines and 4 column lines intersecting, and the keys are located at the intersection of the rows and columns, thus forming 16 keys. The row and column lines at the intersection are not connected. When the key is pressed, the row and column lines at this intersection are turned on. Figure 2 (b) The row line is connected to VCC through a pull-up resistor. When no key is pressed, the row line is in a high level state; when a key is pressed, the row and column lines are turned on at the intersection. At this time, the row line level will be determined by the column line level connected to this row line. This is the key to identifying whether the key is pressed. However, each row line in the matrix keyboard intersects with 4 column lines. Whether the key at the intersection is pressed or not affects the level of the row and column lines where the key is located. The keys will affect each other. When analyzing the key, the row and column line signals must be combined and properly processed to determine the position of the closed key.

  It is worth noting that the matrix keyboard introduced in this article adds a four-input AND gate chip 74HC21 to the output end of the traditional matrix keyboard. When one of the four inputs is low, the output is low. Connect the output end of 74HC21 to the external interrupt 0 (P32 pin) of the microcontroller. In this way, when the real-time requirement is high, set P00~P03 to all low to wait for the key trigger. When any key is pressed, the system will enter the interrupt service program, which improves the keyboard response time and is very practical when the system real-time requirement is high. The full source code of this article can be found at www.ele169.com.

  3. Programming

  The key procedures of the design example in this article are as follows.

  Independent key program

  …

  #define keyio P0 (1)

  #define key1 P0_3 (2)

  …

  keyio|=0X0F; (3)

  if (key1 == 0) (4)

  {

  delay_nms(20); (5)

  if (key1 == 0) (6)

  {

  while(key1==0); (7)

  return 1; (8)

  }

  }

[page]

    Program Description:

  (1) Define the button pins.

  (2) Define the button connection pins.

  (3) Connect the button to the pin to output a high level, thereby receiving input.

  (4) If the button connected to the pin is pressed at this time.

  (5) Delay for a period of time to de-bounce the operation.

  (6) If the button is still pressed, it is valid.

  (7) Wait for the key to be released, an infinite loop. If the key is pressed, the waiting process continues.

  (8) Return the key value. Matrix keyboard program

  …

  #define KEYIO P0 (1)

  …

  code ksp[4]={0x7F,0xBF,0xDF,0xEF}; (2)

  unsigned char keypad_scan() (3)

  {

  char key,i; (4)

  KEYIO=0xF0; (5)

  if (KEYIO!=0xF0) (6)

  {

  for (i=0;i<=3;i++) (7)

  {

  delayKey(10); (8)

  KEYIO=ksp[i]; (9)

  delayKey(10); (10)

  if (KEYIO!=ksp[i]) (11)

  {

  delayKey(10); (12)

  key=KEYIO; (13)

  while(KEYIO==key); (14)

  return (key); (15)

  }

  }

  }

  }

[page]

    Program Description:

  (1) Define the matrix keyboard pins.

  (2) Define the four output level states of the pins used during scanning as an array.

  (3) Key scanning program.

  (4) Define two temporary variables key and i.

  (5) Let the upper four bits of the keyboard pin output high level and the fourth bit be low level, ready for scanning keys.

  (6) If the pin status level changes at this time.

  (7) Assign the values ​​in the previously defined array to the pins and start scanning one by one.

  (8) Delay for a period of time to eliminate jitter.

  (9) Output the scan button voltage level.

  (10) Delay for a while longer.

  (11) If the key pin level is still not the default output level at this time, it means that a key is pressed.

  (12) Delay for a while to allow the voltage level to stabilize.

  (13) Read the current key pin level, that is, the key value.

  (14) Wait for the key to be released, an infinite loop. If the key is pressed, the waiting process continues.

  (15) Return the key value.

  4. Debugging points and experimental phenomena

  After the hardware is connected, download the hex file generated by the program to the MCU through cold start, open the serial port debugging assistant software, set the baud rate to 9600, reset the MCU, and then press any of the 4×4 buttons on the board, and pay attention to the display on the serial port debugging assistant. (See Figure 3), you can observe that the data of the key is displayed in the receiving window.

Figure 3 Pressing the button to display the interface through the serial port debugging assistant

Figure 3 Pressing the button to display the interface through the serial port debugging assistant

  In addition, in the experimental program attached to this article, the send character function and the send string function are called in the serial communication. When there is no simulator and some prompt information needs to be displayed, the serial port printing method can be used, which is not only intuitive and convenient but also does not increase other costs.

  V. Conclusion

  This article introduces the working principle of the single-chip external keyboard and gives an example. Through this article, we can know that a complete keyboard control program should have the following functions:

  (1) Detect whether a key is pressed and take hardware or software measures to eliminate the impact of mechanical contact jitter of keyboard keys.

  (2) There is a reliable logic processing method. Only one key is processed at a time. The operation of any key during this period will not affect the system. No matter how long a key is pressed, the system will only execute the key function program once.

  (3) Accurately output the key value (or key number) to meet the key function requirements. For a matrix keyboard, the row and column line signals must be combined and properly processed to determine the position of the closed key.

  In addition, there are many ways to scan the keys. This article describes the program scanning method. Other common methods include the timing scanning method and the interrupt scanning method. These methods can all be implemented on this learning board. Therefore, readers are expected to combine the knowledge of the previous lectures to write and debug the program by themselves. The next lecture will describe the principle and example of the microcontroller dynamically driving the digital tube, so stay tuned.

Reference address:Learn 51 MCU with me (IV): Independent-matrix keyboard application and design

Previous article:Design of signal generator based on single chip microcomputer and MAX038
Next article:Learn 51 MCU from me (V): MCU dynamic scanning drive digital tube

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号