Summary of Connect USB HID with PIC18F4450 Code
USB HID Library enables communication between PIC microcontrollers and host computers using Human Interface Devices like keyboards, mice, and sensors. It includes routines for enabling, reading, writing, and disabling USB connections via interrupt or polling methods. A descriptor file is essential for defining device identity and report structures.Parts used in the USB HID Project:
- USB Library with HID routines
- Descriptor source file (USBdsc.c)
- mikroC PRO for PIC integrated USB HID terminal
- HID_Enable function
- HID_Read function
- HID_Write function
- HID_Disable function
- USB_Interrupt_Proc routine
- USB_Polling_Proc routine
USB HID Class
The HID class consists primarily of devices that are used by humans to control the operation of computer systems. Typical examples of HID class devices include :
- Keyboards and pointing devices, for example: standard mouse devices, trackballs, and joysticks.
- Front-panel controls, for example: knobs, switches, buttons, and sliders.
- Controls that might be found on devices such as telephones, VCR remote controls, games or simulation devices, for example: data gloves, throttles, steering wheels, and rudder pedals.
- Devices that may not require human interaction but provide data in a similar format to HID class devices, for example, bar-code readers, thermometers, or voltmeters.
Many typical HID class devices include indicators, specialized displays, audio feedback, and force or tactile feedback. Therefore, the HID class definition includes support for various types of output directed to the end user.
Descriptor File
Each project based on the USB library should include a descriptor source file which contains vendor id and name, product id and name, report length, and other relevant information. To create a descriptor file, use the integrated USB HID terminal of mikroC PRO for PIC (Tools › USB HID Terminal). The default name for descriptor file is USBdsc.c, but you may rename it.

- The USB library routines have been changed. Please, have this in mind when migrating projects from previous versions of the compiler.
- Also, this relates to the descriptor source file, so it is necessary to create a new descriptor file in order to make your project work.
Library Routines
- HID_Enable
- HID_Read
- HID_Write
- HID_Disable
- USB_Interrupt_Proc
- USB_Polling_Proc
- Gen_Enable
- Gen_Read
- Gen_Write
HID_Enable
| Prototype | void HID_Enable(char *readbuff, char *writebuff); |
|---|---|
| Description | Enables USB HID communication. |
| Parameters |
These parameters are used for HID communication. |
| Returns | Nothing. |
| Requires | Nothing. |
| Example |
Code listing
|
| Notes | This function needs to be called before using other routines of USB HID Library. |
HID_Read
| Prototype | char HID_Read(void); |
|---|---|
| Description | Receives message from host and stores it in the Read Buffer. |
| Parameters | None. |
| Returns | If the data reading has failed, the function returns 0. Otherwise, it returns number of characters received from the host. |
| Requires | USB HID needs to be enabled before using this function. |
| Example |
Code listing (2)
|
| Notes | None. |
HID_Write
| Prototype | char HID_Write(char *writebuff, char len); |
|---|---|
| Description | Function sends data from Write Buffer writebuff to host. |
| Parameters |
|
| Returns | If the data transmitting has failed, the function returns 0. Otherwise, it returns number of transmitted bytes. |
| Requires | USB HID needs to be enabled before using this function. |
| Example |
Code listing (3)
|
| Notes | Function call needs to be repeated as long as data is not successfuly sent. |
HID_Disable
| Prototype | void HID_Disable(void); |
|---|---|
| Description | Disables USB HID communication. |
| Parameters | None. |
| Returns | Nothing. |
| Requires | USB HID needs to be enabled before using this function. |
| Example |
Code listing (4)
|
| Notes | None. |
USB_Interrupt_Proc
| Prototype | void USB_Interrupt_Proc(void); |
|---|---|
| Description | This routine is used for servicing various USB bus events. Should be called inside USB interrupt routine. |
| Parameters | None. |
| Returns | Nothing. |
| Requires | Nothing. |
| Example |
Code listing (5)
|
| Notes | Do not use this function with USB_Polling_Proc, only one should be used. To enable servicing through interrupt, USB_INTERRUPT constant should be set (it is set by default in descriptor file). |
USB_Polling_Proc
| Prototype | void USB_Polling_Proc(void); |
|---|---|
| Description | This routine is used for servicing various USB bus events. It should be periodically, preferably every 100 microseconds. |
| Parameters | None. |
| Returns | Nothing. |
| Requires | Nothing. |
| Example |
Code listing (6)
|
For more detail: Connect USB HID with PIC18F4450 Code
Quick Solutions to Questions related to USB HID Project:
- What devices are supported by the HID class?
The HID class supports keyboards, pointing devices, front-panel controls, game controllers, bar-code readers, thermometers, and voltmeters. - How do I create a descriptor file for my project?
Use the integrated USB HID terminal of mikroC PRO for PIC under Tools to generate the file. - Can I rename the default descriptor file?
Yes, the default name is USBdsc.c but you may rename it as needed. - Does the USB library require re-creation of descriptor files when migrating?
Yes, USB library routines have changed so a new descriptor file must be created for older projects. - Which function must be called before using other USB HID routines?
You must call HID_Enable before using any other functions in the library. - What is the difference between USB_Interrupt_Proc and USB_Polling_Proc?
USB_Interrupt_Proc handles events inside an interrupt routine while USB_Polling_Proc should be called periodically every 100 microseconds. - Can I use both interrupt and polling procedures simultaneously?
No, you must use only one of them; do not use both together. - How does the HID_Read function indicate failure?
If data reading fails, the function returns 0; otherwise it returns the number of characters received.
Related projects
CAN FD Node based on a PIC18 Microcontroller
The introduction sets the stage for an embedded systems project that involves developing a Embedded C CAN FD project using the MCP2517FD...
Communication & InterfacingLAN9662 Gigabit Switch: Optimized for Industrial Ethernet
Introduction It should come as little surprise therefore that one element of what is now more commonly referred to in some circles...
Communication & InterfacingPIC Microcontroller: Fundamentals & Applications for Students
Introduction Microcontrollers are the key components in the embedded systems market. A variety of embedded systems was formed and developed after the...
Communication & Interfacing10BASE-T1S Ethernet: Simplified OEM Design for Auto Devices
Introduction Cars these days are surrounded by technology. They come loaded with all sorts of electronic gadgets and sensors. This project wants...
Communication & InterfacingDesigning a Microcontroller Framework for PC-Based Control of Electrical Appliances
ABSTRACT The preference for personal computers as a platform for designing and implementing control algorithms is growing due to the ease of...
Communication & InterfacingEfficient Multichannel Data Acquisition with USB Connectivity using PIC Microcontroller
1. INTRODUCTION With the rapid advancements in embedded technology, there is a growing demand for a data acquisition system that combines fast...

