USB communication with C18 and MPLAB

From Mech
Jump to navigationJump to search

Microchip publishes a USB Framework that consists of a number of files that contain code to handle the tedious parts of USB communication, with a few spots where user code can be included to build USB programs.

When using the Microchip USB Framework, programs can operate in two modes: polling or interrupt. This choice comes from the fact that USB devices must continually monitor the connection to the host, looking for incoming data and handling the sending of data in the output queue. There are two ways to accomplish this: the program can either explicitly call a function periodically to handle USB tasks, or this can be done by a regularly scheduled interrupt.

The framework provides two slots in which user code can be placed: the functions UserInit and ProcessIO. UserInit is called once at the beginning of the program to allow you to do some initialization. ProcessIO is called in a loop; each iteration should be very short and must be non-blocking, if you are using the polling structure.

Below is an example program using the polling structure, showing just the UserInit and ProcessIO functions. You can download the full MPLAB project here. Note: this MPLAB project is configured to work with the USB HID bootloader. Using it without a bootloader or with another bootloader will require changes.