这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 嵌入式开发 » MCU » keyboard programming(老站转)

共1条 1/1 1 跳转至

keyboard programming(老站转)

菜鸟
2002-07-29 03:06:07     打赏
水木清华 助理工程师 来自: 发表总数:4   查看   短消息   电子邮件   引用   回复   -------------------------------------------------------------------------------- 发信人: haowai (号外), 信区: DriverDev 标 题: keyboard programming 发信站: BBS 水木清华站 (Mon Apr 1 20:17:17 2002) 觉得很好,转贴一下 by Wout Mertens The computer and the AT or MF II interface through I/O ports 60h and 64h, controlled by a programmable Intel 8042 (old ATs), 8741 or 8742 (newer, allow two input devices (like the PS/2 mouse)) microprocessor or compatible, which allows typematic rate programming, LEDs lighting and some other stuff. It also has a +-20 byte output buffer for smooth operation and long scancodes. The old XT keyboard has a 8048, which is in essence just a very primitive one-way serial interface, so all used is port 61h, to disable and reenable the keyboard on every scancode. Port 60h: Input & output ------------------------ Read: Scancodes and keyboarddata -------------------------------- This port gives the following output codes: 00h: Keyboard error, too many keys are being pressed at once aah: Basic Assurance Test (BAT) end abh 41h: The result of requesting keyboard ID on a MF II keyboard eeh: The result of the echo command fah: ACK(noledge). Sent by every command, except eeh and feh fch: BAT failed feh: Resend your data please ffh: Keyboard error All the rest are make (press) and break (release) codes of the keys. Write: Command data ------------------- This is the place where command data has to be sent. If the command consists of two bytes, you must wait until the outputbuffer is sent to the keyboard. Check on it via bit 1 of port 64h. When you send a command, the outputbuffer is cleared, so pending results may not come. During transmission of a two-byte command, the keyboard stops scanning. When you send something out of range or so, the keyboard will react with feh (resend). All commands, except echo (eeh) and resend (feh) result in ACK (fah) to be sent. Commands: edh: Set keyboard LEDs Send a second byte with: bit 0 = Scroll Lock 0=Off 1=On 1 = Num Lock 2 = Caps Lock rest = 0 Do make an effort to keep the BIOS keyboard flags in sync. eeh: Great fun. Send it, and get 0eeh right back! :-] (Diagnostics) f0h: Select scancode set. 0: return current set number: 1:'C', 2:'A', 3:'?' 1: set scancode set no 1 2: set scancode set no 2 -> standard 3: set scancode set no 3 f2h: Identify keyboard XT: nothing (that is, time-out error :-) (see port 64h) AT: ACK MF II: ACK abh 41h f3h: Typematic rate programming Send a second byte with: bit 0 -> 4: rate. Timings: 0: 30 keys/sec 10: 10 1: 26.7 13: 9 2: 24 16: 7.5 4: 20 20: 5 8: 15 31: 2 bit 5 & 6: pause before repeat: 0: 250 ms 1: 500 2: 750 4: 1000 bit 7: Always 0 The next three are doubtfull, since one of my sources say they don't exist and another says they do. I leave it up to you :) f4h: Enable keyboard. It clears its buffer and starts scanning. f5h: Reset keyboard, disable scanning f6h: Reset keyboard, enable scanning feh: Resend last transmission. I really don't know what it does, since it sends something incomprehesible. ffh: Internal diagnostics: Sends aah if successfull. Warning! The keyboard reacts with ACK and then you have to set the data and clock pins high, DURING AT LEAST 500 SECONDS!. Do this via the outputport (see 64h). After that, the BAT (Basic Assurance Test) starts. This sends aah on success and fch on failure. Example: Set the keyboard LEDs start: in al, 64h \It would be good and al, 02h ;Test if command buffer is empty |to put this in a jnz start /macro... mov al, edh out 60h, al ;Write outputport wait: in al, 64h and al, 02h ;Test if command came through jnz wait mov al, 0111b out 60h, al ;Set all LED's to ON. Port 61h -------- This port is used to acknoledge the receival of a scancode, by disabling the keyboard and immediately reenabling it. This also means that you can read a scancode as many times as you like, until you acknoledge the receival. bit 0 -> 5: Nothing to do with keyboard, but with the Programmable Peripheral Interface (PPI) -> save them! bit 6: Hold keyboard clock low -> Keyboard can't send any data. bit 7: 0=Enable keyboard; 1=Disable keyboard Example: in al, 61h mov ah, al ;Save keyboard status or al, 80h ;Disable out 61h, al mov al, ah ;Enable (If it was disabled at first, you wouldn't out 61h, al ; be doing this anyway :-) Port 64h: Interface: data and control ------------------------------------- Read: Statusport ---------------- bit 0: 1: Keyboard data is in buffer 0: Output buffer empty -> use it to check for results 1: 1: User data is in buffer 0: Command buffer is empty -> time to send a command 2: 1: Selftest successful 0: Reset (?) 3: 1: 64h was last accessed port 0: 60h was last accessed port 4: 1: Keyboard enabled 0: Keyboard locked 5: PS/2: Mouse interface 6: 1: Time-out error occurred: Keyboard or PS/2 mouse didn't react. Use the Resend command to retry fetching the data byte. This could happen when trying to get a XT keyboard to do something :). 7: 1: Last transmission had a parity error Write: Control register ----------------------- This is the control room of the keyboard interface. If additional data is required, send it to port 60h after writing the command to 64h. Also, check 61h bit 2 before sending anything. Commands: aah: Keyboard self test. Sends 55h if successfull. abh: Test interface. Sends: 00h: No error 01h: Clock low 02h: Clock high 03h: Data low 04h: Data high ffh: Total Error adh: Deactivate keyboard aeh: Activate keyboard c0h: Read inputport. This is some highly specialized stuff and I wonder why I am typing this. Ok. The inputport is that what the keyboard is sending and some more. Layout: bit 0: Keyboard data in pin 1: PS/2 mouse in pin 2->5: reserved 6: Wether you have a color or mono screen 7: 1: Keyboard not locked 0: Keyboard locked When you issue this command, the inputport is put on the outputbuffer, so you have the great priviledge of reading it at port 60h. c1h: Puts the low nibble of the input port over bits 4-7 of the statusport, so you can read them out continuously. This lasts until bit 2 of the statusport gets set, meaning you are sending data to the keyboard. c2h: Ditto, but it puts the high nibble over bits 0-3 of the statusport. Lifespan is the same. d0h: Puts the outputport on the buffer. Layout: bit 0: 1: Reset processor 1: 1: A20 gate enable 2: PS/2 mouse data out 3: PS/2 mouse clock signal 4: 1: Output buffer full 5: 1: Output buffer PS/2 mouse full 6: Keyboard clock signal 7: Keyboard data out Bit 0 and 1 are quite important for high memory and 286-extended-memory access. d1h: Write the following data byte to the outputport d2h: Write the following data byte to the keyboardbuffer. This is VERY handy for TSRs that need to read codes that start with e0h. This way, they don't have to pass through the e0h, unless they know for sure it isn't their code, which results in correct functioning shift keys etc. At least, if it does what I think it does... [UNTESTED] d3h: Ditto, for PS/2 mouse. d4h: Write byte to PS/2 mouse. e0h: Reads the keyboards testinputs, T0 and T1. T0 goes to bit 0 and T1 to bit 1 of the byte that is put on the outputbuffer. fxh: I think it sends x to the low nibble of the output port. It does reset my computer when I send feh, but that doesn't mean anything :-). The official explanation says that it keeps the corresponding bits in the output port low for 6ms... Example: Send something to the outputport start: in al, 64h \It would be good and al, 02h ;Test if command buffer is empty|to put this in a jnz start /macro... mov al, d1h out 64h, al ;Write outputport wait: jnz wait mov al, 01h out 60h, al



关键词: keyboard     programming     老站转         

共1条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]