isn’t much else that would be useful in terms of basic
The rest of the components are supporting hardware
in order to make the PIC happy. Y1 is our ceramic
resonator clock source. JP1 is a pin header used for in-circuit programming. Resistor R1 (33K ohms-39K ohms) is
the MCLR (programming voltage) pull-up resistor. C2 is a
decoupling capacitor with a value of 1 µF to 5 µF. C4 is a
ceramic capacitor used by the PIC18LF2550’s internal
regulator. There is a range of values that C4 can have, but
I’ve found 0.47 µF works the best in most situations.
D3 and D4 are 3. 9 Vdc zener diodes used as
transient and over-voltage protection for the USB data
lines. There are many ways to perform this task. I had a
bunch of zener diodes, so I used them. D1 is a 1N4148
type used to isolate the PCB voltage from the USB
attached to the PC. If we are using the power from the
USB connection, leave D1 connected as shown.
If, for some reason, you choose to use a separate
power supply in addition to the PC, remove D1 from the
circuit. This will disconnect the PC power from the circuit
and allow our own board voltage sources of IC3 or
(directly) X1 to power our device. The design used here
has the USB as the power source, so as not to damage
the PCB or any attached USB host when using optional
Figure 3 shows our EAGLE produced PCB layout.
Notice there is a row of pin headers on the top and a
double row on the bottom. The top row is connected only
to the junction of the switch pull-up resistor and the switch.
These are used in the case where your switches use a
shared ground. The bottom double row is used for two
wire connected momentary switches as shown in Figure 4.
Additional power options are on the right of the PCB.
The final piece of the puzzle is that the PIC/PCB
needs to act like a HID keyboard device. Luckily, we don’t
have to start from scratch because my PIC compiler of
choice — CCS’ PIC-C — has been good enough to provide
some example USB HID files that we can quickly modify
in order to get our device up and running. These files
begin with “usb_kbmouseXXX.c” and are within the PCW
USB directory of the compiler.
If you do not have this compiler, no big
deal. The concept is always the same: Tell the
attached PC what kind of HID device we are via
the device descriptor, and send messages to (in
other cases, receive from) the PC that are
accepted as HID type messages. Beyond that,
we probably want to tell our PIC what messages
(key/scan codes) to send, depending on which
key closures are shorted to ground. The
provided firmware sends these key/scan codes:
0-9, a-d, <return>, and <backspace>.
Figure 5 is a screenshot of the main
function of our PIC18LF2550’s firmware code.
We’ll go over the main function first and its
generalities, followed by a couple of smaller
sections of code that aid in understanding what
is actually happening.
Lines 264-282 get the PIC up and running
with configuration and initialization code related
to setting port pins as inputs or outputs,
registers, and setting up the USB device
30 November 2013
■ FIGURE 4. The final
■ FIGURE 3.