Microchip USB: Part 1
For the last few weeks I've been skirting around USB, looking at USB to serial adapters and even talking to some off-the-shelf USB hardware using libusb. Of course, the next big step is to just go ahead and build something that actually attaches as a USB HID device. There's lots of support for this if you get the right microcontroller, and the list grows longer every day.
I finally decided to get started with the Microchip PIC solution. I happened to have some PIC 18F2550 chips handy and I recently wrote about my experiences with MPLAB-X on Linux and the ICD 3 programmer/debugger.
One nice thing about Microchip parts is the pin outs rarely change. The PIC 18F2550 has the familiar 28 pin footprint you expect from a PIC. A few pins are unique to the USB-nature of the part, of course. For example, port C loses a pin to the Vusb function and two more pins if you actually use the onboard USB peripheral.
Still, it was easy enough to connect the device to a breadboard for initial testing. I had previously picked up some nice USB breakout boards that let me hook a standard USB cable to a breadboard, but lacking that I'd have just cut a USB cable in half and soldered pins to the exposed wires. The USB board has five pins but one of them is the frame ground. The other four pins provide signal ground, 5V, and the two data lines (D+ and D-).
If you want to follow along, the circuit is fairly straightforward. I connected a 10K resistor between the 5V supply and pin 1 (the RESET pin). The 5V supply, by the way, comes straight off the USB cable. Pin 20 also connects to the 5V rail. The Vss pins (pins 8 and 19) go to ground, of course, along with the USB cable's ground pin. I also put a 20MHz resonator on the PIC's clock pins (pins 9 and 10).
The data sheet also tells you to bypass the Vusb pin (pin 14) with a .22uF capacitor. This is the 3.3V output provided for the USB transceiver and I didn't think it was that critical. I used two .1uF capacitors in parallel and decided that was close enough. You can optionally pull up the D+ and D- lines from Vusb (through resistors, of course), but I was trying to stay as simple as possible. The regulator, by the way, has to be enabled so if you aren't using USB you shouldn't need anything on this pin.
The other items I connected were pretty simple: a connector for the ICD 3 (I have a modular jack wired for just this purpose; the ICD 3 manual shows the connections for power, ground, RESET, RB7, and RB6 — you can leave RB3 disconnected) and a .1uF bypass capacitor between the chip's power lines (pins 19 and 20). I also wanted some simple I/O, so I put a 5V LED on pin 4 (RA2) and a switch with a 10K pull up resistor on pin 5 (RA3).
Below is a picture of the rat's nest — er — breadboard. No schematic, at least not this week, but if you grab the data sheet and follow the pin numbers you can hardly go wrong.
The remaining piece of the puzzle is software. You need three major components:
Last time I installed the USB framework it was a bit quirky, but I'm hopeful it will be better this time since it seems to be packaged a little better. The C compiler nicely integrates with MPLAB-X and I wrote a simple little test program to check out the hardware. The only thing the USB port does in this case is provide power (and note, do NOT check the box to have the ICD 3 deliver power to the device while you are using any kind of external power; the default is for the ICD 3 to remain passive, so you should not need to turn it off, but be careful not to turn it on).