USB communication with PC

From Mech
Revision as of 16:31, 20 April 2009 by Kwang Sim (talk | contribs) (by Greg McGlynn)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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 18f4553, which is similar to the 18f4520 in almost all respects, except that a few pins can be used for USB communication. The 18f4553 will work just fine in one of the 4520_Board_intro.

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|http://en.wikipedia.org/wiki/Usb]. In particular, USB connectors have four leads:

  • +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.

USB with the PIC 18F4553

Hardware

On the 18f4553, the D+ line connects to pin RC5 and the D- line to pin RC4. The ground line of course connects to ground. Hook these up and your circuit is ready to talk USB.

Software

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 or Hyperterminal and use to talk to the PIC.

A sample program using CCS's CDC library looks like this:

[code]#include <18f4550.h> //if your version of the compiler doesn't support the 4553, you can put the 4550 in your code, which is almost exactly the same.

  1. fuses HS, NOWDT, NOLVP, NOPROTECT //standard fuses

//these fuses are critical. they assume a 20Mhz oscillator:

  1. fuses PLL5 //The USB module must run at 48Mhz. This tells the PIC to take the oscillator, divide the frequency by 5, then multiply by 12. The result must be 48Mhz.
  2. fuses HSPLL //I'm not exactly sure what this fuses does, but it's probably necessary.
  3. fuses VREGEN //The USB module must run at 3.3V. This tells the PIC to accomplish this by using an internal voltage regulator.
  4. fuses CPUDIV1 //This tells the PIC to run at a frequency of 48Mhz / 1 = 48Mhz. It may or may not be necessary.
  1. include <usb_cdc.h> //Include the CCS USB library. If you want to see all the functions available look at usb_cdc.h and usb.h.

void main() {

  int c;
  //these lines do some initialization, obviously:
  usb_cdc_init();
  usb_init();
  while(true) {
      usb_task(); //handles connections to and disconnections from the computer, registering the PIC with the computer, etc. call this pretty often so that the PIC responds to being plugged in. 
      
      if(usb_cdc_kbhit()) { //did we get some incoming data? 
          c = usb_cdc_getc(); //get one character of input
          printf(usb_cdc_putc, "you typed: %c\r\n", c); //print out a response over usb
      }
  }

}[/code]


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, unfettered USB can go up to 12M bits/second, so HID is fairly slow in comparison.

Microchip provides a USB framework for use with its C18 compiler.

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