Design of interface module between 51 single-chip microcomputer system and standard PC keyboard

Publisher:山宝宝Latest update time:2011-11-07 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. For different single-chip microcomputer systems, special keyboard hardware design and programming debugging are required, which has poor versatility and complicates project development. The standard PC keyboard is quite mature in process and technology, and it works stably and is inexpensive. This design implements an interface module, which converts the position scan code sent by the standard PC keyboard into standard ASCII code and OEM scan code or Windows virtual key code, and then transmits it to the upper single-chip microcomputer in parallel or serial mode.

Features of the interface module
This module plays a role in converting between the PC keyboard and the upper microcontroller. It shields the complex process of data and command interaction with the PC keyboard, greatly simplifying the input design of the upper microcontroller system; it implements a function similar to the keyboard interrupt service program in the DOS operating system, so that designers only need to care about the results of receiving keystrokes and can use standard keyboard coding for programming; it requires the upper microcontroller to connect 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 to it. Especially for those applications that want to occupy less system resources of the microcontroller and need to expand more keys, and the overall instrument needs to be beautiful, its performance-price ratio is more advantageous. The connection relationship between the module and the microcontroller system is shown in Figure 1, and 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 the module and the upper MCU system

Figure 2 Keyboard interface timing (a) Keyboard send timing; (b) Keyboard receive timing

Figure 3 Schematic diagram of the interface module between the single-chip microcomputer system and the standard PC keyboard

The keyboard is connected to the host through a keyboard plug. The keyboard plug has two types: a 5-pin large plug and a 6-pin small plug (PS/2 interface). The interface signals are: 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 position scan code of the key is sent to the keyboard interface circuit of the mainboard in a serial manner. When a key is pressed, a connection scan code is sent, and when the key is released, a disconnection scan code of the key is sent. The disconnection scan code is generally a disconnection flag byte F0H added before the connection scan code. If a key is pressed continuously, the connection scan code of the key is continuously sent at the key repetition rate. The scan code is related to the position of the key and has no corresponding relationship with the ASCII code of the key. The second column of Table 1 gives the position scan codes of several keys obtained through actual testing. As can be seen from Table 1, depending on whether the key is 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 bits of data include: start bit 0, 8 data bits (LSB first), odd parity bit P, and stop bit 1. Figure 2(a) shows the keyboard sending timing. 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 receiving timing. Before the host sends, first pull KB_CLK low to inhibit the keyboard from sending, then pull KB_DAT low to send the start bit, and then release 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.

Working principle of standard PC keyboard interface module
Overview of basic working principle
The block diagram of the 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 cooperation of timer T0 and 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 system FIFO stack. When the upper microcontroller can receive a 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, the hardware is equipped with power, decoding, FIFO stack overflow, code value ready and other indicator lights.

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

Working principle of the main program
The main program has four main tasks: ① Convert 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 and ASCII code through table lookup and other algorithms; ② 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; ③ Maintain the FIFO stack established in the system; ④ Handshake interaction process with the upper microcontroller code value transmission. The flow chart of the main program is shown in Figure 4. [page]

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 Windows virtual key code or ASCII code and OEM scan code of the corresponding key according to the setting of the hardware jumper. When the jumper in Figure 4 is connected to the "W" position, it is encoded as the Windows virtual key code. The virtual key code is a set of key encoding constants introduced in the Windows system, and each key has a unique code value corresponding to it. The ASCII code and OEM scan code are defined in the DOS system, but the key code value definition used in the Windows system, each key has two code values ​​corresponding to it. For function keys, such as F1, HOME, UP, etc., there is only an OEM scan code, and its ASCII code is 0, see Table 1.

The FIFO stack is the sending buffer set in the program. It is a 32-byte circular queue established according to the "first in, first out" principle, with a queue head pointer and a queue tail pointer. When entering the queue, the coded data enters the unit pointed by the queue tail pointer, and the queue tail pointer increments to point to the next unit. When data continues to enter the queue and the tail pointer points to the end of the queue, the tail pointer loops back to the beginning of the queue; when exiting the queue, the coded data is taken from the unit pointed to by the queue head pointer, and the queue head pointer increments to point to the next unit. When the head pointer points to the end of the queue, it also loops back to the beginning of the queue, but the head pointer cannot "exceed" the tail pointer. If the key press speed is faster than the speed at which the upper microcontroller receives the code value, it is possible that the tail pointer will be equal to the head pointer again after looping back, indicating that the queue is full and no more data can be stored. If another key is pressed at this time, the stack overflow indicator will light up.

Three flags are set up in the system, corresponding to the status of the CAPS LOCK key, NUM LOCK key and SCROLL LOCK key. Every time these three keys are pressed, the program must flip the corresponding flags and then send the EDH command to the keyboard, commanding the keyboard to stimulate 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 checked to determine whether the upper microcontroller has taken the previous code value. If the ACK signal is valid, the code value is latched on the P1 port, and then the analog clock pulse signal is generated by P3.7. On the one hand, the 8-bit parallel code value is placed in the serial-to-parallel conversion chip (74LS165), and on the other hand, the trigger (74LS74) is set to 1, so that the terminal becomes 0, providing the upper microcontroller with a status signal of code value ready (PS_READY#), and lighting the indicator light. In the upper microcontroller, this status signal can be queried and interrupts can be requested using this status signal. If the upper microcontroller adopts the parallel interface method, it sends a read buffer signal (P_RD#) and a chip select signal (P_CS#), and the key value can be obtained through the tri-state buffer (74LS244); if the serial interface method is adopted, the serial clock (S_CLK) needs to be sent to 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 set to valid 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 the application of this module can not only greatly simplify the keyboard input circuit and program design, but also make it more convenient to write programs in high-level languages. This module can recognize all the keys on a standard PC keyboard and automatically consider the impact of the SHIFT, NUMLOCK and CAPLOCK keys on the encoding. This module does not consider the combination keys such as CRTL+key and ALT+key in the DOS system, but the number of keys currently provided is sufficient for the upper-level single-chip system.

Reference address:Design of interface module between 51 single-chip microcomputer system and standard PC keyboard

Previous article:Design of intelligent baby carriage control system based on 51 single chip microcomputer
Next article:Design of traffic light control network based on 51 single chip multi-computer communication

Recommended ReadingLatest update time:2024-11-16 21:45

51 MCU and FIFO interface and operation
In the following program, the microcontroller reads the data in the FIFO and sends it out from the serial port. ;************************************     ef bit p3.3 ;fifo empty flag     rst bit p3.5 ;reset fifo     read bit p3.7 ;read fifo     org 0000h     ljmp main     org 0030h main: ;------ initial
[Microcontroller]
Data transmission and storage of acoustic emission signals based on fifo memory
Acoustic emission technology is the product of the combination of optical fiber sensing technology and acoustic emission technology, and is the current development trend of acoustic emission technology. It installs high-sensitivity acoustic emission sensors on the surface of the load-bearing component to form a certain
[Industrial Control]
Data transmission and storage of acoustic emission signals based on fifo memory
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号