How to do USB in Physical Computing

Warning: technical

Serial cables are slowly becoming obsolete; being excluded with every new computer version. Ever wonder if it would be possible to write a USB device for PCs? If you know how to program PIC chips, then this tutorial will show you how it's done, with hardware, firmware and software examples.

Example source code available for these programming languages on a PC:

Microsoft Visual Basic
Macromedia Flash

Macromedia Director
Proce55ing
Java


In the PicBasic Pro compiler, there are two cryptic commands - USBIn and USBOut. I say cryptic because learning how to use such commands sends you on a wild Internet goose chase. USB does not work unless you use one of the two USB-able pics from Microchip. Unfortunately, neither of these chips comes in EEPROM format, like the PIC16F84 or PIC16F876. Instead, there is an EPROM version (note 1 'E') that can be reprogrammed ONLY if you place it under an ultraviolet lamp for 15 minutes. If you do not first erase this chip with the lamp, then the EPICWin downloader will give you errors. These UV lamps can be ordered online. If you are in ITP, then UV eraser available for check out in the ITP equipment room. Ask for the tan and blue tackle box.

Here are the component part numbers.

Component Digikey Part #
PIC16C745 (cdip) PIC16C745/JW-ND
24 Mhz crystal CTX091-ND
USB female B connector 924K-ND

The two electrolic capacitors and the resistor are available at Radioshack.





The USB female B connector connects to your USB cable. For more info about connecting the USB cable to the chip, read How USB Works.



Some example code

Here is how to talk to your usb pic chip from Director Lingo. First, the PicBasic project:

Download Zip File

Make sure that USBDESC.ASM remains in the same folder as any of these .BAS files. The PicBasic compiler looks for that exact file name if it detects that you are compiling a program for a PIC16C745.

and now the "hello world" program (note: this includes a USBXtra demo that will pop up a demo nag, then it will shut down Director. You need to buy USBXtra):

Download USBXtra Director Example
Download USBXtra Flash Example
Download USBXtra Proce55ing Example
Download USBXtra Java Example
Download USBXtra Visual Basic Example

After downloading the PicBasic code into the chip, connect the circuit to the computer and run the director file. You should see a small dot onmouseH the stage. Your is being transmitted to the pic chip through the USB cable. The pic chip then takes that number and adds 1 to it. The chip then sends this byte back through the usb cable and the Lingo command reads it - storing whatever comes back into the locH of sprite(1). Look in the code to see how Lingo sends and gets bytes from the USBXtra object. It is important to note that unplugging the USB device will cause the isOpen() function to return FALSE. Most of the examples have a loop that attempts to reconnect.




Programming settings for EPICWin

(first remember to set it to "PIC16C745"
Go to the configuration menu and make sure none of the items are checked. I'm talking about "Brown-Out Voltage," "watchdog Timer," and "Power-Up Timer."

In the oscillator sub-menu, make sure that "HS" is checked for the 24 Mhz crystal.

Connecting 100 USBPics to One Computer

First of all, since this is USB, you can connect up to 127 devices to a single computer. If you run out of USB ports, then you will need to purchase a USB hub from your local Circuit City, Radioshack, etc. Usually, these hubs will use one USB port, but provide between two and ten new ports. I have yet to see a USB hub provide more than ten ports. That means that if you wish to connect 100 USBPics to your computer, you could (for example) connect two 10-port hubs to your computer, then connect twenty 5-port hubs, completely filling up both 10-port hubs, but leaving you with 100 USB ports on one computer.

On the PIC Chip, you will need to change the Vendor ID and Product ID on the chips so that each of them is unique. Later, you specify this number in the open() function of USBXtra. In USBDESC.ASM at line 133, you will find the following 4 literal bytes:

retlw 0x25 ; idVendor
retlw 0x09 ; high order byte

retlw 0x34 ; idProduct
retlw 0x12 ; high order byte


Note, these numbers are in hexadecimal, but here is the translation:

0x0925 -> 2341
0x1234 -> 4660

In the example code, you will see these two numbers in the open() function:

usbxtra.open(2341, 4660, false)

This is the vendor id and Product id of LakeView Research. Those are the people who wrote the original USB example for Pic Basic. Change these numbers so that everyone plugging in their devices will have unique numbers for their programs to identify by. These ID numbers are supposed to adhere to a central registry, but if you are just making a prototype, or if you are only worried about conflicting with other devices on your own computer, then this ID system can still be privately useful.

back to Josh Nimoy