dimanche 6 mai 2012

USB communication


In this tutorial I'm using a PIC18F4550 with 48MHz crystal to communicate through usb with PC.
If you are using proteus ISIS I'm happy to tell you that there's a wonderful feature which create a virtual usb driver so you don't need any electronics to test your program.

I'm going to show you how to create a project that includes the usb HID (Human interface devices) library. So after this project you will be able to create your own HID devices with your product and vendor IDs.

First start by creating a new Projects with all the settings mentioned above and copy this code which is by the way the example code given by the Help.

unsigned char readbuff[64] absolute 0x500;   // Buffers should be in USB RAM, please consult datasheet
unsigned char writebuff[64] absolute 0x540;

char cnt;
char kk;

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

void main(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(cnt=0;cnt<64;cnt++)
      writebuff[cnt]=readbuff[cnt];

    while(!HID_Write(&writebuff,64))
      ;
  }
}
The code simply send back what he received ^^

Now Tools->HID terminal

Set your VID and PID your Vendor Name and Product Name as you want.
Then select the mikroC radio and save your descriptor file with your project files (not an obligation but recommended :p ).

Go to view-->project manager
right click on the "source" folder and choose "add files to project" to add the descriptor file you already created.

before compiling one last step:
go to edit project and set it like in the picture


Finally Compile your project. It should work fine if you are not using the demo version.


Aucun commentaire:

Enregistrer un commentaire