HID USB Communication (Pic18f2550 With PC) : ROBOCON 2013 Competition
HID USB Communication (Pic18f2550 With PC) : ROBOCON 2013 Competition
HID USB Communication (Pic18f2550 With PC) : ROBOCON 2013 Competition
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)
Actually this PLL convert your 4 MHz clock to 96 MHz clock, and then you can divide it by 2 to get your 48MHz
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)
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.2- HID_Read:
Prototype Description Parameters Returns Requires Example char HID_Read(void); Receives message from host and stores it in the Read Buffer. None. If the data reading has failed, the function returns 0. Otherwise, it returns number of characters received from the host. USB HID needs to be enabled before using this function. // retry until success while(!HID_Read();
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 Requires Example If the data transmitting has failed, the function returns 0. Otherwise, it returns number of transmitted bytes. USB HID needs to be enabled before using this function. // retry until success while(!HID_Write(&writebuff,64);
4.4-HID_Disable:
Prototype Description Parameters Returns Requires Example void HID_Disable(void); Disables USB HID communication. None. Nothing. USB HID needs to be enabled before using this function. 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 Requires Example Nothing. Nothing. 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(); }
This code aims to receive any message from your PC and then increment every byte by a one and send it back.
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:
Please note that in this step you have to set your clock with its real value (20MHz for example) and not 48MHz
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