This is only a preview of the May 2000 issue of Silicon Chip. You can view 32 of the 96 pages in the full issue, including the advertisments. For full access, purchase the issue for $10.00 or subscribe for access to the latest issues. Items relevant to "Building The Ultra-LD 100W Stereo Amplifier; Pt.2":
Items relevant to "Build A LED Dice":
Items relevant to "Low-Cost AT Keyboard Translator":
Purchase a printed copy of this issue for $10.00. |
By STEVE CARROLL & BOB NICOL
Build this
low-cost AT
keyboard translator
This simple device converts the complex
scan-codes from an AT keyboard to
standard ASCII character and control
codes. It was originally designed for use
with the BASIC Stamp, Counterfeit and
PIC series of microcontrollers but has lots
of other uses as well.
The rapid progress of computer
technology has resulted in many old,
fully-functional IBM AT keyboards
being thrown away in favour of newer,
fancier ones. If you’ve ever wanted
to connect one of those discarded
keyboards to a project which accepts
standard ASCII codes, this AT Keyboard Translator could be just what
you’re looking for.
In operation, the device connects
directly to any 101/104-key AT key-
board with a 5-pin DIN connector and
converts the key scan-codes to standard ASCII “character” and “control”
codes. It then outputs these codes in
standard inverted asynchronous format at 300, 1200, 2400 or 9600 baud.
The baud rate chosen depends on
your “receiver” and is selected using
a single jumper designated HDR2 on
the PC board.
The output data is compatible with
the RS-232 serial port of many devic-
Information On PC Keyboard Standards
Want to find out more about PC keyboard standards? You’ll find lots of
information at these two websites:
(1) http://www.hth.com/filelibrary/txtfiles/keyboard.txt
(2) http://linton.csie.ntu.edu.tw/design-reference/pc/keyboard_FAQ.html
72 Silicon Chip
es, allowing you to send text or control
codes to your application. It’s just the
shot for interfacing with microcontrollers such as the BASIC Stamp,
Counterfeit and PIC series (in fact, the
device was originally designed to do
just that). There really is no easier way
to connect over 60 switches to one
pin of a Stamp1, Counterfeit Stamp1,
Stamp2 or PIC chip (the non-ASCII
keys are not used).
Of course, it’s not just limited for
use with microcontrollers. It can also
interface with other serial devices
such as a serial printer (via a suitable RS232 driver interface) or LCD
drivers. One of the photographs with
this article shows the AT Keyboard
Translator driving a 4-line alphanumeric LCD via an “LCD Serial Backpack” (designed by Scott Edwards
Electronics).
Other possible applications include
use in an ASCII User Terminal, an
RF/Infrared Keyboard Link, a Video
Text Generator and a Moving Message
Display. The accompanying panel
lists 12 possible applications but there
are lots more.
By the way, if your keyboard has a
PS/2 connector (six pins) rather than
the 5-pin DIN type, an adaptor can be
purchased from most electronics sup-
Fig.1: the circuit uses a PIC microcontroller to decode the complex scan-codes from an AT keyboard and convert
them to ASCII character and control codes.
pliers. Alternatively, an “off-board”
PS/2 socket on a short length of cable
can be used to replace the standard
5-pin DIN socket on the PC board.
Power for the keyboard (+5V) is
provided via the keyboard socket – it’s
just a matter of plugging the keyboard
in and applying power (12V DC) to the
adjacent DC power socket.
10kHz to 30kHz.
Fig.1 shows the circuit details of
the AT Keyboard Translator. It’s deceptively simple, with all the “magic”
taking place inside a pre-programmed
PIC series microcontroller (IC2), either a PIC16F84-10 or PIC16F84-20.
These devices have 1KB of “flash”
EEPROM program-memory, 68 bytes
of RAM and 64 bytes of EEPROM
data storage. In this application, we
require only a handful of external
components to make a complete
working circuit.
How it works
The AT keyboard has a fairly
complex two-way communications
protocol that is quite a handful to
decipher. There’s no logical mathematical pattern to the “scan-codes”
sent by the keyboard and certainly
no similarity to ASCII.
These AT scan-codes can involve
up to 13 bytes of data being sent for
a single keypress and release. Just to
make things difficult, even the simple
act of pressing “Caps Lock” does not
automatically light the “Caps Lock”
LED on the keyboard. Instead, the
keyboard sends a “Caps Lock” scancode to the host (normally a PC or,
in this case, the Keyboard Translator
itself), which then sends a “Light
Caps Lock LED” message back to the
keyboard. Finally, as if all that isn’t
complicated enough, an AT keyboard
can operate at anything from about
The PC board should only take about 10 minutes to assemble. It really doesn’t
get much simpler than this!
May 2000 73
Parts List
1 PC board, 51mm x 61mm,
1 10MHz ceramic resonator
(CR1)
1 18-pin DIL IC socket
1 2.1mm DC socket
1 5-pin DIN socket, PC-mount
1 2-way pin-header strip
2 4-way pin-header strips
1 5-way pin-header strip
1 pin header jumper
1 link wire
Semiconductors
1 LM7805 3-terminal regulator
(IC1)
1 PIC16F84-10/20
microcontroller with ATKB
program (IC2)
1 1N4001 silicon diode (D1)
2 1N4148 signal diodes (D2, D3)
Capacitors
1 100µF 25VW PC electrolytic
(C1)
2 0.1µF ceramic (C2-C3)
Resistors (0.25W, 5%)
3 10kΩ (R5-R7)
2 2.2kΩ (R1-R2)
2 1kΩ (R8, R9)
2 220Ω (R3-R4)
Basically, the PIC microcontroller
converts the complex IBM AT scancodes to standard ASCII codes. Much
of the actual decoding function is
achieved by the use of “lookup” tables. Each time a key is pressed, the
keyboard sends the scan-code to pins
17 & 18 of IC2 via resistors R3 & R4.
Pin 17 accepts the clock signal, while
pin 18 accepts the data signal.
The microcontroller separates the
eight data bits and uses this value as
a memory address offset to look up
the appropriate ASCII value. Assum-
ing that the code is a valid ASCII
“character” or “control” code,
the serial data appears on pin 13
and is fed to pin 4 of a 5-way pin
header (HDR3) via resistor R8
(1kΩ). The CTS (clear to send)
line (pin 8) is optional and in
most cases only one pin on the
receiver is needed to ensure clean
communications.
Note that communications between the keyboard and translator
are almost exclusively one way.
The only translator-to-keyboard
commands involve turning the
“Caps Lock” LED on or off as
required.
Optional CTS function
Sometimes typing speeds can
be too fast for the receiving device (Stamp, PIC, etc), so an optional
CTS function has been programmed
into the PIC microcontroller. In this
circuit, the CTS output at pin 8 is
normally tied to 0V by resistor R7.
However, if necessary, it can be pulled
high (+5V) by the receiver, taken low
to receive the next byte, then immediately returned to the high state until
the receiving device is ready again.
A similar method of data flow control is used between the keyboard and
the translator, utilising the keyboard’s
inbuilt buffer to temporarily store key
presses until the microcontroller is
ready for them. Unfortunately, this
buffer has a limited storage capacity
so prolonged bursts of high-speed
typing may cause some characters to
be missed if the receiving device is
too slow.
The baud rate (ie, the rate at which
data is transmitted from pin 13 of IC2)
is set by placing a jumper across one of
four pairs of header pins (HDR2). This
can be set to either 300, 1200, 2400 or
9600 baud (bits per second) and must
be set to match the receiving device.
Specifications
Supply voltage ����������������������7.5-15VDC
Supply current ����������������������<1mA (idle). Note that a typical AT keyboard
current of up to 300mA must be added to this.
Keyboards supported �����������Most IBM-compatible 101/104/105-key AT keyboards with 5-pin DIN connector. A keyboard
with a PS/2 connector can be used via a suitable
adapter.
Output data format ����������������Standard asynchronous (inverted) at 300, 1200,
2400 or 9600 baud (8N1).
74 Silicon Chip
Fig.2: take care to ensure that all semi
conductors and the electrolytic capacitor
go in with the correct polarity. Depend
ing on the keyboard, it may be necessary
to fit a small heatsink to regulator IC1.
For example, a “Stamp1” is limited
to a maximum of 2400 baud, as is the
LCD Serial Backpack, but many other
devices will readily accept speeds up
to the Keyboard Translator’s maximum 9600 baud rate.
The logic levels on pins 6 & 7 of IC2
determine the baud rate. As shown in
Fig.1, one side of the 4-way dual pin
header is commoned and connected
to the +5V rail. When the jumper is
in the 300 baud position, pins 6 &
7 are both pulled low via R5 & R6.
In the 1200 baud position, pin 6 is
pulled high (+5V) and pin 7 is low,
while for 2400 baud pin 6 is low and
pin 7 is high.
Finally, when the jumper is in the
9600 baud position, pins 6 & 7 are
both pulled high via diodes D2 & D3.
Clock signals for IC2 are derived
from an internal oscillator between
pins 15 & 16. Its frequency is set to
10MHz by ceramic resonator CR1.
Power supply
An AT keyboard requires 5V DC
and typically draws a current of
100-300mA. This is provided by a
7805 regulator which also provides
a regulated +5V rail for the rest of
the circuit and to the 5-way “output”
socket. The 78xx series of regulators
can handle in excess of 1A and have
internal current-limiting and thermal-protection circuitry, making them
almost bullet-proof.
Note that earlier keyboards may
have higher power requirements than
later models. For this reason, if you
use an early keyboard, the regulator
may get quite hot. If this happens,
the answer is to fit a small heatsink.
This view shows the AT Keyboard Translator driving a 4-line alphanumeric
LCD (via the Scott Edwards LCD Serial Backpack). Note that the LCD Serial
Backpack writes lines 1 & 3 first, then lines 2 & 4.
The unit itself runs from 7.5-12V
DC and this can come from a DC
plugpack supply. The power can be
applied via an on-board 2.1mm DC
socket or to a nearby 2-pin header
(HDR1). The centre pin on the DC
input socket is positive, while the
body contact has negative polarity.
Building it
The PC board is very easy to assemble and should cause no problems if
the overlay illustration is carefully
followed – see Fig.2. As with most
boards, it’s a good idea to begin with
the smallest parts and work up to the
larger ones.
Start by installing the wire link
(this goes between the keyboard and
DC power sockets), then install the
resistors, diodes and capacitors. Take
care also to ensure that the electrolytic
capacitor and the diodes are mounted
with the correct polarity. Note particularly that D1 is a 1N4004 power
diode, while D2 & D3 are 1N4148
small signal diodes.
The pin headers can now be installed, followed by the IC socket, the
DC power socket and the keyboard
socket. Make sure that the sockets are
all seated correctly on the PC board
before soldering their pins.
The PIC16F84-10/20 is a static-sensitive device, so normal ESD (electrostatic discharge) precautions should
be employed. This device should not
be installed in its socket until all other
assembly has been completed. Take
care to ensure that it is installed the
right way around. The 7805 3-terminal regulator (IC1) must be installed
with its metal tab towards the centre
of the board (see photo).
Setup & testing
Once the assembly has been com-
pleted, carefully examine the rear of
the PC board for solder bridges between pins, missed solder joints and
vacant holes. You should also check
that all the parts are in their correct
positions and that all polarised parts
are correctly oriented.
If all appears OK, place the jumper
across the appropriate pins to select
the required baud rate and connect the
+5V, DAT (Data), 0V and CTS pins to
the receiver as required. Also ensure
that the receiver is configured for
8,N,1 (ie, 8 data bits, no parity bit, 1
stop bit), inverted polarity. If using the
AT Keyboard Translator with a BASIC
Stamp1, the correct serial modes are
N300, N1200 or N2400. The Stamp (or
Where To Buy The Parts
The Keyboard Translator is available pre-assembled or in kit form from two
companies, as follows:
(1) Control Electronics, 231D Timmsvale Rd, Timmsvale, NSW 2450. Phone
(02) 6654 5458; email ctrl<at>mpx.com.au
(2) Microzed Computers, PO Box 634, Armidale, NSW 2350. Phone (02)
6772 2777; fax (02) 6772 8987; email sales<at>microzed.com.au
Fully assembled and tested PC board (no case)..............................$49.00
Short-form kit (PC board plus all on-board components)..................$39.00
Programmed PIC microcontroller and 10MHz ceramic resonator.....$18.00
The BASIC Stamp, 4-line alphanumeric display and the Scott Edwards LCD
Serial Backpack are available from Microzed Computers. Further information
is available by phone or from www.microzed.com.au
May 2000 75
Suggested Applications For The Keyboard Translator
(1) STAMP “SERIN” COMPATIBLE:
the Keyboard Translator’s output is
directly compatible with the Stamp,
Counterfeit and PICBASIC “SERIN”
function. You can use it at 300, 1200
or 2400 baud for Stamp1 or Counterfeit and also 9600 baud for faster
devices (PIC, Stamp2, etc), making
keyboard input a simple matter for
a wide variety of applications. A
short program listing that enables
the Stamp1 or Counterfeit to receive
data from the Keyboard Translator is
shown in the accompanying panel.
(2) >60 SWITCHES, ONE STAMP
PIN: an AT keyboard and Keyboard
Translator combination is equivalent
to more than 60 switches on one pin
of a Stamp, Counterfeit or PIC, etc
(two pins if CTS used).
red link or RF data transmitter and
receiver, the Keyboard Translator
could be used to remotely send
data to a PC running a QBASIC or
terminal program.
LCD display could allow a Stamp,
Counterfeit, etc to accept ASCII
data from a keyboard. You could
then edit it and print it to a serial
printer.
(5) TV TEXT OVERLAY: a video
text overlay generator could be
designed to display text on a television screen using the Keyboard
Translator and a suitable IC such
as the STV 5730A.
(9) RS-232 ASCII: with the addition
of an RS-232 driver, standard ASCII
character and control codes could
be transmitted via cable to many
devices and applications (eg, a
serial printer).
(6) MOVING-MESSAGE DISPLAY:
the Keyboard Translator could be
used, along with a Stamp or some
other microcontroller and a suitable
display, to design a moving-message
display without tying up a PC.
(10) HOME AUTOMATION: the
Keyboard Translator could be used
with an LCD display and a Stamp to
control a simple home automation
system.
(3) ASCII USER TERMINAL: ap
plication notes for a Stamp-based
“User Terminal” extend only to a 3
x 4 or 4 x 4 numeric keypad. The
Keyboard Translator, along with an
LCD, can be used to build a far more
versatile terminal, with all ASCII character and control codes available.
(7) MOTOR CONTROL: an LCD
display, a Stamp (or similar) and
the Keyboard Translator could be
used to program a wide variety of
motor-control systems, especially if
an extra memory chip (a serial EEPROM or similar) was used to store
additional data for longer sequences,
etc. Again, this could be done without
a dedicated PC.
(4) RF/INFRARED KEYBOARD
LINK: in conjunction with an infra-
(8) DO-IT-YOURSELF TYPEWRITER: the Keyboard Translator and an
(11) EASY MORSE CODE: a
Stamp and the Keyboard Translator
could be used to send the Morse
code tones for each keypress,
without the user needing to learn
Morse code.
(12) TEXT WRITER/PLOTTER:
using a microcontroller, a serial
EEPROM or other memory IC and
the Keyboard Translator, a writer/
plotter system could be devised
to write text onto objects such as
signs, etc.
Fig.3: if you want to use the Keyboard
Translator to send data to a terminal program, you will need to wire
the connector as shown here. This
tricks the port into thinking that it is
connected to a serial device with full
handshaking.
The LCD Serial Backback is mounted on the back of the alphanumeric display
via a 14-way pin header. It accepts serial data in ASCII format and decodes it
to drive the display.
76 Silicon Chip
similar) can also be powered from the
+5V and 0V pins on the 5-pin output
header.
In our example, we are using the
AT Keyboard Translator to drive the
Stamp/Counterfeit Program Listing
ASCII Control Codes
Following is a short BASIC Stamp1 (or Counterfeit) program to receive
serial data from the AT Keyboard Translator and display it on a PC monitor.
After typing in the listing exactly as it appears below and connecting the
keyboard to the Keyboard Translator, connect the 0V pin on the Stamp to
the 0V pin on the Keyboard Translator, then connect Pin 0 on the Stamp to
the Data pin on the Keyboard Translator.
Next connect Pin 1 on the Stamp to the CTS pin on the Keyboard Translator and ensure that the baud-rate jumper is set to the required bit-rate.
Finally, connect the Stamp’s download cable and press <Alt><R> as usual
to download the program and execute the DEBUG instruction. At this stage,
pressing any ASCII keys on the keyboard should result in the corresponding
characters appearing on the PC monitor.
NUL
Ctrl <at> Null
SO H
Ctrl A Start of heading
S TX
Ctrl B Start of text
ETX
Ctrl C End of text
EO T
Ctrl D End of transmission
EN Q
Ctrl E Enquiry
LF
Ctrl J Line feed
‘KB_READ.BAS
‘BS1 PROGRAM TO RECEIVE SERIAL ASCII DATA FROM THE “AT KEYBOARD
‘TRANSLATOR” AND DISPLAY IT ON THE PC MONITOR VIA “DEBUG”.
‘BAUD-RATES OF 300, 1200 AND 2400 ARE SELECTABLE. (PLACE ‘ AT BEGINNING
‘OF UNUSED LINES)
‘DATA FORMAT IS INVERTED, 8,N,1, (N300, N1200, N2400).
VT
Ctrl K Verti cal tab
‘NB: FALSE CHARACTERS WILL BE DISPLAYED BY THE STAMP1 “DEBUG”
‘FIRMWARE IF CONTROL-CODES ARE SENT.
SYMBOL COMS=0
SYMBOL CTS=1
PAUSE 1000
LOOP:
LOW CTS
‘SERIN COMS,N300,B2
‘SERIN COMS,N1200,B2
SERIN COMS,N2400,B2
HIGH CTS
DEBUG #<at>B2
GOTO LOOP
‘SERIAL COMS ON PIN0
‘CTS ON PIN1
‘WAIT FOR KEYBOARD AND TRANSLATOR
‘TO INITIALISE
‘ENABLE KEYBOARD TRANSLATOR TX
‘N300
‘N1200
‘N2400
‘DISABLE KEYBOARD TRANSLATOR TX
‘DISPLAY ASCII CHARACTER ON PC MONITOR
‘GET NEXT CHARACTER
LCD Serial Backback and this requires
connections from the +5V, 0V and data
(DAT) pins. These points respectively
go to +5V, GND and SER (serial) on the
LCD Serial Backpack. Note that this
device does not write to lines 1-4 of
the LCD in numerical order. Instead it
writes to line 1 first, then line 3, then
line 2 and finally line 4 (ie, the order
is 1, 3, 2, 4).
The keys
As mentioned earlier, the keys that
have no ASCII equivalent are unused.
All ASCII character-codes, both shifted and unshifted, are generated in the
usual way by pressing the appropriate
key/s. The “Caps Lock” function operates as usual and lights the “Caps
Lock” LED when it is enabled. The
alternative characters on the numeric
keypad are all non-ASCII, so these
have been disabled and the keypad
operates in numeric mode only.
Similarly, all the arrow keys are
disabled with the exception of the
“Backspace” key, which sends 08 hex,
the ASCII code for “BS”.
Pressing either “Enter” key will
generate an ASCII “Carriage-Return”
(CR). The “control” codes are accessed
by holding down the “Ctrl” key or
“Ctrl” + “Shift” and the relevant key.
The <Esc>, <Backspace>, <Del>, <Enter> and <Tab> keys also generate the
corresponding ASCII control codes.
You can refer to the accompanying
“ASCII Control Codes” table for a full
ACK
Ctrl F
B EL
Ctrl G Bell
Acknowledge
BS
Ctrl H Backspace
HT
Ctrl I
Horizontal tab
FF
Ctrl L Form feed
CR
Ctrl M Carri age return
SO
Ctrl N Sh wt out
SI
Ctrl O Sh wt in
D LE
Ctrl P Data li nk escape
D C1
Ctrl Q Devi ce control 1
D C2
Ctrl R Devi ce control 2
D C3
Ctrl S Devi ce control 3
D C4
Ctrl T Devi ce control 4
N AK
Ctrl U Negati ve acknowledge
SYN
Ctrl V Synchronous idl e
ETB
Ctrl W End of transmission block
CAN
Ctrl X Cancel
EM
Ctrl Y End of medium
SU B
Ctrl Z Substi tute
ES C
Ctrl [
Escape
FS
Ctrl \
Fil e separator
GS
Ctrl ]
Group separator
RS
Ctrl ^ Record separator
US
Ctrl _ Uni t separator
listing of the control codes as defined
in ANSI X3.4.
Keyboard compatibility
Finally, although the device works
with the vast majority of keyboards,
you will inevitably come across the
odd keyboard that won’t work with the
translator. Typically, if you press the
caps lock key on these keyboards, the
keyboard LED indicator comes on and
the device appears to lock up.
The current answer is to use a
different keyboard, although further
refinements to the ATKB PIC program
to include a simple error handling
routine (and still fit the program in
the available space) may eliminate this
problem further down the track. SC
May 2000 77
|