You are on page 1of 11

HID USB Communication

(Pic18f2550 with PC)


ROBOCON 2013 Competition

This document contain our project step by step to help you in communication with your PC throw the HID
USB module in your PIC18f2550 or PIC18f4550

3/25/2013
1- Introduction:

1.1-Why USB ??

- High speed
- Simple programming
- Simple connections and no need for extra hardware
- Familiar and easy to communicate with PC
- High noise immunity as the USB cable is shielded
- No need for external power supply as your power supply will be taken from your PC
- You can communication with multi slaves (using a USB Hub)

**NOTE: USB slave address contain 7 bit and thats mean you are able to drive 27 -1 = 127 devise

1.2-USB speed:

1- USB 1.0: Low speed 1.5 Mbit/sec


: High speed 12 Mbit/sec

2- USB 2.0: High speed 480 Mbit/sec

3- USB 3.0: High speed 5 Gbit/sec

**NOTE: our PIC support USB 1.0 (with low and high speeds)

2- Settings and clock configurations:


2.1-The question now is how to configure my transfer speed (low or high)?

ANS: if your clock is 6MHz >> you are working with low speed
if your clock is 48MHz >> you are working with high speed

Lets assume that we want to communicate with the high speed


actually the 48MHz clock has some problems:
- Noisy
- Very expensive

Microchip made the solution >> PLL (Phase Locked Loop) circuit which can convert your 4MHz clock to 48MHz clock

1 Eng. Kirollos Magdy William | Faculty of Engineering Ain Shams University


Actually this PLL convert your 4 MHz clock to 96 MHz clock, and then you can divide it by 2 to get your 48MHz

2.2- Settings in mikroC Pro for PIC:


Please note that any small mistake will stop the whole operation
Now, creat your new project in mikroC Pro
and then open Project >> Edit Project

2 Eng. Kirollos Magdy William | Faculty of Engineering Ain Shams University


1- As I said before our PLL circuit should take a 4MHz clock
but what if my clock is 20 MHz?!
So you have make sure that your PLL Prescaler Selection >> Divide by 5 as 20/5 = 4MHz !
for 8MHz you should take the PLL Prescaler >> Dvide by 2 and so
2- As our PLL circuit generates a 96MHz we have to divide it by 2
so make sure that your System Clock Postscaler Selection >> 96MHz PLL Src:/2
and make sure that your USB Clock Selection >> USB clock source comes from the 96MHz PLL divide by 2
3- your MCU Oscillator Frequency is 20MHz
and edit your Oscillator Selection to be HS oscillator (HS)
4- Make sure that your USB Voltage Regulator is Enabled
what is the USB Voltage Regulator?!
It is a 3.3 voltage regulator built in your PIC and its very important for your USB communication
so you have to enable your PIC to generate the 3.3 v or to supply it by yourself externally
but take care, once you enabled your PIC to generate 3.3 v you have to put a cap. Connecting the Vusb pin
to the ground (this cap. Value is 220-470 nf)

3- Descriptor file:

3.1- What is the descriptor file?


This file is very important to make your PIC defined for your PC
the question is, how can my PC recognize for my PIC??!
the answer is that your PIC supports HID protocol
this protocol allows your PC to take some information from your devise and then it will be defined for your PC
information like Vendor ID, Product ID, Manufacture name,.
and no need for any driver (something like your optical USB mouse, it doesnt need any driver)

**NOTE: to communicate with multi slave devices dont depend on the address of every device, why?!!
Because in every time you will connect your slave device to your PC your slave device will take a random address!
So, the fixed information for the device >> VID and PID of the device
so, its prefer to communicate with your slave devices according to deferent VIDs and PIDs not according to deferent
addresses

3.2- How to create my descriptor file?


Now, open your mikroC Pro >> Tools >> HID Terminal >> Descriptor tab

3 Eng. Kirollos Magdy William | Faculty of Engineering Ain Shams University


As you see, you can edit:

- VID >> Vendor ID


- PID >> Product ID
- Input data length (default >> 64 character)
- Output data length (default >> 64 character)
**NOTE: its prefer to make it multiple of 2
e.g.: 8,16,32,64, ..
- Product Name: I made it in my project ROBOCON2013 Board 1

**NOTE: dont forget to make Bus powered option >> true


this give a permission for your PIC to take its power from PC
and also dont forget to choose your right compiler >> mikroC

Now, click on Save descriptor and save it in any place you want desktop for example and you can save it
with any name you like.

4 Eng. Kirollos Magdy William | Faculty of Engineering Ain Shams University


3.3- Include your descriptor file to your project:

4-mikcroC functions and programming:

4.1- HID _Enable:

Prototype: void HID_Enable(char *readbuff, char *writebuff);


Description: Enables USB HID communication.
Parameters: readbuff: Read Buffer.
writebuff: Write Buffer.

Returns Nothing.
Requires Nothing.
Example HID_Enable(&readbuff,&writebuff);
Notes This function needs to be called before using other routines of USB HID Library.

4.2- 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 // retry until success
while(!HID_Read();

5 Eng. Kirollos Magdy William | Faculty of Engineering Ain Shams University


4.3- HID_Write:

Prototype char HID_Write(char *writebuff, char len);


Description Function sends data from Write Buffer writebuff to host.
Parameters writebuff: Write Buffer, same parameter as used in initialization
len: specifies a length of the data to be transmitted.

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 // retry until success
while(!HID_Write(&writebuff,64);

4.4-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 HID_Disable();

4.5- HID_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 void interrupt()


{
USB_Interrupt_Proc();
}

**NOTE: This function is very important for USB bus events, without it nothing will work with you

6 Eng. Kirollos Magdy William | Faculty of Engineering Ain Shams University


5-CODE:
unsigned char readbuff[64] absolute 0x500; // Buffers should be in USB RAM
unsigned char writebuff[64] absolute 0x580;

char cnt;

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

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

while(1)
{
while(!HID_Read());

for(cnt=0;cnt<64;cnt++)
writebuff[cnt]=readbuff[cnt]+1;

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

This code aims to receive any message from your PC and then increment every byte by a one and send it back.

Very Important Notes:


1- you have to save your read and write buffers in the USB RAM Memory
in pic18f4450/2550 (from datasheet) USB RAM Memory (0x500-0x7FF)

7 Eng. Kirollos Magdy William | Faculty of Engineering Ain Shams University


So I made my read buffer location is 0x500 and my write buffer is 0x580

2- You have to send all your 64 character in all cases (even if you didnt use all of these)

You may have some errors like that the mikroC put a carriage return charcater after your message
CR Hex code is 0x0D
and all unused characters will be set as null
and thats mean when the PIC recives the message again
you will see your message then 0x0E (SOLO CHARCATER) and then 0x01 characters until the end!
so I made some modification in the code:

for(cnt=0;cnt<64;cnt++)
{
If (readbuff[cnt] == 0x0D || readbuff[cnt]==0) writebuff[cnt]=0;
Else writebuff[cnt]=readbuff[cnt]+1;
}

3- If you are using a demo version of mikroC the compiler will stop the debugging operation
so you have to crack your version

6-Hardware connections:

Wire Color (USB) Function


RED Vcc
BLACK GND
White DATA-
GREEN DATA+

8 Eng. Kirollos Magdy William | Faculty of Engineering Ain Shams University


7-Simulation using Proteus:
In this part I will tell you how to simulate your project virtually
but actually this simulation didnt work with me except only one time!
and I dont know whats wrong with it!

By the way, setup your Proteus program


2- Start >> Program files >> Proteus >> Install USB drivers
**NOTE: USB drivers just computable with 32 bit versions of windows

3- Connect your hardware in Proteus and as shown

Please note that in this step you have to set your clock with its real value (20MHz for example) and not 48MHz

9 Eng. Kirollos Magdy William | Faculty of Engineering Ain Shams University


8-Connecting our PIC to PC in real:
When you connect you PIC to your PC you may have a new window telling you that its installing your device
let it complete the installation successfully.

Now go to your device manager


Under Human Interface Devices you will find your PIC with a name >>USB Input Device

Right click on it >> Properties >> Details tab

You will find that Property >> Device description

If you changed it to Hardware Ids >> you will see the VID and PID of your PIC as u set before
(In our project VID=1234 and PID=0001)

And if you changed it again to Bus reported device description >> you will see the Product Name of your PIC as you
set before
(In our project Product Name: ROBOCON2013 Board 1)

Now open your mikroC Pro >> tools >> HID Terminal >> Terminal tab >> Choose your device and enjoy!

9- References
-Advanced PIC Microcontroller Projects in mikroC

Thank You!

10 Eng. Kirollos Magdy William | Faculty of Engineering Ain Shams University

You might also like