USB communication with PC

From Mech
Jump to navigationJump to search

Some PIC models have built-in USB transceivers that make it simple to connect the PIC to a computer via USB. One such PIC is the PIC 18f4550, which is similar to the 18f4520 in almost all respects, except that it has hardware support for USB built into it. The 18f4550 will work just fine in one of the PIC protoboards.

USB software for the PC tends to be OS-specific. This article currently assumes you're running Windows.

The USB protocol

USB is a pretty complex protocol with a long specification, but it's not necessary to understand every part of it to use it. It's enough to know what wires to hook up and what software libraries to include in your code.

Wikipedia has a detailed and informative page on USB.


USB connectors have four leads; the pictures on Wikipedia show which is which on standard connectors. If you cut open a USB cable, the colors will also tell you which wire is which:

  • +5V (red): the host (computer) provides up to 500mA of current at 5V that you can use for power. If your PIC is self-powered, though, make sure you don't connect the two +5V sources or bad things may happen.
  • GND (black): ground; make sure you do connect this to your PIC's ground.
  • D+ (green) and D- (white): data is transmitted on these lines. The exact protocol is complicated and you probably don't need to know the details.

USB with the PIC 18F4550


On the 18f4550, the D+ line connects to pin RC5 and the D- line to pin RC4. The ground line of course connects to ground.

Additionally pin 18 on the PIC, called Vusb, should have a capacitor to ground. Try 220 or 470nF.


On the software side you have some choices. Probably the simplest thing to do is use a preexisting software library that makes the USB connection look like an RS232 connection. The CCS compiler has a CDC library that provides functions like usb_cdc_getc or usb_cdc_putc that work over USB exactly like regular getc and putc work over RS232. CCS also provides a Windows driver for the PC that creates a virtual COM port that you can open in Matlab, Hyperterminal, or any other RS232-capable program and use to talk to the PIC. See USB communication with CCS.

Microchip provides a USB framework that works with its C18 compiler and its MPLAB development environment. See USB communication with C18 and MPLAB.

CCS also has an example where the PIC registers itself as a Human Interface Device (HID). USB mice and keyboards use HID. The advantage of this is that doesn't require the user to install a special driver. The disadvantages are (a) the connection doesn't look like a simple RS232 link and (b) bandwidth is limited. A HID can only send messages to the computer once every millisecond, and can only send 64 bytes in each message, so the maximum data transfer rate is 64K bytes/second. In theory, USB can go up to 12M bits/second, so HID is fairly slow in comparison.

It's also possible to create one's own setup from scratch or by modifying a preexisting library. This probably takes some effort.

See also

USB Bootloading