USB Communication Circuit Diagram

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.

Figure 3: Inserting a file into the project

MikroC Pro for PIC USB Library Functions

Below is a quick descriptions of some USB library functions, For more information, please visit online the mikroC pro for PIC USB library page.
USB Library contains HID routines that support HID class devices when a PIC microcontroller with built-in USB is used (e.g., PIC18F4550), and port pins RC4 and RC5 are connected to the D+ and D- pins of the USB connector respectively.
The library supports also the generic routines that can be used with vendor specified drivers.
This function enables USB communication and requires two arguments: the read-buffer address and the write-buffer address.
Before you can use any other functions, this  function should be called first.
This function returns no data.

Note: USB buffers for sending and receiving data must be placed in the USB RAM.
RAM areas that can be used for data buffers by these PIC microcontrollers are:
PIC18F2455/2550/4455/4550 : 0x500 – 0x7FF
PIC18F46J50 Family : 0x60 – 0x3FF and 0x500 – 0xEBF
PIC18F87J50 Family : 0x60 – 0x3FF and 0x500 – 0xF3F
PIC18F47J53 Family : 0x60 – 0xCFF and 0xE00 – 0xEBF
PIC18F/LF1XK50 : 0x280 – 0x2FF
PIC18(L)F2X/45K50 : 0x500 – 0x7FF
For more information, please check the corresponding datasheet.
USB RAM Memory for the PIC18F4550 is from 0x500 to 0x7FF as it can be seen on figure 4 below.

Figure 4: PIC18F4550 USB RAM memory

This function receives data from the USB bus and stores it in the read buffer.
It has no arguments but returns the number of characters received.

This function sends data from the write buffer to the USB bus.
The name of the buffer (the same buffer used in the initialization) and the length of the data to be sent must be specified as arguments to the function.
If the data transmitting has failed, the function returns 0. Otherwise, it returns number of transmitted bytes and always make sure to call this function repeatedly as long as data is not successfully sent.

This function disables the USB data transfer.
It has no arguments and returns no data.

This routine is used for servicing various USB bus events. Should be called inside USB interrupt routine.  

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).


This project example (circuit on figure 1) establishes connection with the HID terminal that is active on the PC. Upon connection establishment, the HID Device Name will appear in the respective window. After that software will wait for data and it will increment every received byte by a one and send it back.

Figure 5: USB HID Terminal

  • Plug the microcontroller into the PC’s USB port
  • You should see the list of HID devices connected to your computer. In this example a DELL Laser Mouse and our USB Bus Communication project are shown.

Select the device and type something in the Communication text box and click “send” to send data to microcontroller and anything received from the microcontroller will be displayed below. In our example the sent data was echoed back to the PC.

Figure 6: Using the HID Terminal to send and receive data

You can download the full project files (Flowcode and Proteus Schematic design) below here.
All the files are zipped, you will need to unzip them (Download a free version of the Winzip utility to unzip files).  

Download USB Bus Communication mikroC project
Download USB Communication Proteus