1. 程式人生 > >電腦鍵盤工作原理

電腦鍵盤工作原理

       所以,當你購買鍵盤的時候,絕對不要忽略“鍵位衝突”的問題,一定要把鍵盤裝到電腦上,試一試自己常玩的遊戲是否能夠正常使用(只有實際試用才能試出鍵位 衝突問題,由於它與鍵位表相關,所以看外表是看不出來的)。同時,一些遊戲高手朋友也不要白費心機去尋找什麼“沒有鍵位衝突的鍵盤”了,只要它依然是非編 碼鍵盤,那麼鍵位衝突就是不可避免的,只要它的鍵位衝突不對你所玩的遊戲構成影響,這就足夠了。

PS/2協議分析

The PS/2 mouse and keyboard implement a bidirectional synchronous serial protocol.
PS/2滑鼠和鍵盤執行一個雙向同步序列協議。

The bus is "idle" when both lines are high (open-collector).
匯流排空閒時,兩條線都是高電平(集電極開路)。

This is the only state where the keyboard/mouse is allowed begin transmitting data.
在這種狀態下,鍵盤/滑鼠才允許開始傳輸資料。

The host has ultimate control over the bus and may inhibit communication at any time by pulling the Clock line low.
主機對匯流排有最高的控制權,在任何時候通過將時鐘線拉低就可以禁止通訊。

The device always generates the clock signal.
時鐘訊號總是由裝置端生成的。

If the host wants to send data, it must first inhibit communication from the device by pulling Clock low.
如果主機想傳送資料,它必須先將時鐘拉低來禁止來自裝置端的通訊。

The host then pulls Data low and releases Clock.
然後主機再拉低資料線,釋放時鐘。 註釋:釋放時鐘,就是再恢復時鐘為高



This is the "Request-to-Send" state and signals the device to start generating clock pulses.
這就是"請求傳送(Request-to-Send)"狀態,提示 裝置端 開始生成時鐘訊號。

Summary: Bus States
Data = high, Clock = high: Idle state.
Data = high, Clock = low: Communication Inhibited.
Data = low, Clock = high: Host Request-to-Send
總結:匯流排狀態
              資料                      0                        1

                          0          ---------通訊禁止-----------
    時鐘
                          1          主機要求傳送          匯流排空閒


The clock and data pins are bidirectional, open-collector
signals that are pulled to 5 V by pullup resistors in the keyboard.
時鐘和資料 引腳時 雙向 集電極開路的訊號,可以被鍵盤內部的上拉電阻 拉高到5V


Data sent from the device to the host is read on the falling edge of the clock signal; data sent from the host to the device is read on the rising edge.
從裝置傳送給主機的資料時在時鐘訊號的下降沿讀取的;從主機發給裝置的資料是在上升沿讀取的。


The clock frequency must be in the range 10 - 16.7 kHz. This means clock must be high for 30 - 50 microseconds and low for 30 - 50 microseconds..
時鐘頻率必須在10-16.7KHz之間。這意味著時鐘必須是 高電平持續30~50毫秒,低電平持續
30~50毫秒。


If you're designing a keyboard, mouse, or host emulator, you should modify/sample the Data line in the middle of each cell. I.e. 15 - 25 microseconds after the appropriate clock transition.
如果你設計一個鍵盤 滑鼠 或者 主機模擬器,你必須 在每個單元的中間時刻 (也就是,在時鐘跳變之後的15~25毫秒後) 修改/取樣資料線.


Again, the keyboard/mouse always generates the clock signal, but the host always has ultimate control over communication.
重複一遍,鍵盤/滑鼠 總是 生成時鐘訊號, 而 主機 控制著整個通訊過程。


Timing is absolutely crucial. Every time quantity I give in this article must be followed exactly.
時序是非常重要的。在本文中給出的時間數必須嚴格遵循。


裝置傳送資料到主機


The Data and Clock lines are both open collector.
資料和時鐘線都是集電極開路的。

A resistor is connected between each line and +5V, so the idle state of the bus is high.
在+5V 和每根線 之間連線著一個電阻,所以 匯流排的空閒狀態 是 高電平。


When the keyboard or mouse wants to send information, it first checks the Clock line to make sure it's at a high logic level.
當鍵盤或者滑鼠想傳送資料時,它首先必須檢查時鐘線 ,確認它處於高電平。

If it's not, the host is inhibiting communication and the device must buffer any to-be-sent data until the host releases Clock.
如果不是,主機禁止通訊,裝置必須緩衝任何要傳送的資料,直到主機釋放時鐘。


The Clock line must be continuously high for at least 50 microseconds before the device can begin to transmit its data.
在裝置開始傳輸資料之前,時鐘線 必須持續為 高電平的 時間 必須 至少50ms


The keyboard/mouse writes a bit on the Data line when Clock is high, and it is read by the host when Clock is low.
當時鍾為高電平時,鍵盤/滑鼠寫一個bit到資料線上;當時鍾為低電平時,主機從資料線上讀取這個bit 。



The Data line changes state when Clock is high and that data is valid when Clock is low.

當時鍾位高時,資料線 改變狀態;
當時鍾位低時,(資料線上的)資料是有效的。




The clock frequency is 10-16.7 kHz.
時鐘頻率是10-16.7KHz

The time from the rising edge of a clock pulse to a Data transition must be at least 5 microseconds.
從時鐘脈衝的上升沿到資料跳變 的時間必須至少 5ms

The time from a data transition to the falling edge of a clock pulse must be at least 5 microseconds and no greater than 25 microseconds.
從資料跳變 到時鐘脈衝的下降沿 必須 至少5ms,且不超過25ms

The host may inhibit communication at any time by pulling the Clock line low for at least 100 microseconds.

主機可在任何時間禁止通訊,只需要將時鐘線下拉位低電平超過100ms即可


If a transmission is inhibited before the 11th clock pulse, the device must abort the current transmission and prepare to retransmit the current "chunk" of data when host releases Clock.
如果在第11個脈衝時禁止傳輸,裝置必須中止當前的傳輸,準備重新傳輸當前的資料"chunk(塊)"當主機釋放時鐘時


A "chunk" of data could be a make code, break code, device ID, mouse movement packet, etc.
一個數據塊可能時 通碼,斷碼,裝置ID,滑鼠移動包 等等。

For example, if a keyboard is interrupted while sending the second byte of a two-byte break code, it will need to retransmit both bytes of that break code, not just the one that was interrupted.
舉個例子,如果當傳送 一個兩位元組斷碼的 第2個位元組時,鍵盤被中斷,它將需要重新發送
此斷碼的兩個位元組,而不僅僅時被中斷掉的那個位元組。


If the host pulls clock low before the first high-to-low clock transition, or after the falling edge of the last clock pulse, the keyboard/mouse does not need to retransmit any data.
如果在第一個 高->低 時鐘跳變 時,(或者在 最後一個時鐘脈衝的下降沿 之後)主機將時鐘拉低,鍵盤/滑鼠 不必重新傳輸任何資料。


However, if new data is created that needs to be transmitted, it will have to be buffered until the host releases Clock.
但是,如果新產生的資料需要傳輸,它必須將資料緩衝,知道主機釋放時鐘。


Keyboards have a 16-byte buffer for this purpose. If more than 16 bytes worth of keystrokes occur, further keystrokes will be ignored until there's room in the buffer.
鍵盤有一個16位元組的緩衝區。如果有超過16個位元組的擊鍵存在,更多的擊鍵將被忽略。直到
緩衝區有空地。


Mice only store the most current movement packet for transmission.
滑鼠只能緩衝最近的一個要傳輸的(移動)資料包。




時間引數                                             最大值/最小值
T1資料跳變到時鐘的下降沿                  5/25 us
T2時鐘的上升沿 到 資料跳變                5/T4 - 5 us
T3時鐘inactive                                         30-50 us
T4時鐘active                                            30-50 us
T5                                                              >0/50 us



Time to auxiliary device inhibit after clock 11 to ensure the auxiliary device does not start another transmission


The auxiliary device checks the 'clock' line. If the line is inactive, output from the device is not allowed.
輔助裝置(指 鍵盤) 檢查 時鐘線 ,如果時低電平,禁止傳送資料


The auxiliary device checks the 'data' line. If the line is inactive, the controller receives data from the system.
裝置檢查資料線,如果是 低電平,那麼控制器從系統接收資料

The auxiliary device checks the 'clock' line during the transmission at intervals not exceeding 100 microseconds. If the device finds the system holding the 'clock' line inactive, the transmission is terminated. The system can terminate transmission anytime during the first 10 clock cycles.
裝置在傳輸過程中 檢查 時鐘 線 間隔不超過100us。 如果裝置發現 主機系統 將電平拉低,就終止傳輸。 在傳輸過程的前10個時鐘週期的任何時候,主機系統可以中止傳輸。


A final check for terminated transmission is performed at least 5 microseconds after the 10th clock.
終止傳輸的最後一個檢查 要持續至少5us




The system can hold the 'clock' signal inactive to inhibit the next transmission.
系統拉低時鐘線,將禁止下一次傳輸


The system can set the 'data' line inactive if it has a byte to transmit to the device. The 'data' line is set inactive when the start bit (always 0) is placed on the 'data' line.
系統如果有一個位元組要傳輸給裝置,可以拉低資料線。當開始位(總是0)放置到資料線時,資料線被拉低。


The system raises the 'clock' line to allow the next transmission.
系統拉高時鐘將允許下一次傳輸


Host-to-Device主機到裝置的通訊
First of all, the PS/2 device always generates the clock signal.
首先,PS/2裝置總是產生時鐘訊號

If the host wants to send data, it must first put the Clock and Data lines in a "Request-to-send" state as follows:
如果主機想傳送資料,它必須先將時鐘線和資料線設定成"Request-to-send"狀態:
1) Inhibit communication by pulling Clock low for at least 100 microseconds.
拉低時鐘線至少100us來禁止通訊
2) Apply "Request-to-send" by pulling Data low, then release Clock.
拉低資料線,請求 "Request-to-send", 然後釋放時鐘 .


The device should check for this state at intervals not to exceed 10 milliseconds.
裝置應該在不超過10ms(注意,是毫秒)的間隔內就要檢查一次這個狀態。

When the device detects this state, it will begin generating Clock signals and clock in eight data bits and one stop bit.
當裝置檢測到這個狀態,它將開始產生時鐘訊號,


The host changes the Data line only when the Clock line is low, and data is read by the device when Clock is high.
只有當時鍾線 為低的時候,主機才可以改變資料線(也就是將資料寫入到資料線)。資料將在時鐘為高電平的時候 被 裝置讀取。


After the stop bit is received, the device will acknowledge the received byte by bringing the Data line low and generating one last clock pulse.
在收到停止位之後,裝置將通過拉低資料線,生成最後一個時鐘脈衝 來 應答收到的位元組


If the host does not release the Data line after the 11th clock pulse, the device will continue to generate clock pulses until the the Data line is released (the device will then generate an error.)
在第11個時鐘脈衝之後,如果主機並沒有釋放資料線,裝置將繼續產生時鐘脈衝,直到資料線被釋放(然後 裝置將產生一個錯誤)


The host may abort transmission at time before the 11th clock pulse (acknowledge bit) by holding Clock low for at least 100 microseconds.
在第11個脈衝之前(迴應位),主機可以隨時中止傳輸,只要拉低時鐘持續100us即可


時間引數                      最大值/最小值
T7 時鐘低電平                30-50 us
T8 時鐘高電平                30-50 us
T9                                     30-50 us



T9 == Time from inactive to active CLK transition, used to time when the auxiliary device samples DATA



FreeScale(Moto)提供的時序圖
主機到鍵盤的資料傳輸



1.主機 通過拉低 時鐘線 來 啟動一個主機到鍵盤的資料傳輸。大約35us後,主機拉低資料線
。這個事件序列 就是 通知 鍵盤,主機要傳輸一個命令了。在資料訊號的下降沿大約 125us 後 時鐘訊號釋放,就被鍵盤內部的上拉電阻拉為高電平 。

2. 在時鐘訊號的上升沿 約 1ms ,資料傳輸開始。這個期間,資料線保持低電平。鍵盤拉低時鐘線,clocking in the low data line. 這個就做傳輸的起始位。

3. 鍵盤用時鐘驅動 8個數據位從主機傳輸到自己這邊。 這個時鐘有50%的佔空比(duty cycle).高電平和低電平的時間都在30~50us之間。主機在每個週期的低電平的時候改變資料線上的資料。在每個時鐘的上升沿5~25us之後鍵盤 開始從資料線上取樣,取出主機發來的資料。

4.資料位之後是一個奇偶校驗位(採用奇校驗)

5.最後是停止位

6.如果鍵盤讀到一個高電平的停止位,鍵盤就在 取樣停止位的那個時鐘的下降沿 之後的低電平期間 拉低資料線。這個行為叫做 鍵盤給主機的應答訊號。
鍵盤拉高時鐘之後,也拉高資料線。


7.在收到一個位元組後,鍵盤對接收的資料執行一個奇偶校驗。如果奇偶校驗錯誤,或者接收的資料無法識別為有效的命令,那麼鍵盤請求重新發送(傳送0xFE給主機)




1. 鍵盤傳送資料前,首先必須讓時鐘 和資料線都在鍵盤內部的上拉電阻作用下 拉成高電平。然後鍵盤拉低資料線,5-25us之後,鍵盤拉低時鐘線。在時鐘的下降沿,開始傳輸起始位The falling edge of the clock line clocks in the transfer’s start
bit.


2. 鍵盤用時鐘驅動 8個數據位到主機。 這個時鐘有50%的佔空比(duty cycle).高電平和低電平的時間都在30~50us之間。鍵盤在每個週期的高電平的時候改變資料線上的資料。這個改變出現在時鐘的上升沿的5us後 和 下降沿的5us前。鍵盤的資料在時鐘的下降沿被鎖存進主機。


3.資料位之後是一個奇校驗位

4.最後是停止位。在時鐘的下降沿 0~50us之間 主機拉低時鐘 來鎖存停止位。這就告訴鍵盤主機現在忙,不能在接收 其他的鍵盤傳輸資料。在處理完接收的資料,準備好接收下一個資料之前,主機釋放時鐘。


6.在鍵盤傳輸資料到主機的過程中,主機任何時候都可以中斷這個傳輸,傳送一個命令到鍵盤。主機拉低時鐘和資料線 即可中止傳輸。所以,鍵盤在時鐘為 低電平的時候必須取樣資料線。(無論什麼時候輸出高資料位的時候)。在產生時鐘的上升沿時,必須讀取時鐘。

在任何時候發現這兩個條件不滿足,就拉高時鐘和資料線,放棄這次傳輸,並準備從 主機接收命令。



Moto 香港公司的 Derek Lau

當時鍾 高電平是,傳送給主機的資料被主機讀取。在空閒狀態,時鐘和資料線都被拉為高電平。主機 拉低 時鐘最少100ms(?還是us)開始傳送資料給鍵盤:

1.主機等待,直到沒有外部裝置傳輸被處理

2.主機 拉低時鐘

3.主機拉低資料作為開始位

4 .主機釋放時鐘

5.裝置拉低時鐘

6.主機發送資料

7.裝置釋放時鐘,讀取資料

8.裝置檢查時鐘線,如果時鐘為低,中止傳輸

9.重複 5-7步,傳送 資料1-到資料0,以及 奇校驗位

10. 裝置拉低時鐘

11. 主機釋放資料

12.裝置時鐘時鐘

13.裝置讀取資料線,想得到停止位,如果資料線為低(不是停止位),產生錯誤

14.裝置拉低 時鐘和資料線

15.裝置釋放 時鐘和資料線




從裝置傳送到主機的資料,在時鐘的下降沿被讀取。在傳送資料前,裝置檢查通過檢測時鐘訊號為高來得知 主機是否貯備好:

1.鍵盤(裝置)等待時鐘為高(約50ms?us)

2.如果資料為 低,終止

3.鍵盤 傳送資料

4.鍵盤拉低時鐘為低

5.鍵盤釋放時鐘為高

6.鍵盤 讀取時鐘線,如果時鐘為低,中止傳輸

7.重複 4-6步,傳輸資料0-資料7,奇校驗位和停止位

8.鍵盤釋放時鐘為高