Summary of USB Human Interface Device Communication with PIC Microcontroller – MikroC
Summary (under 100 words) This article explains using the USB HID class (0x03) with PIC microcontrollers via mikroC Pro for PIC. It covers USB descriptors (device, configuration, interface, HID, endpoint), how a host enumerates a device, default VID/PID for development, and generating a HID descriptor file using mikroC Pro's HID Terminal. It also outlines descriptor options (VID/PID, buffer sizes, vendor/product names, bus power) and how to save and include the descriptor in a project.
Parts used in the USB HID Communication with PIC Microcontroller:
- PIC microcontroller
- USB cable/USB connection
- mikroC Pro for PIC compiler/software
- USB HID Terminal tool (in mikroC Pro)
- HID descriptor file (generated by HID Terminal)
- Host PC (USB host/operating system)
Figure 1: USB Communication Circuit Diagram
The HID device class code is “0x03, this class is used for devices operated by human, devices like keyboard, mouse, joystick and so forth.
The advantage of HID devices is that, they don’t require to install drivers, in most modern operating systems, the device will be detected without any problem.
MikroC Pro for PIC provides USB HID library that make it easy for a host device to communicate with a slave device on a USB bus.
Before you can go through this article, please read the USB Communication with PIC Microcontroller article.
When a new USB device is plugged into a bus, the USB host uses address 0 to ask basic information from the device. Then the host assigns it a unique USB address. After the host asks for and receives further information about the device, such as the name of the manufacturer, device capabilities, and product ID, two-way transactions can only begin.
All USB devices have a hierarchy of descriptors that describe various features of the device like the manufacturer ID, the version of the device, the version of USB it supports, what the device is, its power requirements, and so forth. The most common USB descriptors are:
- Device descriptors
- Configuration descriptors
- Interface descriptors
- HID descriptors
- Endpoint descriptors
The USB HID protocol allows a personal computer to recognize a USB HID connected to it without the need to create a device driver, this is the same like when you connect a USB optical mouse or a USB keyboard you don’t need to install drivers for that. Windows operating system will load the required drivers, all that is needed is to to supply the PC with a descriptor file containing some information from the device like Vendor ID (VID), Product ID (PID), Manufacture name and so forth.
To create a descriptor file in mikroC Pro, open your mikroC Pro project go to Tools menu and click HID Terminal then click the Descriptor tab.
Figure 2: Creating a Descriptor file
–> VID (vendor ID) and PID (product ID): Each USB device must have a unique set of PID and VID codes.
The default PID and VID values can be used for development and experimental purposes only but to release your own USB devices you must purchase a unique VID and PID from the USB specifications organization. The default VID is 1234 and PID 0001
–> Input and Output data length: This is the input and output buffer size. the default is 64 character.
–> Vendor Name and Product Name: here you can write your vendor name and product name.
–> Bus power: Tick this option to give a permission for your PIC to take its power from PC.
–> Select the correct compiler: mikroC
–> Click on “Save descriptor” and save it in any place you want, in our example we are going to save it in the same directory as the project and you can save it with any name you like.
–> Now you need to include your descriptor file to your project: In project manager, click on Add file to project, browse to the location where you saved your descriptor file, select it and click Open.
For more detail: USB Human Interface Device Communication with PIC Microcontroller – MikroC
- What is the HID class code used in this project?
The HID device class code is 0x03. - Do HID devices require custom drivers on modern operating systems?
No, HID devices typically do not require installing drivers and are detected by most modern operating systems. - Which descriptors are commonly used for USB devices?
Common USB descriptors are device, configuration, interface, HID, and endpoint descriptors. - What are the default VID and PID values provided for development?
The default VID is 1234 and PID is 0001 for development and experimental purposes. - How do you create a HID descriptor file in mikroC Pro?
Open your project, go to Tools, click HID Terminal, then use the Descriptor tab to set values and save the descriptor file. - What descriptor options should be specified in the HID Terminal?
Specify VID and PID, input and output data length, vendor name, product name, bus power option, and select the correct compiler mikroC. - Where should the saved descriptor file be added in the project?
Save the descriptor file (for example in the project directory), then use Project Manager Add file to project to include it. - Can the default VID/PID be used for released products?
No, for released products you must purchase unique VID and PID from the USB specifications organization; defaults are for development only.