📄 interfacing the pc's keyboard.htm
字号:
<TR>
<TD vAlign=top><B>EE</B></TD>
<TD><B>See Echo Command (Host Commands)</B></TD></TR>
<TR>
<TD></TD>
<TD></TD></TR>
<TR>
<TD vAlign=top>FE</TD>
<TD>Resend - Upon receipt of the resend command the Host should
re-transmit the last byte sent. </TD></TR>
<TR>
<TD></TD>
<TD></TD></TR>
<TR>
<TD vAlign=top><B>00</B></TD>
<TD><B>Error or Buffer Overflow</B></TD></TR>
<TR>
<TD></TD>
<TD></TD></TR>
<TR>
<TD vAlign=top>FF</TD>
<TD>Error or Buffer Overflow</TD></TR></TBODY></TABLE></CENTER></UL><BR></UL>
<HR>
<B><FONT size=+2>Scan Codes</FONT></B>
<HR>
<UL>
<P>The diagram below shows the Scan Code assigned to the individual keys. The
Scan code is shown on the bottom of the key. E.g. The Scan Code for ESC is 76.
All the scan codes are shown in Hex. </P>
<CENTER><IMG alt="Scancodes - Keyboard"
src="Interfacing the PC's Keyboard.files/scancode.gif"></CENTER><BR>
<UL>
<P>As you can see, the scan code assignments are quite random. In many cases
the easiest way to convert the scan code to ASCII would be to use a look up
table. Below is the scan codes for the extended keyboard & Numeric
keypad. </P></UL>
<CENTER><IMG alt="Scancodes - Numeric Keypad"
src="Interfacing the PC's Keyboard.files/scancod1.gif"></CENTER><BR></UL>
<HR>
<B><FONT size=+2>The Keyboard's Connector</FONT></B>
<HR>
<UL>
<P>The PC's AT Keyboard is connected to external equipment using four wires.
These wires are shown below for the 5 Pin DIN Male Plug & PS/2 Plug. </P>
<CENTER>
<TABLE width="70%">
<TBODY>
<TR>
<TD>
<CENTER>
<IMG alt="5 Pin Din Numbering (Male Plug)"
src="InterfacingthePC's%20Keyboard.files/5pindin.gif" width="83" height="85"><BR><B>5 Pin
DIN</B><BR></CENTER></TD>
<TD>1. KBD Clock<BR>2. KBD Data<BR>3. N/C<BR>4. GND<BR>5. +5V
(VCC)<BR></TD>
<TD>
<CENTER>
<IMG alt="PS2 Numbering (Male Plug)"
src="InterfacingthePC's%20Keyboard.files/ps2.gif" width="83" height="85"><BR><B>PS/2</B><BR></CENTER></TD>
<TD>1. KBD Clock<BR>2. GND<BR>3. KBD Data<BR>4. N/C<BR>5. +5V
(VCC)<BR>6. N/C <BR></TD></TR></TBODY></TABLE></CENTER>
<P>A fifth wire can sometimes be found. This was once upon a time implemented
as a Keyboard Reset, but today is left disconnected on AT Keyboards. Both the
KBD Clock and KBD Data are Open Collector bi-directional I/O Lines. If
desired, the Host can talk to the keyboard using these lines. </P>
<P><I>Note: Most keyboards are specified to drain a maximum 300mA. This will
need to be considered when powering your devices </I></P></UL>
<HR>
<B><FONT size=+2>The Keyboard's Protocol</FONT></B>
<HR>
<UL><B><FONT size=+2>Keyboard to Host</FONT></B>
<HR>
<P>As mentioned before, the PC's keyboard implements a bi-directional
protocol. The keyboard can send data to the Host and the Host can send data to
the Keyboard. The Host has the ultimate priority over direction. It can at
anytime (although the not recommended) send a command to the keyboard. </P>
<P>The keyboard is free to send data to the host when both the KBD Data and
KBD Clock lines are high (Idle). The KBD Clock line can be used as a Clear to
Send line. If the host takes the KBD Clock line low, the keyboard will buffer
any data until the KBD Clock is released, ie goes high. Should the Host take
the KBD Data line low, then the keyboard will prepare to accept a command from
the host. </P>
<P>The transmission of data in the forward direction, ie Keyboard to Host is
done with a frame of 11 bits. The first bit is a Start Bit (Logic 0) followed
by 8 data bits (LSB First), one Parity Bit (Odd Parity) and a Stop Bit (Logic
1). Each bit should be read on the falling edge of the clock. </P>
<CENTER><IMG alt="Keyboard to Host Protocol Waveform"
src="Interfacing the PC's Keyboard.files/keyboar1.gif"></CENTER>
<P>The above waveform represents a one byte transmission from the Keyboard.
The keyboard may not generally change it's data line on the rising edge of the
clock as shown in the diagram. The data line only has to be valid on the
falling edge of the clock. The Keyboard will generate the clock. The frequency
of the clock signal typically ranges from 20 to 30 Khz. The Least Significant
Bit is always sent first. </P><B><FONT size=+2>Host to Keyboard</FONT></B>
<HR>
<P>The Host to Keyboard Protocol is initiated by taking the KBD data line low.
However to prevent the keyboard from sending data at the same time that you
attempt to send the keyboard data, it is common to take the KBD Clock line low
for more than 60us. This is more than one bit length. Then the KBD data line
is taken low, while the KBD clock line is released. </P>
<P>The keyboard will start generating a clock signal on it's KBD clock line.
This process can take up to 10mS. After the first falling edge has been
detected, you can load the first data bit on the KBD Data line. This bit will
be read into the keyboard on the next falling edge, after which you can place
the next bit of data. This process is repeated for the 8 data bits. After the
data bits come an Odd Parity Bit. </P>
<CENTER><IMG alt="Host to Keyboard - Protocol Waveform"
src="Interfacing the PC's Keyboard.files/keyboard.gif"></CENTER>
<P>Once the Parity Bit has been sent and the KBD Data Line is in a idle (High)
state for the next clock cycle, the keyboard will acknowledge the reception of
the new data. The keyboard does this by taking the KBD Data line low for the
next clock transition. If the KBD Data line is not idle after the 10th bit
(Start, 8 Data bits + Parity), the keyboard will continue to send a KBD Clock
signal until the KBD Data line becomes idle. </P></UL><A name=1></A>
<HR>
<B><FONT size=+2>Interfacing Example - Keyboard to ASCII Decoder</FONT></B>
<HR>
<UL>
<P>Normally in this series of web pages, we connect something to the PC, to
demonstrate the protocols at work. However this poses a problem with the
keyboard. What could be possibly want to send to the computer via the keyboard
interface? </P>
<P>Straight away any devious minds would be going, why not a little box, which
generates passwords!. It could keep sending characters to the computer until
it finds the right sequence. Well I'm not going to encourage what could
possibly be illegal practices. </P>
<P>In fact a reasonably useful example will be given using a 68HC705J1A single
chip microcontroller. We will get it to read the data from the keyboard,
convert the scan codes into ASCII and send it out in RS-232 format at 9600
BPS. However we won't stop here, you will want to see the bi-directional use
of the KBD Clock & Data lines, thus we will use the keyboards status LEDS,
Num Lock, Caps Lock and Scroll Lock. </P>
<P>This can be used for quite a wide range of things. Teamed up with a
reasonably sized 4 line x 40 character LCD panel, you could have yourself a
little portable terminal. Or you could use it with a microcontroller
development system. The 68HC705J1A in a One Time Programmable (OTP) is only a
fraction of the cost of a 74C922 keyboard decoder chip, which only decodes a 4
x 4 matrix keypad to binary. </P>
<P>The keyboard doesn't need to be expensive either. Most people have many old
keyboards floating around the place. If it's an AT Keyboard, then use it (XT
keyboards will not work with this program.) If we ever see the introduction of
USB keyboards, then there could be many redundant AT keyboards just waiting
for you to hook them up. </P><B><FONT size=+2>Features</FONT></B>
<HR>
<P>Before we start with the technical aspects of the project, the salesman in
me wants to tell you about the features packed into the 998 bytes of code.
</P>
<UL>
<LI>Use of the keyboard's bi-directional protocol allowing the status of the
Num Lock, Caps Lock and Scroll Lock to be displayed on the Keyboards LEDs.
<BR><BR>
<LI>External Reset Line activated by ALT-CTRL-DEL. If you are using it with
a Microcontroler development system, you can reset the MCU with the
keyboard. <I>I've always wanted to be able to use the three fingered solute
on the HC11!</I> <BR><BR>
<LI>Scroll Lock and Num Lock toggles two Parallel Port Pins on the HC705.
This can be used to turn things on or off, Select Memory Pages, Operating
Systems etc <BR><BR>
<LI>"ALTDEC" or what I call the Direct Decimal Enter Routine. Just like
using a PC, when you enter a decimal number when holding down one of the ALT
keys the number is sent as binary to the target system. E.g. If you press
and hold down ALT, then type in 255 and release ALT, the value FF (Hex) will
be sent to the system. <I>Note. Unlike the PC, you can use both the numeric
keypad or the numbers along the top of the keyboard. </I><BR><BR>
<LI>"CTRLHEX" or you guessed it, Direct Hexadecimal Enter Routine. This
function is not found with the PC. If you hold CTRL down, you can enter a
Hexadecimal number. Just the thing for Development Systems or even debugging
RS-232 Comms? <BR><BR>
<LI>Output is in ASCII using a RS-232 format at 9600 BPS. If using it with a
development System, you can tap it in after the RS-232 Line Transceivers to
save you a few dollars on RS-232 Level Converters. </LI></UL><BR><B><FONT
size=+2>Schematic & Hardware</FONT></B>
<HR>
<P>The schematic below, shows the general connections of the keyboard to the
HC705. </P>
<CENTER><IMG alt="Schematic for PC Keyboard Decoder"
src="Interfacing the PC's Keyboard.files/keybrd05.gif"></CENTER>
<P>The TXD pin, while it transmits in RS-232 format, is not at RS-232 Voltage
Levels. If you want to connect it to RS-232 Devices then you will need to
attach a RS-232 Level Converter of some kind. If you are using it with a
development system, you can bypass both RS-232 Level Converters and connect it
directly to the RXD pin of the MCU. However the keyboard can't be a direct
replacement for a terminal on a development system, unless you want to type in
your code each time! You may want to place a jumper or switch inline to switch
between your RS-232 Port and the Keyboard. </P>
<P>The Keyboard requires open collector/open drain outputs. This is achieved
by using the Data Direction Register (DDR). A zero is written to the port
which is internally latched. The DDR is then used to toggle the line from
logic 0 to high impedance. If the port pin is an output, a logic zero will be
present on the pin, if the port is set to be an input, the port will be high
impedance which is pulled high by the external resistors. </P>
<P>The circuit is designed to run on a 4Mhz crystal (2Mhz Bus Speed). The
timing for the RS-232 transmission is based on the bus speed, thus this
crystal has to be 4 Mhz. If you are lucky enough to have a 4 Mhz E Clock on
your development system you can use it. </P>
<P>The power supply can also create a slight problem. A standard keyboard can
drain about 300mA max, thus it would be recommended to use it's own regulator
rather than taking a supply from elsewhere. Decoupling capacitors are not
shown on the general schematic but are implied for reliable operation. Consult
your MC68HC705J1A Technical Data Manual for more Information. </P><B><FONT
size=+2>Reading Bytes from the Keyboard.</FONT></B>
<HR>
<P>Now it is time to look at the code. I cannot include a description of all
the code in this article. The list file is just on 19 pages. Most of it
(hopefully) is easy to follow. (Just like other good code, count the number of
spelling errors while you are at it!) </P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -