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]
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.
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
- Popular Resources
- Popular amplifiers
- Computer Vision Applications in Autonomous Vehicles: Methods, Challenges, and Future Directions
- Semantic Segmentation for Autonomous Driving: Model Evaluation, Dataset Generation, Viewpoint Comparison, and Real-time Performance
- Investigation of occupancy perception in autonomous driving: An information fusion perspective
- Digilent Vivado library
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- What are forward and flyback power supplies? What are the differences between forward and flyback? How to quickly distinguish them?
- Brief Introduction of Push-Pull Circuit
- Loto practical tips (7) FFT spectrum analysis using an oscilloscope
- SHT31 Review + Unboxing Test
- Clock system of MSP430 microcontroller
- MicroPython hands-on——by eagler8
- msp430F149 AD conversion and 12864 LCD display program
- Software and hardware circuit design of power amplifier switching power supply based on TMS320F28122 DSP
- Regarding the starting problem of an oscillation circuit,
- Who has XC7VX690T-2FFG1930I