ps2键盘检测(PS2 keyboard detection)
ps2键盘检测(PS2 keyboard detection)
PS/2 keyboard key detection
Category: latest news release date: 2011-03-09 hits: 526
In the SCM system, often use the keyboard is a special keyboard, the keyboard are generally separate design, high cost, the use of hardware connection, and the reliability is not high, the situation in the application system that require keyboard more of the more prominent. Compared with this, PS/2 keyboard is widely used in PC system. It has the characteristics of low price, general reliability and less connection line (only using 2 signal lines), and can meet the requirements of many systems. Therefore, the application of PS/2 keyboard is a good choice in the SCM system.
In this paper, PS/2 protocol and principle, read and identify the PS/2 keyboard, the first type of keyboard keys.
1 PS/2 protocol
The PC is now widely used as the PS/2 interface for the mini - DIN 6 pin connector. Its pin is shown in figure 1.
PS/2 equipment has master-slave, main equipment adopts female socket, male socket is adopted in equipment. The widely used PS/2 keyboard and mouse work from the device mode. The clock and data lines of the PS/2 interface are all collector
An open circuit structure must be externally connected with a pull-up resistor. The general pull-up resistor is set in the
main equipment. Data communication between master and slave devices is transmitted in a two-way synchronous serial mode, and the clock signal is generated from the device.
(1) communication from equipment to main equipment
When data is transmitted from the device to the main device, the clock line is first checked to verify that the clock line is high. If the level is high, data can be transmitted from the device; otherwise, the data can only be transmitted until the device has to wait for control of the bus. Each frame of the transmission consists of 11 bits, the timing of transmission and the meaning of each bit, as shown in figure 2.
In each frame, the start bit is always 0, and the data check is in the odd check mode, and the stop bit is always 1. When communicating from the device to the main device, the device always changes the status of the data line when the clock line is high, and the main device reads the data line state at the falling edge of the clock.
(2) communication of master device to slave device
When the master device communicates with the slave device, the host device first sets the clock line and data line to "request send" status. Specifically, first, pull down the clock line for at least 100 us to suppress communication, then pull down the data line "request send", and finally release the clock line. During this process, the device must be checked within an interval of not more than 10us. When the device detects this state, it begins to produce a clock signal.
At this point, each frame of the data transmission is made up of 12 bits, each of which has its timing and meaning as shown in figure 3.
Compared to the communication from device to host, each frame has one ACK bit of data. This is the acknowledge bit of the received byte from the device
The device generates data by pulling low data lines, and the response bit ACK is always.. In the process of communication between the main device and the slave device, the main device always changes the status of the data line at the time of the clock low, and reads the data line state from the rising edge of the device.
/*-----------------------------------------------
Name: PS/2 keyboard driver
Sirius Electronics
Website: www.tlxmcu.com
Writing: Lin Feng
Date: 2009.5
Modification: None
Content: PS/2 keyboard identification of the first type of
button, with digital display
------------------------------------------------*/
#include
#define uchar unsigned char
#define uint unsigned int
Sbit psdata=P3^0;
Sbit psclk=P3^2;
Uchar numbit;
Uchar keyv;
Uchar key[3];
Uchar keynum;
Uchar, m, n;
Unsigned, char, code, smg_du[]={0x3f, 0x06,0x5b, 0x4f, 0x66,0x6d, 0x7d, 0x07,0x7f, 0x79,0x71,0x00}, 0x6f, 0x77,0x7c, 0x39,0x5e;
Unsigned char code smg_we[]={0x08,0x18,0x28,0x38,0x48,
0x58,0x68,0x78};
* * * * / / * * * * * * * * * * * * * * * * * * * *
/ / 延时函数, 在12mhz的晶振频率下
/ / 大约50us的延时
* * * * / / * * * * * * * * * * * * * * * * * * * *
Void delay _ 50US (uint t)
{
Uchar J;
For (; T > 0; I --)
For (J = 19; J > 0; J -);
}
* * * * / / * * * * * * * * * * * * * * * * * * * *
/ / 延时函数, 在12mhz的晶振频率下
/ / 大约50ms的延时
* * * * / / * * * * * * * * * * * * * * * * * * * *
Void delay _ 50ms (uint t)
{
Uint J;
For (; T > 0; I --)
For (J = 6245; J > 0; J -);
}
Void main ()
{
It0 = 1; / / 外部中断下降沿触发
EX0 = 1; / / 打开外部中断0
Ea = 1; / / 打开总中断
While (1)
{
If (keyNum = = 4)
{
Ea = 0;
KeyNum = 0; / / 0x1c
N = key [1] / 16;
M = key [1] 16%;
Ea = 1;
}
P1 = SMG _ du [m];
We _ SMG P2 = [0];
Delay _ 50US (20);
P1 = SMG _ du [n];
We _ SMG P2 = [1];
Delay _ 50US (20);
}
}
Void interrupt int0 (0)
{
If ((numbit > 0) & & (numbit < 9))
{
Keyv keyv > > = 1;
If (psdata)
Keyv = keyv | 0x80;
}
Numbit + +;
If (numbit > 10)
{
Numbit = 0;
[key] = keyv keyNum;
KeyNum + +;
}
}