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.

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 specificationsorganization. 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 themikroC 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.Hid_Enable
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.Example:

1

2

3

unsignedcharreadbuff[64]absolute0x500;// Buffers should be in USB RAM, please consult datasheet

unsignedcharwritebuff[64]absolute0x540;

HID_Enable(&readbuff,&writebuff);// Enable HID communication

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

Hid_Read
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.Example:

1

2

3

// keep trying to receive until success

while(!HID_Read())

;

Hid_Write
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.Example:

1

2

3

4

// keep trying to send until success

while(!HID_Write(&writebuff,64))

;

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

1

2

//Disable the USB data transfer

HID_Disable();

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

1

2

3

4

voidinterrupt()

{

USB_Interrupt_Proc();

}

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

Example

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

unsignedcharreadbuff[64]absolute0x500;// Buffers should be in USB RAM, please consult datasheet

unsignedcharwritebuff[64]absolute0x540;

charcount;

voidinterrupt()

{

USB_Interrupt_Proc();// USB servicing is done inside the interrupt

}

voidmain(void)

{

ADCON1|=0x0F;// Configure all ports with analog function as digital

CMCON|=7;// Disable comparators

HID_Enable(&readbuff,&writebuff);// Enable HID communication

while(1)

{

while(!HID_Read());

for(count=0;count<64;count++)

writebuff[count]=readbuff[count]+1;

while(!HID_Write(&writebuff,64));

}

}

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.

I think the problem in here: Unable to connect to Virtual USB Hub Host. But my Proteus cannot support for windown 64bit.

Nam Trung Dang

I fixed it. This above problem is the major reason. After I download Proteus Professional 8.0 and install Virtual USB Driver. Everything is ok. Can you make a tutorual that relate to USB and C#. I mean we can control PIC through Graphic User Interface. Thanks.

Nam Trung Dang

Hi Sir, I want to control a small thing like LED. For a scenario, after I receive ‘a’ letter from MCU, the LED in PORTB.B0 will turn on. Thanks.

dtn89

I DONE. Could you make a tutorial which is developed by using C# and USB. I am really thanks for your help.

It difficult to know what could be wrong based on this. It could be hardware or software problem.

Anas Mohammed

Hi , when I compile your code with my compiler it doesn’t work , hex file comes out a mere 1kb as compared to your 17kb . It mentioned something about source paths, Are there any missing files from the compressed file or should I try a different version of Mikroc Pro ? Thank you for your time and help

Hi. We don’t know what it could be, maybe the difference of mikroC Pro versions. Can you try to create a new project instead of using our project and see if it will be successful?
When you compile, does it generate errors?
Is your 1Kb code working as expected?

Anas Mohammed

Yea I tried creating my own project but i get the same 1kb hex file. The hex file doesn’t work , in Proteus it gives an error saying illegal opcode. Which version of Mikroc pro are using ? There are no errors during compilation , the USB descriptor is added to the source file but I don’t believe it is being compiled. Is there a video you recommend that shows all steps ? As maybe I am missing out a small detail.

Your project compiles well. I didn’t change anything, it compiled well and the size of hex file is 17Kb. I compiled with mikroC version 7.0.1 The project you downloaded on our website was done with version 6.5. It seems like the problem is your version 5, is not compatible. You must upgrade your compiler. Mikroelektonika allows upgrades of compilers for free if you have a valid license. You can upgrade it for free online.

Stay Update:

Search Here

Translate Website into your Language

Please support our work with a Donation

Writing tutorials and recording tutorials videos requires a lot of resources and time. If you found these tutorials helpful, please show your support and donate any amount you wish so that we can do even more.
Thanks