PIC18F4550 USB HID Example using CCS C compiler

PIC18F4550 microcontroller has 1 USB (Universal Serial Bus) communication module. This topic shows how to use PIC18F4550 as a USB HID (Human Interface Device) to send and receive data from the PC.
The USB HID device doesn’t need any additional driver because it’s already installed in most of modern operating systems.

PIC18F4550 USB HID example hardware circuit:
The following figure shows our example basic circuit schematic.
PIC18F4550 USB HID example circuit

The USB power supply pin (5V) can be used in this project.
To send/receive data to/from the microcontroller using USB communication a software named HID Terminal from MikroElektronika is used. The main interface of this software is shown below:

USB HID Terminal

HID Terminal can be downloaded from the following link:
HID Terminal

About Example:
After plugging the USB cable which comes from the microcontroller, the device will appear in the HID devices list which named USB HID Example. If we want to send data to the microcontroller just tape the text and click on Send.
The LCD is used to display the data received by the microcontroller and after 1 second the microcontroller sends the same data back to the PC which will appear in the HID terminal software.

PIC18F4550 USB HID Example C code:
The C code below was tested with CCS C compiler version 5.051.
In this project the an external oscillator (8MHz) is used to run the microcontroller as well as the USB module. PIC18F4550 microcontroller always needs an external oscillator to run its USB module.
The fuses used in this project are:
HSPLL : high speed oscillator is used with PLL enabled
PLL2 : Divide by 2  (8MHz oscillator input)
CPUDIV1 : No system clock postscaler
USBDIV : USB clock source comes from PLL divide by 2
VREGEN : USB voltage regulator enabled
NOMCLR : Master Clear pin used for I/O (MCLR pin function disabled)
The program can send and receive a maximum of 16 bytes. The sizes can be changed with the following two lines:
#define USB_CONFIG_HID_TX_SIZE 16                // Transmit packet size (bytes)
#define USB_CONFIG_HID_RX_SIZE 16                // Receive packet size (bytes)
The following two lines for product ID and vendor ID numbers. This numbers can be changed:
#define USB_CONFIG_PID 1                         //Chnage Product Id
#define USB_CONFIG_VID 1234                      //Chnage Vendor Id
Other lines are described in the code.

PIC18F4550 USB HID Example Videos:
The following video shows our example in protoboard circuit.

And the following video shows example Proteus simulation.

Discover more from Simple Circuit

Subscribe to get the latest posts sent to your email.

5 thoughts on “PIC18F4550 USB HID Example using CCS C compiler”

  1. hi there i tried this code but when i connect usb to my laptop it shows error USB device not recognized. i also checked the connection but it still shows same error.

    //string 2 – product (this is optional, but we specified it’s use in the device descriptor)
    USB_STRING(“USB HID Example”),
    //string 3 – serial number (this is optional, but we specified it’s use in the device descriptor)

    I had this error message. I could not solve it. Please help me? Email: [email protected]

    Expression must evaulate to a constant – _ unicode

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top