Saturday, April 20, 2013

Quick USB Tutorial - Build your own USB device with PIC18F4550

Since I'm a big fan of USB technology, and I'd to share my experience about how to build your own USB device with everyone, so will introduce you a short tutorial about how to build a USB interface and where you can get the hardware and software required.
Before you start you will better comfortable with microcontrollers, C programming language and basic electronic circuits.

Why USB?
Because it’s a recent modern technology, you can find everywhere in all computers and handheld devices as well.

How to build a USB Interface?
- You need the hardware: a USB interfacing chip or a microcontroller with a built-in USB module.
- If you are using a microcontroller with a built-in USB module, then you will need the firmware that includes the “USB stack” to be downloaded on it, so the internal CPU can communicate over the USB.
- On the PC side, you will need a library that will communicate with the USB device, and it is dependent on the USB device "class" you decided to use, like if your hardware will appear as an USB HID class then you will need a C/C++/C#/Java...etc library to communicate with it, if you are using USB CDC class it is easier cause you can use COM (serial) port APIs to communicate with it.

Why I prefer USB CDC class interface devices more than other USB device classes?
- Because the microcontroller or the USB chip will appear to your PC as a virtual COM (serial RS-232) port, which ease the development of the application on your computer side to communicate with the USB device you designed, you can use the same APIs in C++/C#/Java or even Python to communicate.
Examples:

  • “MAX3420” USB chip from Maxim Integrated.
  • “FT2232” USB-to-Serial chip from FTDI.
  • Microchip “PIC18F4550”, a microcontroller with built-in USB module.
A ready-made USB projects for this microcontroller and others can be found inside "Microchip Application Libraries": 

Building USB interface with PIC18F4550 microcontroller:
I will share with you my experience about" how to build a USB interface" using PIC18F4550 microcontroller from Microchip, this tutorial is intended for people who are little experienced in Microchip microcontrollers.

First to know, USB is a mix between hardware and software stack, unlike PC parallel or serial ports interfaces which rely more on the hardware.

USB devices are categorized into classes, i.e. each group of USB devices that have similar features are grouped under what is called "Class", we have many USB classes like:

  • HID class for USB mice and keyboards.
  • Printer class for USB printers.
  • CDC class for USB-to-Serial converters and COM port communication.
  • Mass storage class for USB flash drives and HDD's.

In this tutorial I will show how to build USB-CDC class based device with the PIC18F4550 microcontroller, first download and install in sequence the following:


- MPLAB IDE from Microchip official website, registration required, its for free: http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en019469&part=SW007002
- C18 C-compiler from Microchip, Academic version is for free:
- Microchip application libraries, group of open source, ready-made projects developed by Microchip corporation that supports a wide range of their products, all projects developed in C: http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=2680&dDocName=en547784
After you finish installing everything, go to this path on your hard drive:
"C:\[Microchip Solutions Directory]\USB\Device - CDC - Basic Demo\Firmware\USB Device - CDC - Basic Demo\C18 - PICDEM FSUSB” project file.


Open project file "USB Device - CDC - Basic Demo - C18 - PICDEM FSUSB", this is a USB - CDC firmware for PICDEM development kit from Microchip Technologies, this kit is based on the PIC18F4550 microcontroller.
Build the project then burn it to your microcontroller, when you connect your microcontroller to a PC, a "New device found" wizard will pop-up, browse the device driver file in:
C:\[Microchip Solutions directory]\USB Device - CDC - Basic Demo\inf

After installation is done, open your device manager, you will find under "COM & LPT" your microcontroller installed as virtual COM port, from here you can interact and communicate with your microcontroller with the usual API's for COM port in any programming language you prefer.

This is a simple tutorial about how to build an application to interface COM in Win32 API's: http://msdn.microsoft.com/en-us/library/ff802693.aspx

and this a free and easy to use IDE and C/C++ compiler to develop your applications on the PC side: http://www.bloodshed.net/dev/devcpp.html




Continuing the discussion, you can modify the source code of the "CDC - Basic Demo - Firmware" project since its open source.


To add new headers to the code, search for headers files declaration in the code and add yours to them.


To add and declare new variables to the code search in the beginning in the code for a comment "Variables", under you can declare and initialize your variables.

To configure and enable modules of the PIC18F4550, you can do this in the "main( )" function, but take care not to configure any module inside the "while(1)" loop.

To add any I/O interacting process related to the microcontrollers I/O, you can add them inside the "while(1)" in the "main()" function or in "ProcessIO()" function, both ways are fine.






USB Interface circuit using PIC18F4550 microcontroller


Bill of Materials (BOM):
U1: PIC18F4550 8-bits microcontroller.
X1: 20 MHz crystal.
C1, C2: 18 pF capacitors.
C3: 470 nF tantalum capacitor.
R1: 1 K Ohm resistor.
PB1: push button.

Update (May 2014): 
Microchip has a newer version of the PIC18F4550 microcontroller, the PIC18F4553, its identical to the PIC18F4550 but with a better resolution built-in Analog to Digital converter (A/D) which is  12-bits (the old one is 10-bits only), to configure the USB stack to work with the new PIC18F4553 microcontroller please follow the instruction in this link: http://www.eee.metu.edu.tr/~design/lib/exe/fetch.php?media=lecture_notes:tutorial_1_-_implementation_of_a_usb_based_pic-to-pc_communication.pdf


Update (July 2016):
A copy of a USB-CDC firmware demo project for PIC18F4550 microcontroller based on Microchip Application Libraries is available on GitHub, read "ReadMe.txt" file first before setting up your project: https://github.com/kelray/USB-CDC-stack-for-PIC18F4550


If you are not familiar with Microchip development tools for PIC microcontrollers here is a simple starter guide how to use MPLAB IDE and C18 compiler from Microchip Inc. to develop applications and code for various PIC microcontroller in general and PIC18F4550 in specific as an example in this tutorial.

links and websites in the article might change or get outdated, so rely on google if  the link is not working.

This is an example project using PIC18F4550 microcontroller as a dual channel USB data-logger/oscilloscope: