Design of interface module between microcontroller system and standard PC keyboard

Publisher:码上奇迹Latest update time:2006-08-11 Source: 电子设计应用 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Overview
  : In a single-chip microcomputer system, when there are many input keys, there is always a contradiction between hardware design and software programming. Different microcontroller systems require special keyboard hardware design and programming debugging, which has poor versatility and complicates project development. Standard PC keyboards are quite mature in terms of craftsmanship and technology, and they work stably and are cheap. This design implements an interface module, which converts the position scan code sent by the standard PC keyboard into a standard ASCII code and OEM scan code or Windows virtual key code, and then transmits it to the upper microcontroller in parallel or serial mode.

Features of the interface module:
  This module plays a conversion role between the PC keyboard and the host microcontroller. It shields the complex process of data and command interaction with the PC keyboard, greatly simplifying the input design of the host microcontroller system; it implements a DOS-like operating system The function of the mid-keyboard interrupt service routine allows designers to only care about the results of receiving key presses, and can use standard keyboard coding for programming; it requires the upper microcontroller to be connected to it through an 8-bit parallel interface. For systems that cannot provide a parallel interface, An SPI-compatible synchronous serial interface can be used to connect it. Especially for those applications that want to occupy less system resources of the microcontroller but need to expand a larger number of keys, and the overall instrument needs to be beautiful and elegant, its performance and price ratio are more advantageous. The connection relationship between this module and the microcontroller system is shown in Figure 1. The signal definition of the 20-pin connector connected to the upper microcontroller is also given in the figure.

How a standard PC keyboard works in a computer

      Figure 1 The connection relationship and signal definition between this module and the upper microcontroller system

    Figure 2 Keyboard interface timing (a) keyboard sending timing; (b) keyboard receiving timing

   Figure 3 Principle block diagram of the single-chip computer system and standard PC keyboard interface module

  The keyboard and the host are connected through a keyboard plug. There are two types of keyboard plugs: a 5-pin large plug and a 6-pin small plug (PS/2 interface). The interface signals include: power, ground, keyboard clock KB_CLK, keyboard data KB_DAT. During normal operation, the keyboard circuit continuously scans the keyboard matrix. If a key is pressed, the key position scan code is sent to the mainboard keyboard interface circuit in a serial manner. When a key is pressed, the on scan code is sent, and when the key is released, the key's off scan code is sent. The disconnection scan code generally adds a disconnection flag byte F0H before the connection scan code. If a key is pressed continuously, the key's turn-on scan code will be sent continuously at the key repetition rate. The scan code is related to the position of the key and has no correspondence with the ASCII code of the key. The second column of Table 1 shows the position scan codes of several keys obtained through actual testing. As can be seen from Table 1, depending on the key pressed or released and the key pressed, this sequence can be 1, 2, 3, 4, 6, or 8 bytes, which can be called a position scan code sequence.

  The communication between the standard keyboard and the host is bidirectional and uses an 11-bit serial asynchronous communication format. The 11-bit data includes: start bit 0, 8 data bits (LSB first), odd parity bit P, stop bit 1. Figure 2(a) shows the keyboard sending timing sequence. The data (KB_DAT) changes on the rising edge of the clock (KB_CLK), is valid on the falling edge, and can be read by the host. Figure 2(b) shows the keyboard reception timing sequence. Before the host sends, it first pulls KB_CLK low to inhibit the keyboard from sending, then pulls KB_DAT low to send the start bit, and then releases the KB_CLK line. The keyboard takes over KB_CLK and generates a clock signal. The host sends other bits in synchronization with the KB_CLK signal. Overview of the basic working

principle of the standard PC keyboard interface module

  The schematic block diagram of this module is shown in Figure 3. The PC keyboard is connected to the module through a dedicated socket, the data KB_DAT is connected to the P3.0 pin of AT89C2051, and the clock KB_CLK is connected to the pin. When a key is pressed on the PC keyboard, the KB_CLK signal will cause a continuous interrupt of the AT89C2051. Through the cooperative work of the timer T0 and the external interrupt, the position scan code sequence sent by the PC keyboard can be received into the buffer. Then, in the main program, the position scan code is decoded, converted by table lookup, and then encoded into a one-byte WINDOWS virtual key code or a two-byte OEM scan code and ASCII code, and stored in the FIFO stack in the system. When the upper microcontroller can receive the new key value, the encoded data in the FIFO stack is transmitted to the upper microcontroller in parallel or serial mode.

  In order to more clearly indicate the current working status of the system, indicator lights such as power supply, decoding, FIFO stack overflow, and code value ready are installed on the hardware.

The working principle of interrupt decoding:
  Since the keyboard key input is random, in order to respond in real time, the timer T0 interrupt and interrupt are used to work together in the program to restore the position scan code sequence to the keyboard receiving buffer. The interrupt service routine is used to move one bit of the code value into the buffer. The T0 overflow interrupt service routine is mainly used to determine whether all the codes sent by a key press have been received. The system sets the timing interval of T0 to 5ms, and starts timing after the system starts. Since during the normal process of receiving the code value sequence of each key, the interval between each bit of data sent by the keyboard will not be greater than 5ms. Therefore, in each interrupt service, it is first necessary to determine whether T0 has overflowed. If it has overflowed, it is considered that this time An interrupt is the beginning of a new code value reception. The bit counter needs to be cleared, otherwise it is only necessary to move in one bit of data, then restart the timer and exit the interrupt service routine. After the new key code value sequence is received, the blnDataValid flag is set to notify the main program.

The working principle of the main program.
  The main program has four main tasks: ① Change the position scan code of the keyboard receiving buffer into a unified one-byte Windows virtual key code or a two-byte OEM scan code through table lookup and other algorithms. ASCII code; ② Control the on and off of the three indicator lights on the keyboard according to the status information of the CAPS LOCK key, NUM LOCK key and SCROLL LOCK key in the system; ③ Maintenance of the FIFO stack established in the system; ④ Transmission of code values ​​with the upper microcontroller Handshake interaction process. The flow chart of the main program is shown in Figure 4.

  After the blnDataValid flag is detected in the main program, it means that a new position scan code sequence has been received in the keyboard receiving buffer. The program performs different processing according to the different characteristics of this sequence, and finally obtains the result according to the settings of the hardware jumper. The Windows virtual key code or ASCII code and OEM scan code of the corresponding key. When the jumper in Figure 4 is connected to the "W" position, the code is the Windows virtual key code. Virtual key codes are a set of key encoding constants introduced in Windows systems. Each key has a unique code value corresponding to it. ASCII codes and OEM scan codes are defined in the DOS system, but the key code value definition is still used in the Windows system. Each key has two code values ​​corresponding to it. For function keys, such as F1, HOME, UP, etc. , only the OEM scan code exists, and its ASCII code is 0, see Table 1.

  The FIFO stack is a sending buffer set in the program. It is a 32-byte circular queue established according to the "first in, first out" principle. It has a queue head pointer and a queue tail pointer. When entering the queue, the encoded data enters the unit pointed by the queue tail pointer. At the same time, the queue tail pointer is incremented and points to the next unit. When data continues to enter the queue so that the tail pointer points to the end of the queue, the tail pointer loops back to the beginning of the queue; When dequeuing, the encoded data is taken out from the unit pointed by the queue head pointer. At the same time, the queue head pointer is incremented and points to the next unit. When the head pointer points to the end of the queue, it also needs to be wrapped back to the beginning of the queue, but the head pointer can never be " exceeds the tail pointer. If the key pressing speed is faster than the speed of the upper microcontroller receiving the code value, it is possible that the tail pointer wraps around and becomes equal to the head pointer again. This indicates that the queue is full and no more data can be stored. If another key is pressed at this time, then the stack The overflow indicator light will illuminate.

  Three flags are set up in the system corresponding to the status of the CAPS LOCK key, NUM LOCK key and SCROLL LOCK key. Each time these three keys are pressed, the program must flip the corresponding flag and then send the EDH command to the keyboard. Command the keyboard to activate the three LED indicators on it accordingly.

  Before sending the code value at the head of the FIFO stack to the upper microcontroller, the ACK signal status must be detected to determine whether the upper microcontroller has taken the last code value. If the ACK signal is valid, the code value is latched on the P1 port, and then P3.7 generates an analog clock pulse signal. On the one hand, the 8-bit parallel code value is placed into the serial-to-parallel conversion chip (74LS165), and on the other hand, the The flip-flop (74LS74) is set to 1, causing the terminal to change to 0, providing the host microcontroller with a status signal that the code value is ready (PS_READY#), and lighting the indicator light. In the host microcontroller, this status signal can be queried or used to apply for an interrupt. If the upper microcontroller adopts the parallel interface method, it sends out the read buffer signal (P_RD#) and chip select signal (P_CS#), and then the key value can be obtained through the three-state buffer (74LS244); if the serial interface method is used, it is necessary to Issue the serial clock (S_CLK) and read back the 8-bit code value from the serial data terminal (S_DAT) of the 74LS165. After the upper microcontroller reads the current key value, the ACK signal will be automatically enabled by the handshake logic, and the system can send the next code value by detecting the status of the ACK signal.

Figure 4 Main program flow chart

Conclusion
  Practice has proved that applying this module can not only greatly simplify keyboard input circuit and program design, but also make it more convenient to write programs in high-level languages. This module recognizes all keys on a standard PC keyboard and automatically takes into account the impact of the SHIFT, NUMLOCK and CAPLOCK keys on encoding. This module does not consider key combinations such as CRTL+ key and ALT+ key in DOS systems, but compared to higher-end microcontroller systems, the number of keys currently provided is sufficient.

References
1 Rong Zhen, Liang Hua, Wang Chunsheng. Technology of extending standard microcomputer keyboard with single chip microcomputer. Microcomputer and Application, 2000(7)
2 Yu Yinquan, Yi Xiaodong, Wang Zhaojuan. Interface between single chip microcomputer and PC keyboard. Diesel Locomotive, 2000( 5)

Reference address:Design of interface module between microcontroller system and standard PC keyboard

Previous article:Design of interface module between microcontroller system and standard PC keyboard
Next article:Design of airborne video identification card based on MB90092

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号