Need Help in Connecting PIC to USB

Discussion in 'The Projects Forum' started by a.salah, Apr 25, 2009.

  1. a.salah

    Thread Starter Member

    Mar 24, 2008
    11
    0
    Currently i am doing my bachelor project.

    the idea of the project is to control several devices and systems using PC, i will connect the PC to PIC using USB interface

    i already found the circuit and connected it and also the pc (windows xp sp2) identified my circuit as Human Interface device(HID).

    ● The problem now how can i test this connection(how do i send and receive data between both terminals)?


    ● Which program language or platform can i use to have access to usb port in the PC?


    --here is code that i downloaded to the PIC microcontroller

    1-The interrupt part for keeping the connection alive by sending message every 3.3ms

    2-When pic receives (P=??) it is expected to send to PC data on PORTC

    When it receives (P=nT) it is expected to output n in PORTB

    Code ( (Unknown Language)):
    1.  
    2. #include "C:\Program Files\Mikroelektronika\mikroC\Examples\EasyPic4\extra_examples\HID-library\USBdsc.c"
    3. unsigned char Read_buffer[64];
    4. unsigned char Write_buffer[64];
    5. unsigned char num,x;
    6. //
    7. // Timer interrupt service routine
    8. //
    9. void interrupt()
    10. {
    11. HID_InterruptProc(); // Keep alive
    12. TMR0L = 100 ; // Re-load TMR0L
    13. INTCON.TMR0IF = 0 ;  // Reload TMR0
    14. }
    15.  
    16.  
    17. void main()
    18. {
    19.  
    20. ADCON1 = 0xFF; // Set PORTB to digital I/O
    21. TRISB=0; // Set PORTB to outputs
    22. TRISD=0;
    23. TRISC=1;
    24. PORTD=0;
    25. PORTB = 0; // Clear all outputs
    26.  
    27. INTCON=0;
    28. INTCON2=0xF5;
    29. INTCON3=0xC0;
    30. RCON.IPEN=0;
    31. PIE1=0;
    32. PIE2=0;
    33. PIR1=0;
    34. PIR2=0;
    35. //
    36. // Configure TIMER 0 for 3.3ms interrupts. Set prescaler to 256
    37. // and load TMR0L to 100 so that the time interval for timer
    38. // interrupts at 48MHz is 256.(256-100).0.083 = 3.3ms
    39. //
    40. // The timer is in 8-bit mode by default
    41. //
    42. T0CON = 0x47; // Prescaler = 256
    43. TMR0L = 100; // Timer count is 256-156 = 100
    44. INTCON.TMR0IE = 1; // Enable T0IE
    45. T0CON.TMR0ON = 1; // Turn Timer 0 ON
    46. INTCON = 0xE0; // Enable interrupts
    47.  
    48. // Enable USB port
    49.  
    50. Hid_Enable(&Read_buffer, &Write_buffer);
    51. Delay_ms(2000);
    52.  
    53. // Read from the USB port. Number of bytes read is in num
    54.  
    55. for(;;) // do forever
    56. {
    57.  
    58. if(Read_buffer[0]=='P' && Read_buffer[1]=='=' && Read_buffer[2]=='?' && Read_Buffer[3]=='?' ){
    59.  
    60.  
    61. while(!Hid_Write(PORTC, 1)){
    62.                    x = Hid_Write(PORTC, 1);
    63.                    }
    64. }
    65.  
    66.  // Read from the USB port. Number of bytes read is in num
    67.  
    68. if(Read_buffer[0] == 'P' && Read_buffer[1] == '=' && Read_buffer[3] == 'T')
    69. {
    70. if(Read_buffer[2]=='1'){
    71. PORTD=1; // opens gate
    72. }
    73. PORTB = Read_buffer[2];
    74. }
    75. }
    76. Hid_Disable();
    77. }
    78.  
    79.  
    80.  
    81.  
     
  2. StayatHomeElectronics

    Well-Known Member

    Sep 25, 2008
    864
    40
    I have not tried it with a PIC. But there is a driver that lets you use the usb port as you would the serial port. You can use Hyperterminal or something like it to communicate to the board...

    I'll see if I can find it. I have not needed it for quite some time now.
     
  3. CDRIVE

    Senior Member

    Jul 1, 2008
    2,223
    99
    Among other things, I also program in VB6. This is a question that comes up on VB forums often and it usually goes unresolved. Are you certain that your model PIC is intended to communicate with USB and not RS232? Yes, I'm aware that some manufactures are providing PICs with a USB interface but they are not as common as RS232. If your PIC is programmed through RS232 it's a sure thing that it's not USB ready.

    As previously posted, if you don't have a serial port on your PC you can purchase a USB/Serial converter. They are very inexpensive and only require a small driver installation. They look like a dongle with a DB9 on one end and a USB connector on the PC end.

    VB provides MSComm Control 6.0 .OCX to communicate with the serial port, which easily interfaces with microcontrollers. I own 5 or six USB/Serial converters and MSComm treats them just like a serial port. If you're PIC uses the Break command then make certain that the converter you buy will recognize it. My Belkin converter is the only one that doesn't but it's an old model. Belkin may have corrected this by now. ;)
     
Loading...