QEX – May/June 2010
5
Reprinted with permission © ARRL
I assembled a six pin mini-DIN connector
breakout board from parts obtained from
SparkFun Electronics, a wonderful resource
for digital electronics hobbyists.
11, 12
Sample
PS/2 protocol data came from an original
IBM AT computer PS/2 keyboard and a
Logitech PS/2 three button mouse. I pur-
chased an inexpensive logic analyzer from
Saleae.
13
Since the PS/2 data bits are sent in
“reverse” order — least significant bit first
— I created a paper form to record the start
bit, eight data bits, parity bit, stop bit and any
acknowledgment bit. Later in the project, I
purchased a USBee SX logic analyzer, since
it can decode the PS/2 protocol for the host
and the device.
14
PIC18F4520 Prototyping Boards
I needed to choose a PIC microcontroller
for my project. Rick Hambly, W2GPS, devel-
ops and sells time related products using PIC
microcontrollers and he recommended that
I use the 18F series PIC microcontrollers. I
purchased the PIC18F4520 development kit
from CCS: it includes a prototyping board,
and most valuably, an exercise booklet to
help get started.
15
Note that the PIC18F4520
prototyping board and exercise booklet can
be purchased separately from the complete
kit. Other prototyping and demonstration
boards I have tried that use the PIC18F4520
are the PICDEM 2 Plus demonstration board
from Microchip, the Dem2PLUS demonstra-
tion board from Sure Electronics, and the
Olimex 40 pin bare prototyping board with
RS-232 from SparkFun.
16, 17, 18
A USB ver-
sion of the Olimex 40 pin bare prototyping
board is also available.
19
I assembled a prototyping board as shown
in Figure 2 by using a breadboard purchased
from Beginner Electronics, a PIC18F4520
microcontroller in the PDIP form factor,
two six pin mini-DIN connectors, an ICD
programming connector and a serial TTL
to RS-232 adapter.
20, 21, 22, 23
I connected the
5 V power supply to the prototyping board
with a positive temperature coefficient (PTC)
resettable fuse device, available from Spark
Fun, at both mini-DIN connectors and at
the ICD programming connector.
24
These
devices protect the prototyping board and
power supply sources against an accidental
short circuit. The two mini-DIN connectors
are connected to a personal computer using
a PS/2 keyboard-and-mouse-to-USB adapter
and two PS/2 male-to-male cables.
25, 26
The
ICD connector is connected to a PIC pro-
grammer with a short cable. As luck would
have it, the receive and transmit pins of the
RS-232 adapter match the corresponding
input/output pins on the PIC18F4520. Note
the probes connected to the logic analyzer
in Figure 2.
The circuit schematic diagram shown in
Figure 3 consists of essentially one compo-
nent — the PIC18F4520 microcontroller.
The PS/2 keyboard connector, the PS/2
mouse connector and the CW keyer paddle
is connected to port B pins of the PIC to
take advantage of the internal pull-up resis-
tors provided within the PIC. No external
clock crystal or resonator is needed since the
internal 8 MHz clock within the PIC is used
instead.
PIC Development Tools
For the PIC programmer, I used the
Microchip PICkit™ 2 programmer/debug-
ger.
27
The PICkit 2 is inexpensive and was
adequate for this project. It has a convenient
power management feature: it provides
power to the development board if it detects
that the board has no power. Also, power to
the board can be turned on and off from a
software menu item.
After evaluating free demonstration ver-
sions of several PIC C compilers, I chose the
CCS C compiler since it appears to best hide
hardware details of PIC microcontrollers
with library functions.
28
The port input/out-
put library functions are easy to use and inter-
rupt service routines are easy to write. Also,
Rick Hambly, W2GPS, recommended the
CCS C compiler since he uses it extensively
for his work.
For the actual development environment,
I preferred to use the Microchip MPLAB
®
integrated development environment (IDE)
instead of the one provided by CCS with
their C compiler suite.
29
Note that the less
expensive CCS PCH C compiler instead of
the full CCS C compiler suite is sufficient
for this project since PCH integrates with the
MPLAB IDE.
30
Learning PIC
C
Programming
My first goal was to repeatedly blink an
LED on and off and to display “hello, world”
on a HyperTerminal serial terminal window.
I read several introductory books on PIC C
programming to get me started.
31, 32, 33
There
are several other good books to read, as
well.
34, 35, 36
Nuts & Volts and Circuit Cellar
magazines also have good articles on PIC
microcontroller development.
37, 38
For program testing and debugging, I
learned to use LEDs and timing pulses on
an output pin to observe with a logic ana-
lyzer. I used the
printf()
function to print
configuration and debugging information
during program development and testing. I
also quickly learned not to introduce a timing
error in the code by putting a
printf()
in
the wrong place.
Writing the Program
This program is organized into three
software modules. The first module decodes
CW characters keyed in with the paddle. The
invalid CW character di di dah dah is used
to indicate that the next character entered
is a command code. Some command codes
are keyboard Enter, keyboard Caps Lock, an
Table 1
The Parts List to Build the PS/2 Keyer on a Circuit Board
Part
Quantity Part Name
Vendor
Part Number
C1, C2 2
0.1
µ
F 50 V 10% PC-Mount Capacitor
Digi-Key
BC1148CT-ND
D5
1
Red round diffused lens LED
Digi-Key
P589-ND
R1-R5 5
330
Ω
¼ W resistor
Digi-Key
P330BACT-ND
R6
1
10 k
Ω
¼ W resistor
Digi-Key
10KQBK-ND
U1
1
40 pin IC socket
Digi-Key
3M5471-ND
U1 1
PIC18F4520-I/P
Digi-Key
PIC18F4520-I/P-ND
4
Self-Stick Rubber Feet
Digi-Key
SJ5012-0-ND
J1, J3 1
PS/2 keyboard extension cable with purple connectors
Micro Center
SKU: 133314
J2, J4 1
PS/2 mouse extension cable with green connectors
Micro Center
SKU: 133272
J4
1
¼ inch In-Line Stereo Audio Jack
RadioShack
Catalog #: 274-141
D1-D4 4
Green rectangular clear lens LED
SparkFun
SKU: COM-08532
Microchip PICkit 2 microcontroller programmer
Digi-Key
PG164120-ND
Saleae Logic logic analyzer
SparkFun
SKU: TOL-08938
Pro USB Converter: USB to PS/2 Keyboard and Mouse
Micro Center
SKU: 919712