film
The plastic film keyboard has four layers. One layer of plastic film has raised conductive rubber, one of which is an isolation layer, and the upper and lower layers have contacts. Press the rubber bump through the key to make the upper and lower layers of contacts contact and output the code. This kind of keyboard has no mechanical wear and high reliability, and currently accounts for a considerable proportion in the market, but many JS also regard this relatively low-cost keyboard as a capacitive keyboard. Its biggest characteristics are low price, low noise and low cost.
Conductive rubber keyboard
The contact of conductive rubber keyboard contacts is made of conductive rubber. Its structure is a layer of conductive rubber with bumps, the bumps are conductive, this part is for each key, and the interconnected plane parts are non-conductive. When the key cap is pressed, the lower contact is pressed because the bump is conductive. If it is not timely, the bump will bounce. At present, it is also used more.
Capacitive keyboard
Capacitive keyboard, similar to the principle of capacitive switch, changes the capacitance by pressing the key to change the distance between electrodes, and temporarily forms the condition that allows the impact pulse to pass. As we know, the capacitance of a capacitor is determined by the medium, the distance between the two poles and the area of the two poles. Therefore, when the key cap is pressed, the distance between the two poles changes, resulting in a change in capacitance. The parameters are designed properly, and there is output when the key is pressed, and there is no output when the key is not pressed. The output is shaped and amplified to drive the encoder. Because the capacitor has no contact, there are no problems such as wear and poor contact in the working process, and the durability, sensitivity and stability are good. In order to prevent dust from entering between the electrodes, the capacitive key switch is sealed and assembled. 100000 to 30 million lives. But at present, there are not many real capacitive keyboards on the market, most of them are the first two kinds of keyboards, and the price of a real capacitive keyboard is also relatively high.
wireless keyboard
Of course, the most advanced is the wireless keyboard. As the name implies, there is no direct physical connection between the keyboard and the computer, and the input information is transmitted to a special receiver by infrared rays or radio waves. The connection of the receiver is basically the same as that of the ordinary keyboard, just connect PS/2 or COM port, USB port, etc. And be sure to pay attention to the difference when buying. Wireless keyboards generally have the suffix "RF" after the logo, indicating that they support radio wave transmission. At present, the frequencies of most products are at 900 MHz, 455 MHz and 330MHz. About.
Wireless keyboard needs dry battery power supply, which has strict directivity for infrared wireless keyboard, especially the relationship between horizontal positions is more sensitive. Because the receiving angle of the receiver is limited (the central straight line is 6 meters), the keyboard will fail if it is too close to the receiver, and you can't type keys quickly when the sensitivity is low, otherwise you will definitely miss words. It is much more flexible to use the keyboard of the radio. Considering that the radio propagates radially, in order to avoid the mutual interference of keyboards of the same model (same frequency) working at close range, there are generally more than four channels, and the frequency can be manually switched in case of interference. In order to meet the mobile demand, wireless keyboards are generally small in size and integrate the functions of the mouse. Note that there are two interfaces between the receiver and the host, one is PS2 and the other is COM port. It is enough to connect these two interfaces to the host one by one, but if you don't want to use the mouse on the keyboard, you just need to connect the PS2 port of the receiver to the host, but COM doesn't! The receiver does not need an external power supply, and the No.3 alkaline battery built in the keyboard can be used normally for 3 months.
Development trend of keyboard
As far as the development of the keyboard is concerned, the number of keys of the keyboard is gradually increasing (but not infinite, after all, the area of the keyboard is limited), and it is developing in the direction of multi-function multimedia. Early computers used an 83-key keyboard, and later an 84-key design standard was introduced, which divided the keyboard into three areas, namely, functional area, typing key area and sub-keyboard area responsible for cursor control and editing. Among them, the cursor keys and numeric keys in the function key area are used as dual-function symbol keys, and a "Numlock" key is used to control the switching of these two functions. Although two kinds of keyboards are rare now, the division of the main areas of the keyboard still follows the standards at that time and has not changed so far. It was not until 1986 that IBM introduced the keyboard with 10 1 key that its functions were further expanded. In addition to adding two function keys, F 1 1 and F 12, a group of special cursor control and editing keys are added in the middle of the keyboard, and Microsoft WIN95 is introduced. For example, Fn key, shortcut key, keyboard with mouse and tablet, etc.
Commonly used keyboard interfaces include AT interface, PS/2 interface and USB interface. Now most motherboards provide PS/2 keyboard interface, which is commonly known as "small mouth". Compatible computers, especially older motherboards, often provide AT interface, which is called "big mouth". Fortunately, there are large and small keyboard conversion connectors on the market, which only cost a few dollars, and the compatibility problem of the two interface keyboards is solved in one fell swoop. Some companies have also introduced keyboards with USB interface. According to the latest PC200 1 specification, with the disappearance of ISA bus, all interfaces working through ISA bus will be replaced by USB. USB allows other devices to be connected at the same time, which is equivalent to integrating a HUB. For example, you can connect a mouse, which actually saves the COM or PS/2 port of the motherboard. Some keyboards even integrate the circuit from PS/2 to USB, which is more convenient. At present, the reason that hinders its popularization is the high price. For keyboards with integrated USB HUB, most of these keyboards use USB interface. In order to use more USB devices, it is necessary to add a USB HUB device to expand the number of USB interfaces. However, professional USB hubs are expensive, so people try to integrate USB hubs into keyboards or monitors and succeed. The keyboard integrated with USB HUB often occupies a USB interface to keep the transmission of keyboard signals with the host, and provides 2 to 4 USB interfaces for other devices to connect. Simply put, it is one in and one out, which is much cheaper than a professional USB HUB.
In the single-chip microcomputer system, the commonly used keyboards are all special keyboards. This kind of keyboard is designed and manufactured independently, with high cost, many wires and low reliability. These problems are more prominent in those application systems that need more keyboard keys. Compared with this, PS/2 keyboard widely used in PC system has the characteristics of low price, universal reliability and few wires (only two signal wires), which can meet the requirements of most systems. Therefore, it is a good choice to apply PS/2 keyboard in single chip microcomputer system.
Based on the analysis of PS/2 protocol and the working principle and characteristics of PS/2 keyboard, the hardware connection method and the design and implementation of driver for AT89C5 1 single chip microcomputer supporting PS/2 keyboard are given.
1PS/2 protocol
At present, the PS/2 interface widely used in PC is miniDIN 6-pin connector. Its pins are shown in figure 1.
1-data line; 2— Not used; 3— Power ground (GND);
4-power supply (+5v); 5— Clock (Clock6— Not used.
Figure 1PS/2 Connector PS/2 devices are divided into master devices and slave devices, with female sockets as master devices and male sockets as slave devices. Nowadays, PS/2 keyboard and mouse which are widely used all work in slave mode. The clock and data line of PS/2 interface are both of open collector structure, and a pull resistor must be connected externally. The pull-up resistor is generally set in the main equipment. The data communication between master and slave devices adopts bidirectional synchronous serial transmission, and the clock signal is generated by slave devices.
(1) Communication from the slave device to the master device
When sending data from the slave device to the master device, first check the clock line to confirm whether the clock line is high. If it is high, the slave device can start transmitting data; Otherwise, the slave device must wait for bus control before it can start transmitting data. Each transmission frame consists of 1 1 bit. The transmission timing and the meaning of each bit are shown in Figure 2.
Fig. 2 In the communication from the slave device to the master device, the start bit of each frame of data is always 0, and the data is checked by odd parity, and the stop bit is always 1. When the slave device communicates with the master device, the slave device always changes the state of the data line when the clock line is high, and the master device reads the state of the data line at the falling edge of the clock.
(2) Communication from master device to slave device
When the master device communicates with the slave device, the master device will first set the clock line and data line to the "request to send" state. The specific way is as follows: firstly, pull down the clock line at least 100 μs to suppress communication, then pull down the data line to "request transmission", and finally release the clock line. During this process, the slave device should check this state within a time interval not exceeding 10μ s ... When the device detects this state, it will start to generate a clock signal.
At this time, each frame of data transmission consists of 12 bits, and its timing and the meaning of each bit are shown in Figure 3.
Fig. 3 shows that the communication from the master device to the slave device has one more ACK bit per frame compared with the communication from the slave device to the master device. This is the response bit of the byte received by the slave, which is generated by pulling down the data line. The response bit ACK is always 0. In the communication process from the master device to the slave device, the master device always changes the state of the data line when the clock is low, and the slave device reads the state of the data line at the rising edge of the clock.
Code and Command Set of 2PS/2 Keyboard
Coding of (1) PS/2 keyboard
At present, the PS/2 keyboard used by PC adopts the second set of scanning codes by default. For this scan code set, please refer to reference \[ 1\]. There are two different types of scan codes: generation code and interrupt code. When a key is pressed or continuously pressed, the keyboard will send the password of the key to the host; When a key is released, the keyboard will send the broken code of the key to the host.
According to the different scanning codes of keyboard keys, keys can be divided into the following categories:
The first type of key, the passcode is 1 byte, and the cracking code is 0xF0+ passcode. For example, the password of key A is 0x 1C, and the decryption code is 0xf00x1c..
Class II key, the password is in the form of 2 bytes 0xE0+0xXX, and the cracking code is in the form of 0xE0+0xF0+0xXX. For example, the right ctrl key, its pass code is 0xE0 0x 14, and its crack code is 0xE0 0xF0 0x 14.
The third type has two special keys. The password for printing the screen key is 0xE0 0x 12 0xE0 0x7C, and the cracking code is 0xe00xf00x7c 0xe00xf00x12. The password of the pause key is 0xe10x140x770xe10xf00x140xf00x77, and the break code is empty.
The scan codes of key combinations are sent according to the order in which the keys appear. For example, press the left SHIFT+A key in the following order: 1 press the left SHIFT key, 2 press the a key, 3 release the a key and 4 release the left SHIFT key, then a string of data received on the computer is 0x1c0xf00x100xf65438.
In driver design, different keys are treated differently according to this classification.
(2)PS/2 keyboard command set
The host can set the keyboard or obtain the status of the keyboard by sending commands to the PS/2 keyboard. Every time a byte is sent, the host will get a reply 0xFA from the keyboard (except the "resend" and "echo" commands). The following briefly introduces the commands used by the driver in the keyboard initialization process (see reference \[ 1\] for a detailed keyboard command set):
0xED host sends a parameter byte after this command, indicating the status of num lock, caps lock and scroll lock led on the keyboard;
0xF3 host sends a byte parameter after this command to define the typing speed and delay of keyboard machine;
After the host sends 0xF5 to disable the keyboard, 0xF4 is used to re-enable the keyboard.
Connecting Circuit between 3PS/2 Keyboard and Single Chip Microcomputer
The connection between PS/2 keyboard and AT89C5 1 MCU is shown in Figure 4. P 1.0 is connected to PS/2 data line, and P3.2(INT0) is connected to PS/2 clock line. Because the P 1 and P3 ports of single chip microcomputer have built-in pull-up resistors, the clock line and data line of PS/2 can be directly connected with P 1 and P3 of single chip microcomputer.
4 driver design
The driver uses Keil C5 1 language and Keil uVision2 programming environment. The main task of PS/2 104 keyboard driver is to realize PS/2 communication between single chip microcomputer and keyboard, and convert the received key scan code into key value KeyVal, which is provided to the upper software of the system.
(1) Program Design of Communication between Single Chip Microcomputer and Keyboard PS/2
In the process of PS/2 communication, the main device (single chip microcomputer) sends and receives data signals when the clock signal is low. Because the single chip microcomputer sends instructions to the keyboard, it needs the keyboard to respond, so this part of the program adopts query mode; When the microcontroller receives keyboard data, the signal on the data line is stable when the clock is low, so this part of the program adopts interrupt mode, and there is no need to add delay program to the program. The keyboard sending interface program of single chip microcomputer can be found on our website.
(2) Design of keyboard scanning code conversion program.
Because the keyboard scanning code has no rules to follow, the key value of the corresponding key (the character key is its ASCII value, and the control keys such as F 1 and CTRL are user-defined values) can only be obtained by looking up the table. Because three types of keys and some keys correspond to two key values (for example, the key value of key A is 0x4 1(A) and 0x6 1(a) according to the CAPS and SHIFT key states respectively), considering the conversion speed and resource consumption, four keyboard tables are used in the design: keyboard scanning code conversion basic set and switching set KB _ plain _ map \ [NR _ keyboard scanning code conversion basic set and switching set kbe0_plain_map\[NR_KEYS\] and kbe0_shiftPS/2 104 The maximum value of keyboard scanning code is 0x83, so NR_KEYS is set to 132. All four keyboard tables are defined as follows: KB_MAP\[MAKE CODE\]=KEYVAL. If the key value corresponding to the scan code is empty, such as KB_MAP\[0x00\], the corresponding key value is defined as NULL_KEY(0x00). The following are some code examples of the basic keyboard scanning code set: kb _ plain _ map \ [NR _ keys \] = { ...
Null _ key 0x2c 0x6b 0x690x6f0x300x39null _ key//Scan code 0x40~0x47
File:// The corresponding key is empty, comma, k, i, o, 0, 9, empty.
File:// corresponding key value 0x00,',',' k',' i',' o',' 0',' 9', 0x00.
……}; Another advantage of designing the keyboard conversion table in this way in the hardware connection circuit of Figure 4 is that if you need to expand the keyboard supporting ACPI and Windows multimedia keys in the future, you only need to modify the corresponding places in the key table. If the password of ACPI power is 0xE0 0x37, just modify kbe0 _ plain _ map \ [0x37 \] = kb _ acpi _ PWR.
Special key pauses are handled by a separate program. If 0xE 1 is received, it will be transmitted to this program. The print screen key regards it as a combination of two "virtual keys", and the passwords are 0xE0 0x 12 and 0xE0 0x7C respectively.
Declare the following global variables in the driver: LED _ status-scroll lock LED off 0 with bit 0, on1; Bit 1-num lock LED off 0, on1; Bit 2-Caps Lock LED is off 0 and on1; Bits 3 to 7 are always 0; Agcs_status records the left and right shift ctrl gui alt status, bit 0- left shift key, bit 1- left ctrl key, bit 2- left gui key, bit 3- left alt key, bit 4- right shift key, bit 5- right ctrl key, bit 6- right gui key and bit 7- right alt key. When pressed, the corresponding key is 65438. E0_FLAG connection 0xE0 setting1; E 1_FLAG receives 0xE 1 and sets it to1; F0_FLAG receives 0xF0 and sets 1. The key value of key is provided to the upper layer through KeyVal.
The flow of PS/2 keyboard scan code key value conversion program ps2_codetrans () is shown in Figure 5.
Fig. 5 Program flow of key value conversion of scan code: if (F0_FLAG) {// The received scan code is broken.
Switch (mcu_revchar){// processing control key.
Case 0x11:AGC _ status &; = 0xF7 broken; //Left alt release
Case 0x12: AGC _ status &; = 0xFE broken; //Left shift release
Case 0x14: AGC _ status &; = 0xFD broken; //Left ctrl Release
Case 0x58: If (led _ status & amp0x04)
Led _ Status & amp= 0x03///Caps Lock Key
else led _ status = 0x04
PS2 _ led change();
Break;
Case 0x59: AGC _ status &; = 0xEF broken; //Move right to release
Case 0x77: if (led _ status &; 0x02)
Led _ Status & amp= 0x05//num Lock Key
else led _ status = 0x02
PS2 _ led change();
Break;
Case 0x7E: If (led _ status & amp0x0 1)
Led _ Status & amp= 0x06///Scroll lock key
else led _ status = 0x 0 1;
PS2 _ led change();
Break;
Default: break
}
F0 _ FLAG = 0;
}
Otherwise, the received scan code is a password.
If (led _ status & amp0x04) caps _ flag =1; else caps _ flag = 0;
If (led _ status & amp0x02) num _ flag =1; else num _ flag = 0;
if(scga _ status & amp; 0x 1 1)shift _ flag = 1; Otherwise shift _ flag = 0;;
File://scan code key value conversion
if ((caps_flag == shift_flag)(! num _ flag))KeyVal = kb _ plain _ map \[MCU _ rev char \];
else KeyVal = kb _ shift _ map \[MCU _ rev char \];
Switch(mcu_revchar){// Processing control key or status key.
Case 0x11:agcs _ status = 0x08; //left alt key
Case 0x12: agcs _ status = 0x01; //press left shift.
Case 0x14: agcs _ status = 0x02; //press left ctrl.
Case 0x59: AGCS _ status = 0x10; //press the right shift.
Default: break
}
} The conversion process of the scan code key value of the second type of keys is similar to that on. When exiting this program segment, you should pay attention to clearing E0_FLAG and F0_FLAG.
Handler of PAUSE key: If 0xE 1 is received, set E 1 _ flag = 1, and then compare the 7 bytes of data received subsequently with the 7 bytes after the password of PAUSE. If they are consistent, return KeyVal=KB_PAUSE. After comparing all 7 bytes, clear the E 1_FLAG flag.
Keyboard Initializer kb_init () process:
① After power-on, receive keyboard power-on self-test pass signal 0xAA or self-test error signal 0xFC. The MCU receives 0xAA and enters the next step; Otherwise, it performs error handling.
(2) Turn off the LED indicator, the microcontroller sends 0xED, then receives the keyboard response 0xFA, and then sends 0x00 to receive 0xFA.
(3) Set the machine delay and speed. The microcontroller sends 0xF3, receives 0xFA, sends 0x00(250ms, 2.0cps) and receives 0xFA.
④ Check LEDs, send 0xED, receive 0xFA, send 0x07 (turn on all LEDs), and receive 0xFA. Send 0xED, receive 0xFA, send 0x00 (turn off LED), receive 0xFA.
⑤ Allow the keyboard to send 0xF4 and receive 0xFA.
Keyboard LED change ps2_ledchange () function flow: send 0xED→ receive 0xFA→ send led_status→ receive 0xFA.
label
The driver is written by Keil uVision2 and runs on AT89C5 1 single chip microcomputer, which supports PS/2 104 keyboard, character key case switching, num lock switching, control key and combination key. This program can also be used for reference in the application of PS/2 keyboard in other embedded or single chip microcomputer systems.
refer to
1 adamcha Puschi. ATPS/2 keyboard interface.
2Adam Chapweske。 PS/2 mouse/keyboard protocol.
Network technology company. PS/2 keyboard & mouse protocol.
4 Linux 2.4. 10 kernel program defkeymap.cdn _ keyb.ckbd.ckybdev.ckboard.ckbd _ kern.hkd.hkeboard.h.
The first bit of PS/2 frame is the start bit, which is 0, then the 8-bit data bit, and one byte of keyboard scan code (scan code is 1-4 bytes) is sent, then the parity bit, and finally the stop bit 1. These signals are sent through the data line (pin 1). When there is no key, both the data line and the always line remain at 1. Press a key, the CLOCK line sends out a pulse, and the data line sends out data. The host (here, 89c5 1 MCU) samples the data line at the falling edge of the always pulse to obtain data. Keyboard scanning codes include pass code and break code, which are sent when the key is pressed and break code is sent when the key is lifted. For more details, please refer to the attached PS/2 technical reference.
According to the above principle, we can connect the pulse line of the keyboard to the external interrupt input port (INT0 or INT 1) of 89c5 1. When the key is pressed and lifted, a pulse will be generated, which will cause the MCU to interrupt. Connect the data cable of the keyboard to the input port of 89c5 1 (such as P 1.0). In the interrupt handler, data is read from the input port, and then the read data bits are processed by cyclic shift. 1 (start bit), 10 (parity check) and 1 1 (stop bit) can be discarded, and parity bits can be applied if it is not troublesome. When a data frame is received, the remaining 2-9 bits (that is, scan code) are sent to the PC through the serial port, which can be viewed by the PC's serial port monitoring software (such as "serial port debugging assistant"). Hardware connection and source code are as follows:
Source code:
Organization 0000H
AJMP MAIN transfers to the main program
ORG 0003H External Interrupt P3.2 Pin INT0 Entry Address
AJMP INT transfer to external interrupt service subroutine
; Set the CPU interrupt mode of the main program below.
Main business: MOV SCON, # 50H is set to serial port 1 mode.
MOV·TMOD, # 20H baud rate generator T 1 works in mode 2.
MOV PCON, the baud rate of # 80H is doubled to 2400x2=4800BPS.
MOV TH 1, # 0F3H preset initial value (preset initial value according to baud rate of 2400BPS)
MOV TL 1, # 0F3H preset initial value (preset initial value according to baud rate of 2400BPS)
SETB EA; Open CPU total interrupt request
SETB IT0; Set the trigger mode of INT0 to pulse negative edge trigger.
SETB EX0; Open INT0 interrupt request
SJMP element
INT:CLR EA; Temporarily shut down all interrupt requests of CPU.
CJNE R0,#0,L 1
L3: INC R0
SJMP L5
l 1:L2 CJNE R0 # 9
SJMP L3
L2: CJNE R0,# 10,L4
SETB tr 1; Start timer T 1
MOV· Spoff
MOV R0,#0
l5:SETB EA; Allow interrupt
RETI; Exit subroutine
L4: MOV C,P 1.0
RRC A
SJMP L3
end
When the key is pressed and released, the scan code will be displayed on the computer.
When the keyboard is powered on, it will perform a self-test. At this point, the Three Lamps District on the keyboard is on. After the self-check is completed, it will go out and send hexadecimal characters AA. To the host.