USB Human Interface Device Communication with PIC Microcontroller – MikroC
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.
Current Project / Post can also be found using:
- connect usb keyboard to pic16f877a
- human interface device
- vid pic mikroc pic18f4550