Professional Documents
Culture Documents
Usb Pic18f PDF
Usb Pic18f PDF
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:
**NOTE: our PIC support USB 1.0 (with low and high speeds)
ANS: if your clock is 6MHz >> you are working with low speed
if your clock is 48MHz >> you are working with high speed
Microchip made the solution >> PLL (Phase Locked Loop) circuit which can convert your 4MHz clock to 48MHz clock
3- Descriptor file:
**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
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.
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:
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:
4.5- HID_USB_Interrupt_Proc
Description This routine is used for servicing various USB bus events. Should be called inside USB interrupt routine.
Parameters None.
Returns Nothing.
Requires Nothing.
**NOTE: This function is very important for USB bus events, without it nothing will work with you
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.
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:
Please note that in this step you have to set your clock with its real value (20MHz for example) and not 48MHz
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!