![]() |
mikroc usb tutorial |
USB communication is better than serial communication. Hardware interfacing is very easier than Rs 232 . In USB 1.0 communication Low Speed is-1.5Mbit/second & High Speed is -12Mbit/second . The upgrade version provides more speed than older version. USB-3.0 has higher speed, 5Gbit/second and it's called super speed. For more information you can visit this page >> Wikipedia Usb.
In this tutorial I will use pic18f2550 microcontroller & it is very popular for usb. Now you can ask a question, how do we get our MCU to work with high speed or low seed ?
At 6MHz clock we will get low speed and at 48MHz Clock we will get High speed. In this tutorial we will try to make higher speed connection. We are not going to use 48MHz clock, because it is very noisy. We will try another technique instead of it. Microchip company provides internal PLL( Phase Locked Loop ) circuit in some of specific products and pic18f2550 has internal PLL Circuit. Now look at the picture :
![]() |
PLL Circuit |
Now let's create a project in Proteus 8.
Proteus 8 Circuit :
Step 1:![]() |
How to Create Project in Proteus _1 |
![]() |
How to Create Project in Proteus 8_2 |
Step3 :
![]() |
How to Create Project in Proteus 8_3 |
![]() |
How to find parts in Proteus 8 |
![]() |
How to find parts in Proteus 8 |
![]() |
Usb Interfacing With PIC Microcontroller Circuit |
Now let's create a project in MikroC.
MikroC Code :
Step 1:![]() |
Create New Project in MikroC_1 |
![]() |
Create New Project in MikroC_2 |
![]() |
Create New Project in MikroC_3 |
![]() |
Include All Library |
![]() |
Editing MikroC project settings_1 |
![]() |
USB Communication With PIC Microcontroller |
1. As we know PLL circuit takes input 4MHz clock. So we have to divide 12MHz by 3 so that we can get 4MHz clock. If we use 4MHz we have no need this part .
2 & 3 . We are using USB 1.0 and it's High Speed clock 48MHz. So we have to devide it by 2.
4 . Here we are using 12MHz Crystal clock and the oscillator selection should be HS Oscillator.
5 . We have to enable voltage regulator. Basically it is an internal 3.3 voltage regulator of pic18f2550. If we enable this, it is required to connect vusb pin with a 220 nf capacitor.
#Source Code :
unsigned char receivedata[64] absolute 0x500; // <--Variable Declaration
unsigned char senddata[64] absolute 0x580;
int i=0;
void Interrupt(){
USB_Interrupt_Proc(); //<--Interrupt function
}
void main() //<--The Main Function
{
HID_Enable(&receivedata,&senddata); //<--To Enable HID
while(1){ //<-- Infinity LOOP
while(!HID_Read()); //<--for reading
for(i=0;i<64;i++){
senddata[i]=receivedata[i]; //<--Sending received data to senddata[i] variable
}
while(!HID_Write(&senddata,64)); //<--for writting
}
}
1. unsigned char receivedata[64] absolute 0x500;
unsigned char senddata[64] absolute 0x580;
Here we have created character type two array variable senddata & receivedata. During USB Communication we must have to keep all data in USB RAM. PIC 18F2550 microcontroller's USB RAM memory address begins with 0x500 and ends with 0x7F. That's why, we kept the receivedata[64] in 0x500 location and senddata[64] in 0x580.
2.
void Interrupt(){
USB_Interrupt_Proc();}
void Interrupt() is one user defined function which contains USB_Interrupt_Proc() function inside . Interrupt means restriction for something. When interrupt is enabled, microcontrller doesn't perform main function. It will perform only interrupt function or ISR(interrupt service routine). After clearing the flag main method works.
3. HID_Enable(&receivedata,&senddata);
This function will initialize USB. It locates receivedata variable and send data variable.
Note : HID means "Human Interface Device"
4. char HID_Read();
This function receives data. If receiving is failed, it returns 0. Otherwise it returns character .
while(!HID_Read());
It means, this function tries to receive data until data are being received .char HID_Write();
This function receive data. If receiving is failed, it returns 0. Otherwise it returns the numbers of data .while(!HID_Write());
It means, this function tries to send data until data are being sent.
5.for(i=0;i<64;i++>
senddata[i]=receivedata[i];
}
This loop will run from 0 to 63 and will keeps the received data to senddata variable from receivedata variable.
#Descriptor File Addition
Descriptor File : This file will make your pc understand about device's informations. LikeManufacture Name, Product ID, Vendor ID etc.
Now i will show you. How to create a Descriptor file .
Step 7:
![]() |
Create Descriptor File_1 |
![]() |
Create Descriptor FIle_2 |
Adding descriptor file with MikroC Project .
![]() |
Adding Descriptor File |
Step 11:
Step 12:
![]() |
How to upload hex file in proteus_1 |
![]() |
How to upload hex file in proteus_2 |
![]() |
How to upload hex file in proteus_3 |
In proteus Simulation we need to install virtual usb .
![]() |
Install Virtual USB in proteus |
![]() |
Run Project |
Now go to the Proteus Circuit and Just Run the project .
Output:
Now go to the Tool >>HID Terminal >> click on Terminal .
![]() |
HID Terminal of MikroC |
![]() |
HID Terminal Output |