Difference between revisions of "USB communication with PC"
m |
m (by Greg McGlynn) |
||
Line 22: | Line 22: | ||
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 [[PIC RS232|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. |
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 [[PIC RS232|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 is shown below. The program waits for some input and then responds to it. When you first plug in the PIC to the computer, Windows will need to install a driver to talk to it. If you're running XP or earlier, point it to the directory C:\Program Files\PICC\Drivers, where it will find what it needs. If you're running Vista the driver may not be there; download [[Cdc_NTXPVista.inf]], save it somewhere, and point Windows to it. Once the driver is installed a virtual COM port will be created (it will be visible in the device manager) and you'll be able to talk to the PIC in Hyperterminal, Matlab, or anything else that communications over RS232. |
A sample program using CCS's CDC library is shown below. The program waits for some input and then responds to it. When you first plug in the PIC to the computer, Windows will need to install a driver to talk to it. If you're running XP or earlier, point it to the directory C:\Program Files\PICC\Drivers, where it will find what it needs. If you're running Vista the driver may not be there; download [[Image:Cdc_NTXPVista.inf]], save it somewhere, and point Windows to it. Once the driver is installed a virtual COM port will be created (it will be visible in the device manager) and you'll be able to talk to the PIC in Hyperterminal, Matlab, or anything else that communications over RS232. |
||
<pre>#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. |
<pre>#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. |
Revision as of 16:29, 20 April 2009
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 it has hardware support for USB built into it. The 18f4553 will work just fine in one of the PIC protoboards.
I'm not clear on how cross-platform USB software tends to be. 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, and using USB will be easy.
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.
- +5V: 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: ground; make sure you do connect this to your PIC's ground.
- D+ and D-: 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 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 is shown below. The program waits for some input and then responds to it. When you first plug in the PIC to the computer, Windows will need to install a driver to talk to it. If you're running XP or earlier, point it to the directory C:\Program Files\PICC\Drivers, where it will find what it needs. If you're running Vista the driver may not be there; download File:Cdc NTXPVista.inf, save it somewhere, and point Windows to it. Once the driver is installed a virtual COM port will be created (it will be visible in the device manager) and you'll be able to talk to the PIC in Hyperterminal, Matlab, or anything else that communications over RS232.
#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. #fuses HS, NOWDT, NOLVP, NOPROTECT //standard fuses //these fuses are critical. they assume a 20Mhz oscillator: #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. #fuses HSPLL //I'm not exactly sure what this fuses does, but it's probably necessary. #fuses VREGEN //The USB module must run at 3.3V. This tells the PIC to accomplish this by using an internal voltage regulator. #fuses CPUDIV1 //This tells the PIC to run at a frequency of 48Mhz / 1 = 48Mhz. It may or may not be necessary. #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 } } }
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.
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.