# C/C++/C# programming for interfacing USB to PIC microcontroller

I am a computer sciences student looking to do some PIC microcontroller programming for some small home projects . I am fluent in C/C++/C#/Java and i also have an understanding of programming microprocessors(ALP - the 8086 ) . I plan on building a USB interface to control some lighting and a motor in one of my projects . I have the circuit components and the layout planned out . I have the PIC 18f4550 in mind and I have already downloaded the libraries from the microchip website . The only problem is I don't know what code to write for this . The libraries are huge and I don't really need to go into all the details(that's what I think) . I tried finding other resources but it appears that the PIC is old and most of the links off of Google have only been dead ends . Does anyone know of a blog , a video , or anything that could help me get started ? I'm hoping it's possible to do this using one of the languages I mentioned above .

• Did you take a look at the examples provided with the Microchip usb library? When you google, try not to search for only PIC18f4550, but for PIC18f USB examples in general. The PIC18 devices are similar enough to transfer (most of the) USB code from one to another. – PetPaulsen Jul 6 '12 at 15:44
• If you don't mind not coming up the USB learning curve, using a FTDI USB chip/connector/cable may be the easiest way to get a PIC up on USB. ftdichip.com/FTProducts.htm and digikey.com/PTM/… – kenny Jul 6 '12 at 20:07
• No one mention MPLAB X here? Am I missing something? – Bradman175 Feb 10 '17 at 12:30

You should first take a look at the examples provides with the Microchip USB Framework (part of the Microchip Application Libraries). The docs give some descriptions on the individual demos. The helpfile itself is in my opinion a little bit hidden, but you can find it at <YourInstallLocation>\Microchip\Help\MCHPFSUSB Library Help.pdf

To get started and depending how you want your device to appear on the host pc, I would go through the code of the following demos:

• Device - CDC Basic Demo
• Device - HID - Custom Demo

Both state in the description that they are able to run on the 'Low Pin Count USB Development Board', which is using a PIC18F14K50. The PIC18F4550 should be similar enough to allow you to transfer most, maybe even all, of the code.

Another approach, as kenny recommended, is to use a IC that handles most of the USB stuff for you. A popular choice are the ones from FTDI. When you take the FT232R, you hook the UART of your microcontroller to the IC. Writing code to transmit / receive data is now much simpler.

Similar to the FT232R is the IC MCP2200 manufactured by Microchip. Which one you choose is a matter of taste (and maybe driver support).

• +1 for offloading the pain to an FTDI chip. Life's too short for rolling your own USB stack unless you're being paid to do exactly that and they won't take FTDI as an answer. There are far more 'fun' problems out there, like how to fit a housebrick up your nose. – John U Jun 8 '15 at 15:01

Building a PIC18F USB Device

I've used his C# library and it works very well and it's easy to use. It can be easily expanded or modified to meet whatever needs you have. His tutorials are also very thorough and easy to understand and he usually replies quickly on the forums if you have questions.

As PetPaulsen already mentioned, look through all the code and sample projects provided by Microchip. You do not need to understand how the USB protocol works. You really only need to modify the main file and usb_descriptors file to get things working.

You may try code from http://tomeko.net/miniscope.php?lang=en There is slightly simplified "Generic HID Demo" from Microchip samples (some redundant multi-board code sections removed) for older MPlab + C18 and console-based Code::Blocks project for Windows (toggling LED, reading pushbutton, reading ADC). If you don't need lots of GPIO try smaller PIC18F2550 instead. That said I don't think these MCUs are competitive at the moment with for example STM32F042F (TSSOP20 package, USB device with no external crystal, more memory, USB bootloader in ROM) or STM32F103 (mini-boards are available just for \$4, UART-based bootloader).

Note that for PIC18F2550/4550 you would probably need to buy programmer (unless you have some old PC with proper RS-232 to connect simple JDM-style programmer).