PIC32 Harmony USB tutorial

How to use MPLAB Harmony USB stack with PIC32 microcontroller

Microchip via its MPLAB Harmony is perhaps one of the few which provides full USB stack solution to its microcontroller users. Microchip recommends using Harmony for its PIC32 family of microcontrollers. Through this USB tutorial you can learn how to implement the USB stack solution provided by Microchip for PIC32 microcontroller for purpose of making a USB CDC device. For the purpose of showing this, the PIC32MX470F512 32 bit microcontroller will be used. But once you have grasped the underlying method you can use it for any microchip microcontroller.

In this USB device tutorial, we will use the existing cdc_com_port_single project example from Microchip. This is helpful because (1) All code are readily available to you because they are free to download when you install Harmony(v1.11 is used). (2) Bug free and detailed cod is provided by microchip which allows you to learn how microchip wants users to implement codes when using harmony framework. (3) Although existing project is used, it does not infer with learning because how the code work will be explained.

What does the cdc_com_port_single project do?

In this project, you will make the PIC32 microcontroller will be configured as device. The PIC32 USB device will appear like a com port or serial port to the PC(after you connect to PC via USB cable). Once the PC recognizes the USB device, you will send a character from a terminal(putty, teraterm or any can be used) on the PC to the device and the device will send back the next character back to the terminal. That is when you type in “a” then you will see “b” on the terminal screen.

How the tutorial is organized

The tutorial starts with hardware aspects showing you how our microcontroller board, micro USB B port and LEDs are connected. Then we will explain the software aspects. In here, we explain the Harmony project for PIC32, the MHC and how to use it for configuring clock, USB stack and how to generate code. Finally we talk about how to actually test the USB device.

Hardware: Circuit Diagram

The hardware consist of the PIC32MX470F512 microcontroller break out board, breadboard, micro USB B port, three LEDs, three 330Ohm resistors, one 100KOhm resistor, 10uF capacitor, 3.3V power supply. Also you need programmer like PICKIT 3 to burn the code into the microcontroller.

As we stated the tutorial is based on the cdc_com_port_single example from Harmony. In harmony you can use the project example with various development board. Once you choose the development board, you can then compile and burn the code into the microcontroller of the chosen development board. One of the development board is the USB starter kit 3 which you can select in the MPLABX IDE by selecting pic32mx_usb_sk3_int_dyn option as you will see later in the software section of this tutorial.

Now, the pic32mx_usb_sk3_int_dyn which corresponds to USB starter kit 3 development board uses the PIC32MX470F512 microcontroller which is same as ours. Hence the code setting for pic32mx_usb_sk3_int_dyn will also work with our microcontroller.

Thus we can set up the hardware to follow the USB starter kit 3 development board. Now the USB starter kit 3 development board has many features and therefore more hardware components. But in our basic purpose we need only the micro USB B port and the LEDs. There are 3 LEDs which indicate the state of the USB device, hence used.

The circuit schematic is shown below.

PIC32 USB B schematic

Note that in our case, the microcontroller is driven using 12MHz external crystal oscillator as opposed to the 8MHz oscillator used in USB Starter Kit 3.

The USB B micro port connection to the PIC32 microcontroller is shown below.

Shown below is the three LEDs connections to the microcontroller that indicates the state of the USB device.

And the picture below shows the implementation of the above circuit diagram: PIC32MX470F512 microcontroller break out board, the LEDs with resistors on breadboard and the USB B micro port connection with resistor and capacitor connection.

usb interfacing with mcu

Leave a Reply

Your email address will not be published. Required fields are marked *