📄 at keyboard interfacing.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0074)http://panda.cs.ndsu.nodak.edu/~achapwes/PICmicro/keyboard/atkeyboard.html -->
<HTML><HEAD><TITLE>AT Keyboard Interfacing</TITLE>
<META content="text/html; charset=iso-8859-1" http-equiv=Content-Type>
<META content="MSHTML 5.00.2920.0" name=GENERATOR>
<META content="Adam Chapweske" name=Author><!--This file created 10:20 PM 3/29/01 by Claris Home Page version 3.0--></HEAD>
<BODY aLink=#3333ff bgColor=#ffffff link=#3333ff vLink=#3333ff><X-CLARIS-WINDOW
right="1012" left="0" bottom="603" top="0"><X-CLARIS-TAGVIEW mode="minimal"><!--This file created 8:24 PM 3/26/01 by Claris Home Page version 3.0-->
</X-CLARIS-TAGVIEW></X-CLARIS-WINDOW>
<TABLE bgColor=#faf0e6 border=0 cellPadding=4 cellSpacing=0 width="100%">
<TBODY>
<TR>
<TD>
<BLOCKQUOTE>
<CENTER>
<P><FONT size=+2>The AT-PS/2 Keyboard Interface</FONT> <BR>This article
is Copyright 2001, Adam
Chapweske</P></CENTER></BLOCKQUOTE><B>Introduction:</B>
<P>This article tries to cover every aspect of AT and PS/2
keyboards. It includes information on the low-level signals and
protocol, scan codes, the command set, initialization, compatibility
issues, and other miscellaneous information. Since it's closely
related, I've also included information on the PC keyboard
controller. All code samples involving the keyboard interface are
written in assembly for Microchip's PIC microcontrollers. All code
samples related to the keyboard controller are written in x86 assembly.
</P>
<P>I should mention that all of the information in this article comes from
my own experiences and other sources that may or may not be
accurate. I did not consult any official documentation of since none
has been available to me. Therefore, I provide the following
disclaimer: </P>
<P>ALL INFORMATION WITHIN THIS ARTICLE IS PROVIDED "AS IS" AND WITHOUT ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
I DO NOT GUARANTEE ANY INFORMATION IN THIS ARTICLE IS ACCURATE, AND IT
SHOULD BE USED FOR ABSTRACT EDUCATIONAL PURPOSES ONLY. </P>
<P>You may click <A
href="http://panda.cs.ndsu.nodak.edu/~achapwes/PICmicro/">here</A> to goto
my main page. There, you will find other articles, code, projects,
and links related to the computer keyboard. Please send me an <A
href="mailto:achapwes@panda.cs.ndsu.nodak.edu">email </A>if you find any
errors on these pages. </P>
<P><B>A History Lesson:</B> </P>
<P>The most popular keyboards in use today include: </P>
<UL>
<LI>USB keyboard - Latest keyboard supported by all new computers
(Macintosh and IBM/compatible). These are relatively complicated
to interface and are not covered in this article.
<LI>IBM/compatible keyboards - Also known as "AT keyboards" or "PS/2
keyboards", all modern PCs support this device. They're the
easiest to interface, and are the subject of this article.
<LI>ADB keyboards - Connect to the Apple Desktop Bus of older Macintosh
systems. These are not covered in this article </LI></UL>IBM
introduced a new keyboard with each of its major desktop computer
models. The original IBM PC, and later the IBM XT, used what we call
the "XT keyboard." These are obsolete and differ significantly from
modern keyboards; the XT keyboard is not covered in this article.
Next came the IBM AT system and later the IBM PS/2. They introduced
the keyboards we use today, and are the topic of this article. AT
keyboards and PS/2 keyboards were very similar devices, but the PS/2
device used a smaller connector and supported a few additional
features. Nonetheless, it remained backward compatible with AT
systems and few of the additional features ever caught on (since software
also wanted to remain backward compatible.) Below is a summary of
IBM's three major keyboards.
<P>IBM PC/XT Keyboard (1981): </P>
<UL>
<LI>83 keys
<LI>5-pin DIN connector
<LI>Simple uni-directional serial protocol
<LI>Uses what we now refer to as scan code set 1
<LI>No host-to-keyboard commands </LI></UL>IBM AT Keyboard (1984) - Not
backward compatible with XT systems<TT><A
href="http://panda.cs.ndsu.nodak.edu/~achapwes/PICmicro/keyboard/atkeyboard.html#A%20History%20Lesson%20FN">(1)</A></TT>.
<UL>
<LI>84 -101 keys
<LI>5-pin DIN connector
<LI>Bi-directional serial protocol
<LI>Uses what we now refer to as scan code set 2
<LI>Eight host-to-keyboard commands </LI></UL>IBM PS/2 Keyboard (1987) -
Compatible with AT systems, not compatible with XT systems<TT><A
href="http://panda.cs.ndsu.nodak.edu/~achapwes/PICmicro/keyboard/atkeyboard.html#A%20History%20Lesson%20FN">(1)</A></TT>.
<UL>
<LI>84 - 101 keys
<LI>6-pin mini-DIN connector
<LI>Bi-direction serial protocol
<LI>Offers optional scan code set 3
<LI>17 host-to-keyboard commands </LI></UL>The PS/2 keyboard was
originally an extension of the AT device. It supported a few
additional host-to-keyboard commands and featured a smaller
connector. These were the only differences between the two
devices. However, computer hardware has never been about standards
as much as compatibility. For this reason, any keyboard you buy
today will be <I>compatible</I> with PS/2 and AT systems, but it may not
fully support all the features of the original devices.
<P>Today, "AT keyboard" and "PS/2 keyboard" refers only to their connector
size. Which settings/commands any given keyboard does or does not
support is anyone's guess. For example, the keyboard I'm using right
now has a PS/2-style connector but only fully supports seven commands,
partially supports two, and merely "acknowledges" the rest. In
contrast, my "Test" keyboard has an AT-style connector but supports every
feature/command of the original PS/2 device (plus a few extra.) It's
important you treat modern keyboards as compatible, not standard. If
your project relies on non-general features, it may work on some systems,
but not on others... </P>
<P>Modern AT-PS/2 compatible keyboards </P>
<UL>
<LI>Any number of keys (usually 101 or 104)
<LI>5-pin or 6-pin connector; adaptor usually included
<LI>Bi-directional serial protocol
<LI>Only scan code set 2 guaranteed.
<LI>Acknowledges all commands; may not act on all of them. </LI></UL><A
name="A History Lesson FN"></A><BR><I>Footnote 1) XT keyboards use a
completely different protocol than that used by AT and PS/2 systems,
making it incompatible with the newer PCs. However, there was a
transition period where some keyboard controllers supported both XT and
AT-PS/2 keyboards (through a switch, jumper, or auto-sense.) Also,
some keyboards were made to work on both types of systems (again, through
the use of a switch or auto-sensing.) If you've owned such a PC or
keyboard, don't let it fool you--XT keyboards are NOT compatible with
modern computers.</I>
<P><B>General Description:</B> </P>
<P>Keyboards consist of a large matrix of keys, all of which are monitored
by an on-board processor (called the "keyboard encoder".) The
specific processor<TT><A
href="http://panda.cs.ndsu.nodak.edu/~achapwes/PICmicro/keyboard/atkeyboard.html#General%20Description%20FN">(1)</A></TT>
varies from keyboard-to-keyboard but they all basically do the same
thing: Monitor which key(s) are being pressed/released and send the
appropriate data to the host. This processor takes care of all the
debouncing and buffers any data in its 16-byte buffer, if needed.
Your motherboard contains a "keyboard controller"<TT><A
href="http://panda.cs.ndsu.nodak.edu/~achapwes/PICmicro/keyboard/atkeyboard.html#General%20Description%20FN">(2)</A></TT>
that is in charge of decoding all of the data received from the keyboard
and informing your software of what's going on. All communication
between the host and the keyboard uses an IBM protocol. <BR><A
name="General Description FN"></A><BR><I>Footnote 1) Originally, IBM
used the Intel 8048 microcontroller as its keyboard encoder. There
are now a wide variety of keyboard encoder chips available from many
different manufacturers.</I> </P>
<UL></UL><I>Footnote 2) Originally, IBM used the Intel 8042
microcontroller as its keyboard controller. This has since been
replaces with compatible devices integrated in motherboards' chipsets. The
keyboard controller is covered later in this article.</I>
<P><B>Electrical Interface / Protocol:</B> </P>
<P>The AT and PS/2 keyboards use the same protocol as the PS/2
mouse. Click <A
href="http://panda.cs.ndsu.nodak.edu/~achapwes/PICmicro/PS2/ps2.htm">here</A>
for detailed information about that protocol. </P>
<P><B>Scan Codes:</B> </P>
<P>Your keyboard's processor spends most of its time "scanning", or
monitoring, the matrix of keys. If it finds that any key is being
pressed, released, or held down, the keyboard will send a packet of
information known as a "scan code" to your computer. There are two
different types of scan codes: "make codes"<I> </I>and "break
codes". A make code is sent when a key is pressed or held
down. A break code is sent when a key is released. Every key
is assigned its own unique make code and break code so the host can
determine exactly what happened to which key by looking at a single scan
code. The set of make and break codes for every key comprises a
"scan code set". There are three standard scan code sets, named one,
two, and three. All modern keyboards default to set two.<TT><A
href="http://panda.cs.ndsu.nodak.edu/~achapwes/PICmicro/keyboard/atkeyboard.html#Scancodes%20FN">(1)</A></TT>
</P>
<P>So how do you figure out what the scan codes are for each key?
Unfortunately, there's no simple formula for calculating this. If
you want to know what the make code or break code is for a specific key,
you'll have to look it up in a table. I've composed tables for all
make codes and break codes in all three scan code sets: </P>
<UL>
<LI><A
href="http://panda.cs.ndsu.nodak.edu/~achapwes/PICmicro/keyboard/scancodes1.html">Scan
Code Set 1</A> - Original XT scan code set; supported by some modern
keyboards
<LI><A
href="http://panda.cs.ndsu.nodak.edu/~achapwes/PICmicro/keyboard/scancodes2.html">Scan
Code Set 2</A> - Default scan code set for all modern keyboards
<LI><A
href="http://panda.cs.ndsu.nodak.edu/~achapwes/PICmicro/keyboard/scancodes3.html">Scan
Code Set 3</A> - Optional PS/2 scan code set--rarely used </LI></UL><A
name="Scancodes FN"></A><BR><I>Footnote 1) Originally, the AT keyboard
only supported set two, and the PS/2 keyboard would default to set two but
supported all three. Most modern keyboards behave like the PS/2
device, but I have come across a few that didn't support set one, set
three, or both. Also, if you've ever done any low-level PC
programming, you've probably notice the keyboard controller supplies set
ONE scan codes by default. This is because the keyboard controller
converts all incomming scan codes to set one (this stems from retaining
compatibility with software written for XT systems.) However, it's
still set two scan codes being sent down the keyboard's serial
line. </I>
<P><B>Make Codes, Break Codes, and Typematic Repeat:</B> </P>
<P>Whenever a key is pressed, that key's make code is sent to the
computer. Keep in mind that a make code only represents a <U>key</U>
on a keyboard--it does not represent the character printed on that
key. This means that there is no defined relationship between a make
code and an ASCII code. It's up to the host to translate scan
codes to characters or commands. </P>
<P>Although most set two make codes are only one-byte wide, there are a
handfull of "extended keys" whose make codes are two or four bytes
wide. These make codes can be identified by the fact that their
first byte is E0h. </P>
<P>Just as a make code is sent to the computer whenever a key is pressed,
a break code is sent whenever a key is released. In addition to
every key having its own unique make code, they all have their own unique
break code<TT><A
href="http://panda.cs.ndsu.nodak.edu/~achapwes/PICmicro/keyboard/atkeyboard.html#MakeBreakTypematic%20FN">(1)</A></TT>.
Fortunately, however, you won't always have to use lookup tables to figure
out a key's break code--certain relationships do exist between make codes
and break codes. Most set two break codes are two bytes long where
the first byte is F0h and the second byte is the make code for that
key. Break codes for extended keys are usually three bytes long
where the first two bytes are E0h, F0h, and the last byte is the last byte
of that key's make code. As an example, I have listed below a the
set two make codes and break codes for a few keys: </P>
<BLOCKQUOTE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<TABLE border=1 cols=3 width=300>
<TBODY>
<TR>
<TD>
<CENTER>Key</CENTER></TD>
<TD>
<CENTER>(Set 2) <BR>Make Code</CENTER></TD>
<TD>
<CENTER>(Set 2) <BR>Break Code</CENTER></TD></TR>
<TR>
<TD>
<CENTER>"A"</CENTER></TD>
<TD>
<CENTER>1C</CENTER></TD>
<TD>
<CENTER>F0,1C</CENTER></TD></TR>
<TR>
<TD>
<CENTER>"5"</CENTER></TD>
<TD>
<CENTER>2E</CENTER></TD>
<TD>
<CENTER>F0,2E</CENTER></TD></TR>
<TR>
<TD>
<CENTER>"F10"</CENTER></TD>
<TD>
<CENTER>09</CENTER></TD>
<TD>
<CENTER>F0,09</CENTER></TD></TR>
<TR>
<TD>
<CENTER>Right Arrow</CENTER></TD>
<TD>
<CENTER>E0, 74</CENTER></TD>
<TD>
<CENTER>E0, F0, 74</CENTER></TD></TR>
<TR>
<TD>
<CENTER>Right "Ctrl"</CENTER></TD>
<TD>
<CENTER>E0, 14</CENTER></TD>
<TD>
<CENTER>E0, F0,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -