1、PS/2 技术参考 著 Adam Chapweske 译 Roy Show 第 1 页共 1 页 02-1-2发布 第一章 PS/2 鼠标 键盘协议 Introduction: 引言 The PS/2 device interface, used by many modern mice and keyboards, was developed by IBM and originally appeared in the IBM Technical Reference Manual. However, this document has not been printed for many year
2、s and as far as I know, there is currently no official publication of this information. I have not had access to the IBM Technical Reference Manual, so all information on this page comes from my own experiences as well as help from the references listed at the bottom of this page. PS/2 设备接口用于许多现代的鼠标
3、和键盘 它是由 IBM 开发并且最初出现在 IBM 技术参考手册里 但是 当我知道的时候这篇文件就已经很多年没有印刷了 因此关于这个内容现在没有官方的出版物 我无法访问 IBM 的技术参考手册 所以本网页中的所有信息都来自于我自己的经验及本页最下面列出的参考的帮助 译者注 这些参考的条目在本章的结尾处 This document descibes the interface used by the PS/2 mouse, PS/2 keyboard, and AT keyboard. Ill cover the physical and electrical interface, as wel
4、l as the protocol. If you need higher-level information, such as commands, data packet formats, or other information specific to the keyboard or mouse, I have written separate documents for the two devices: 这个文件描述了用于 PS/2 鼠标 PS/2 键盘及 AT 键盘的接口 我将论及物理和电气接口也包括协议如果你需要更高级的信息 诸如命令 数据包的格式或者其他关于键盘鼠标的特别细节 那么
5、我对这两种设备写了独立的文件 The PS/2 (AT) Keyboard Interface The PS/2 Mouse Interface 译者注 这两篇文章已经包含到这篇译文中来了 是第二章和第三章 I also encourage you to check out my homepage for more information related to this topic, including projects, code, and links related to the mouse and keyboard. 我同样鼓励你在我的主页上校对更多与这个话题相关的信息 包括工程 代码和
6、与鼠标键盘有关的链接 The Connector: 连接器 The physical PS/2 port is one of two styles of connectors: The 5-pin DIN or the 6-pin mini-DIN. Both connectors are completely (electrically) similar; the only practical difference between the two is the arrangement of pins. This means the two types of connectors can ea
7、sily be changed with simple hard-wired adaptors. These cost about $6 each or you can make your own by matching the pins on any two connectors. The DIN standard was created by the German Standardization Organization (Deutsches Institut fuer Norm) . Their website is at http:/www.din.de/ (this site is
8、in German, but most of their pages are also available in English.) 物理上的 PS/2 端口是两类连接器中的一种 5 脚的 DIN 或 6 脚的 mini-DIN 这两种连接器 在电气特性上 是十分类似的 实际上两者只有一点不同那就是管脚的排列 这就意味着这两类连接器可以很容易用一种简单的硬件连线的适配器来转换 这种适配器大约每个值 6 美元 或者你可以根据任意两种连接器的对应管脚关系做你自己的适配器 DIN 标准是由德国标准化组织 (Deutsches Institut fuer Norm)建立的 他们的网站在 http:/w
9、ww.din.de/ 这个站点是德文的 但他们的很多网页同样可用于英文 PS/2 技术参考 著 Adam Chapweske 译 Roy Show 第 2 页共 2 页 02-1-2发布 PC keyboards can have either a 6-pin mini-DIN or a 5-pin DIN connector. If your keyboard has a 6-pin mini-DIN and your computer has a 5-pin DIN (or visa versa), the two can be made compatible with the adapt
10、ors described above. Keyboards with the 6-pin mini-DIN are often referred to as “PS/2“ keyboards, while those with the 5-pin DIN are called “AT“ devices (“XT“ keyboards also used the 5-pin DIN, but they are quite old and havent been made for many years.) All modern keyboards built for the PC are eit
11、her PS/2, AT, or USB. This document does not apply to USB devices, which use a completely different interface. PC 键盘可以有 6 脚的 mini-DIN 或 5 脚的 DIN 连接器 如果你的键盘是 6 脚的 mini-DIN 而你的计算机是 5 脚的 DIN 或者相反 这两类连接器可以用上面提到的适配器来兼容 具有 6 脚 mini-DIN 的键盘通常被叫做 PS/2 键盘 而那些有 5 脚 DIN 叫做 AT 设备 XT 键盘也使用 5 脚 DIN 但它们非常古老并且多年前就不
12、生产了 所有现代的为 PC 建造的键盘不是 PS/2,AT 就是 USB 的 这篇文章不适用于 USB 设备 它们使用了一种完全不同的接口 Mice come in a number of shapes and sizes (and interfaces.) The most popular type is probably the PS/2 mouse, with USB mice gaining popularity. Serial mice are also quite popular, but the computer industry is abandoning them in su
13、pport of USB and PS/2 devices. This document applies only to PS/2 mice. If you want to interface a serial mouse, check out Microchips appnote #519, “Implementing a Simple Serial Mouse Controller.“ 鼠标流行着大量的形状和大小 和接口 最流行的类型可能算是 PS/2 鼠标 现在 USB 鼠标渐渐开始流行起来了 串行鼠标同样非常流行 但计算机工业放弃了它们转而支持 USB 和 PS/2 设备 这篇文章仅适
14、用于 PS/2 鼠标 如果你要知道一个串行鼠标的接口 请查验 Microchip 的 519 号应用 实现一个简单的串行鼠标控制器 As a side note, there is one other type of connector you may run into on keyboards. While most keyboard cables are hard-wired to the keyboard, there are some whose cable is not permanently attached and come as a separate component. Th
15、ese cables have a DIN connector on one end (the end that connects to the computer) and a SDL (Sheilded Data Link) connector on the keyboard end. SDL was created by a company called “AMP.“ This connector is somewhat similar to a telephone connector in that it has wires and springs rather than pins, a
16、nd a clip holds it in place. If you need more information on this connector, you might be able to find it on AMPs website at http:/ I have only seen this type of connector on (old) XT keyboards, although there may be AT keyboards that also use the SDL. Dont confuse the SDL connector with the USB con
17、nector-they probably both look similar in my diagram below, but they are actually very different. Keep in mind that the SDL connector has springs and moving parts, while the USB connector does not. 作为边注 还有另外一种类型的连接器你可以在键盘上碰到 虽然多数键盘电缆都是固定连接到键盘的但还有一些键盘电缆不是永久挂接的 而是作为一个单独的部件 这种电缆在一端有一个 DIN 的连接器 这端连接到计算机
18、 而连接到键盘的那一端是一个 SDL 屏蔽的数据连接器 连接器 SDL 是由一个叫 AMP 的公司建立的 这种连接器有点象电话连接器 它有金属丝和弹簧代替管脚 用一个卡子保持它在适当的位置上 如果你需要知道关于这种连接器的更多信息 你可以在 AMP 的网站找到它AMP 的网站在 http:/ 尽管可能 AT 键盘同样可以使用 SDL 但我只在一个很老的 XT 键盘上看见过这种连接器 不要把 SDL 连接器和 USB 连接器混淆起来 这两个可能在我下面画的示意图中看起来有点类似 但是它们实际上是非常不同的 记住 SDL 有弹簧和活动部件 而 USB 连接器没有 The pinouts for e
19、ach connector are shown below: 每种连接器的引脚定义如下所示 PS/2 技术参考 著 Adam Chapweske 译 Roy Show 第 3 页共 3 页 02-1-2发布 Male 公的 Female 母的 5-pin DIN (AT/XT): 5 脚 DIN(AT/XT) 1 - Clock 1 时钟 2 - Data 2 数据 3 - Not Implemented 3 未实现 保留 4 - Ground 4 电源地 (Plug) 插头 (Socket) 插座 5 - +5v 5 电源 +5V Male 公的 Female 母的 6-pin Mini-D
20、IN (PS/2): 6 脚 Mini-DIN(PS/2) 1 - Data 1 数据 2 - Not Implemented 2 未实现 保留 3 - Ground 3 电源地 4 - +5v 4 电源 +5V (Plug) 插头 (Socket) 插座 5 - Clock 5 时钟 6 - Not Implemented 6 未实现 保留 6-pin SDL: 6 脚 SDL A - Not Implemented A 未实现 保留 B - Data B 数据 C - Ground C 电源地 D - Clock D 时钟 E - +5v E 电源 +5V F - Not Implemen
21、ted F 未实现 保留 General Description: 一般性描述 (Note: Throughout this document, I may use the more general term “host“ to refer to the computer-or whatever the keyboard/mouse is connected to- and the term “device“ will refer to the keyboard/mouse.) 注意 遍及这篇文章 我使用了更普通的术语 host 使之计算机或者键盘 /鼠标连接到的单元 而术语 device 是
22、指键盘 /鼠标 译者注 下面的译文中 host 可能被翻译成主机 There are four interesting pins on the connectors just described: Ground, +5v, Data, and Clock. The +5V is supplied by the host (computer) and the keyboard/mouses ground is connected to the hosts electrical ground. Data and Clock are both open collector, which means
23、they are normally held at a high logic level but can easily be pulled down to ground (logic 0.) Any device you connect to a PS/2 mouse, keyboard, or host should have large pull-up resistors on the Clock and Data lines. You apply a “0“ by pulling the line low and you apply a “1“ by letting the line f
24、loat high. Refer to Figure 1 for a general interface to Data and Clock. (Note: if you are going to use a microcontroller such as the PIC, where I/O is bidirectional, you may skip the transistors and buffers and use the same pin for both input and output. With this configuration, a “1“ is asserted by
25、 setting the pin to input and let the resistor pull the line high. A “0“ is then asserted by changing the pin to output and write a “0“ to that pin, which will pull the line to ground.) 在刚才提到连接器上有四个有趣的管脚 电源地 5V 数据和时钟 host 计算机 提供 5V 并且键盘 /鼠标的地连接到 host 的电源地上 数据和时钟都是集电极开路的 这就意味着它们通常保持高电平而且很容易下拉到地 逻辑 0
26、任何你连接到 PS/2 鼠标 键盘或 host 的设备在时钟和数据线上要PS/2 技术参考 著 Adam Chapweske 译 Roy Show 第 4 页共 4 页 02-1-2发布 有一个大的上拉电阻 置 0 就把线拉低 置 1 就让线上浮成高电平 参考图 1 中数据和时钟线的一般接口结构 注意 如果你打算使用象 PIC 这样的微控制器 由于它们的 I/O 管脚是双向的 你可以跳过晶体管和缓冲门 并且通用同一个管脚进行输入和输出 在这种组态情况下 要设置管脚为输入就写入 1 使得电阻上拉线上的电平 要改变管脚为输出就写入 0 到那个管脚 把线路下拉到地 Figure 1: Open-co
27、llector interface to Data and Clock. Data and Clock are read on the microcontrollers port A and B, respectively. Both lines are normally held at +5V, but can be pulled to ground by asserting logic 1 on C and D. As a result, Data equals D, inverted, and Clock equals C, inverted. 图 1 数据线和时钟线的集电极开路接口数据
28、和时钟分别由微控制器的 A 端口和 B端口读入 这两条线通常保持 +5V 但可以往端口 C 和 D 写入 1 来拉到地 结果是数据是 D 的反相 时钟是 C 的反相 The PS/2 mouse and keyboard implement a bidirectional synchronous serial protocol. In other words, Data is sent one bit at a time on the Data line and is read on each time Clock is pulsed. The keyboard/mouse can send
29、data to the host and the host can send data to the device, but the host always has priority over the bus and can inhibit communication from the keyboard/mouse at any time by holding Clock low. PS/2 鼠标和键盘履行一种双向同步串行协议 换句话说 每次数据线上发送一位数据并且每在时钟线上发一个脉冲就被读入 键盘 /鼠标可以发送数据到主机 而主机也可以发送数据到设备 但主机总是在总线上有优先权 它可以在任
30、何时候抑制来自于键盘 /鼠标的通讯 只要把时钟拉低即可 Data sent from the keyboard/mouse to the host is read on the falling edge of the clock signal (when Clock goes from high to low); data sent from the host to the keyboard/mouse is read on the rising edge (when Clock goes from low to high.) Regardless of the direction of co
31、mmunication, the keyboard/mouse always generates the clock signal. If the host wants to send data, it must first tell the device to start generating a clock signal (that process is described in the next section.) The maximum clock frequency is 33 kHz and most devices operate within 10-20kHz. If you
32、want to build a PS/2 device, I would recommend keeping this frequency around 15 kHz. This means Clock should be high for about 40 microseconds and low for 40 microseconds. 从键盘 /鼠标发送到主机的数据在时钟信号的下降沿 当时钟从高变到低的时候 被读取 从主机发送到键盘 /鼠标的数据在上升沿 当时钟从低变到高的时候 被读取 不管通讯的方向怎样 键盘 /鼠标总是产生时钟信号 如果主机要发送数据 它必须首先告诉设备开始产生时钟信
33、号 这个过程在下一章节中被描述 最大的时钟频率是 33kHz 而且大多数设备工作在 10 20kHz 如果你要制作一个 PS/2 设备 我推荐你把频率控制在 15kHz 左右 这就意味着时钟应该是高 40 微秒低 40 微秒 All data is arranged in bytes with each byte sent in a frame consisting of 11-12 bits. These bits are: 所有数据安排在字节中 每个字节为一帧 包含了 11 12 个位 这些位的含义如下 1 start bit. This is always 0. 1 个起始位 总是为 0
34、 8 data bits, least significant bit first. 8 个数据位 低位在前 PS/2 技术参考 著 Adam Chapweske 译 Roy Show 第 5 页共 5 页 02-1-2发布 1 parity bit (odd parity). 1 个校验位 奇校验 1 stop bit. This is always 1. 1 个停止位 总是为 1 1 acknowledge bit (Host-to-device communication only) 1 个应答位 仅在主机对设备的通讯中 The parity bit is set if there is
35、 an even number of 1s in the data bits and reset (0) if there is an odd number of 1s in the data bits. The number of 1s in the data bits plus the parity bit always add up to an odd number (odd parity.) This is used for error detection. 如果数据位中包含偶数个 1 校验位就会置 1 如果数据位中包含奇数个 1 校验位就会置 0 数据位中1 的个数加上校验位总为奇数
36、 这就是奇校验 这是用来错误检测 When the host is sending data to the keyboard/mouse, a handshaking bit is sent from the device to acknowledge the packet was received. This bit is not present when the device sends data to the host. 当主机发送数据给键盘 /鼠标时 设备回送一个握手信号来应答数据包已经收到 这个位不会出现在设备发送数据到主机的过程中 Device-to-Host Communicat
37、ion: 设备到主机的通讯过程 The Data and Clock lines are both open collector (normally held at a high logic level.) When the keyboard or mouse wants to send information, it first checks Clock to make sure its at a high logic level. If its not, the host is inhibiting communication and the device must buffer any
38、to-be-sent data until it regains control of the bus (the keyboard has a 16-byte buffer and the mouses buffer stores only the last packet sent.) If the Clock line is high, the device can begin to transmit its data. 数据和时钟线都是集电极开路结构 正常保持高电平 当键盘或鼠标等待发送数据时 它首先检查时钟以确认它是否是高电平 如果不是 那么是主机抑制了通讯 设备必须缓冲任何要发送的数据
39、直到重新获得总线的控制权 键盘有 16 字节的缓冲区 而鼠标的缓冲区仅存储最后一个要发送的数据包 如果时钟线是高电平 设备就可以开始传送数据 As I mentioned in the previous section, the keyboard and mouse use a serial protocol consisting of 11-bit frames. These bits are: 如我在上一节提及的 键盘和鼠标使用一种每帧包含 11 位的串行协议 这些位含义是 1 start bit. This is always 0. 1 个起始位 总是为 0 8 data bits, l
40、east significant bit first. 8 个数据位 低位在前 1 parity bit (odd parity). 1 个校验位 奇校验 1 stop bit. This is always 1. 1 个停止位 总是为 1 Each bit is read by the host on the falling edge of the clock, as is illustrated in Figures 2 channel B is the Data signal. 图 3 Q 键的扫描码从键盘发送到计算机 通道 A 是时钟信号通道 B 是数据信号 The clock fre
41、quency is 10-16.7kHz. The time from the rising edge of a clock pulse to a Data transition should be at least 5 microseconds. The time from a data transition to the falling edge of a clock pulse should be at least 5 microseconds and no greater than 25 microseconds. This timing is very important-you s
42、hould follow it exactly. The host may pull the line low before the 11th clock pulse (stop bit), causing the device to abort sending the current byte (this is very rare.) After the stop bit is transmitted, the device should wait at least 50 microseconds before sending the next packet. This gives the
43、host time to inhibit transmission while it processes the received byte (the host will usually automatically do this after each packet is received.) The device should wait at least 50 microseconds after the host releases an inhibit before sending any data. 时钟频率为 10 16.7kHz 从时钟脉冲的上升沿到一个数据转变的时间至少要有 5 微
44、秒 数据变化到时钟脉冲的下降沿的时间至少要有 5 微秒并且不大于 25 微秒 这个定时非常重要 你应该严格遵循它主机可以在第 11 个时钟脉冲 停止位 之前把线拉低 导致设备放弃发送当前字节 这是非常罕见的在停止位发送后 设备在发送下个包前至少应该等待 50 毫秒 这将给主机时间当它处理接收到的字节时抑制发送 主机在收到每个包时 通常自动做这个 在主机释放抑制后 设备至少应该在发送任何数据前等 50 毫秒 I would recommend the following process for sending a single byte from an emulated keyboard/mou
45、se to the host: 我推荐下面的过程发送一个单一字节从仿真键盘 /鼠标到主机 1) Wait for Clock = high. 2) Delay 50 microseconds. 3) Clock still = high? No-goto step 1 4) Data = high? No-Abort (and read byte from host) 5) Delay 20 microseconds (=40 microseconds to the time Clock is pulled low in sending the start bit.) 6) Output St
46、art bit (0) After sending each of these bits, test 7) Output 8 data bits Clock to make sure host hasnt pulled it 8) Output Parity bit / low (which would abort this transmission.) 9) Output Stop bit (1) 10) Delay 30 microseconds (=50 microseconds from the time Clock is released in sending the stop bi
47、t) 1) 等待 Clock = high 2) 延时 50 微秒 PS/2 技术参考 著 Adam Chapweske 译 Roy Show 第 7 页共 7 页 02-1-2发布 3) Clock s 仍旧为 high? No到第 1 步 4) Data = high? No放弃 (并且从主机读取字节 ) 5) 延迟 20 毫秒 (=40 微秒 to the time Clock is pulled low in sending the start bit.) 6) 输出起始位 (0) 在发送所有这些位的每一位后 7) 输出 8个数据位 测试时钟确认主机是否把它拉低了 8) 输出校验位 /
48、 这说明主机要放弃这次传送 9) 输出停止位 (1) 10) 延迟 30 毫秒 (=50 微秒 from the time Clock is released in sending the stop bit) The process for sending a single bit should then be as follows: 按如下的过程发送单个位 1) Set/Reset Data 2) Delay 20 microseconds 3) Bring Clock low 4) Delay 40 microseconds 5) Release Clock 6) Delay 20 mic
49、roseconds 1) 设置 /复位数据 2) 延迟 20 微秒 3) 把时钟拉低 4) 延迟 40 微秒 5) 释放时钟 6) 延迟 20 微秒 Here is some sample code written for the PIC16F84 that follows the above algorithms to send a byte to the host. “Delay“ is a self-explanitory macro; “CLOCK“ and “DATA“ are the bits connected to the Clock and Data lines; “TEMP0“, “PARITY“, and “COUNTER“ are all general purpose registers. Note that in the “PS2outBit“ routine, the Data and Clock lines are brought low by setting the appropriate I/O pin