CrazyEngineers
  • Help Needed in Connecting PIC to PC via USB

    mosh11

    Member

    Updated: Oct 26, 2024
    Views: 1.1K
    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

    #include "C:\Program Files\Mikroelektronika\mikroC\Examples\EasyPic4\extra_examples\HID-library\USBdsc.c"
    unsigned char Read_buffer[64];
    unsigned char Write_buffer[64];
    unsigned char num,x;
    //
    // Timer interrupt service routine
    //
    void interrupt()
    {
    HID_InterruptProc(); // Keep alive
    TMR0L = 100 ; // Re-load TMR0L
    INTCON.TMR0IF = 0 ;  // Reload TMR0
    }
    
    
    void main()
    {
    
    ADCON1 = 0xFF; // Set PORTB to digital I/O
    TRISB=0; // Set PORTB to outputs
    TRISD=0;
    TRISC=1;
    PORTD=0;
    PORTB = 0; // Clear all outputs
    
    INTCON=0;
    INTCON2=0xF5;
    INTCON3=0xC0;
    RCON.IPEN=0;
    PIE1=0;
    PIE2=0;
    PIR1=0;
    PIR2=0;
    //
    // Configure TIMER 0 for 3.3ms interrupts. Set prescaler to 256
    // and load TMR0L to 100 so that the time interval for timer
    // interrupts at 48MHz is 256.(256-100).0.083 = 3.3ms
    //
    // The timer is in 8-bit mode by default
    //
    T0CON = 0x47; // Prescaler = 256
    TMR0L = 100; // Timer count is 256-156 = 100
    INTCON.TMR0IE = 1; // Enable T0IE
    T0CON.TMR0ON = 1; // Turn Timer 0 ON
    INTCON = 0xE0; // Enable interrupts
    
    // Enable USB port
    
    Hid_Enable(&Read_buffer, &Write_buffer);
    Delay_ms(2000);
    
    // Read from the USB port. Number of bytes read is in num
    
    for(;;) // do forever
    {
    
    if(Read_buffer[0]=='P' && Read_buffer[1]=='=' && Read_buffer[2]=='?' && Read_Buffer[3]=='?' ){
    
    
    while(!Hid_Write(PORTC, 1)){
                       x = Hid_Write(PORTC, 1);
                       }
    }
    
     // Read from the USB port. Number of bytes read is in num
    
    if(Read_buffer[0] == 'P' && Read_buffer[1] == '=' && Read_buffer[3] == 'T')
    {
    if(Read_buffer[2]=='1'){
    PORTD=1; // opens gate
    }
    PORTB = Read_buffer[2];
    }
    }
    Hid_Disable();
    }
    
    
    
    
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Harshad Italiya

    MemberApr 25, 2009

    I think C language is good platform for this project
    Are you sure? This action cannot be undone.
    Cancel
  • Harshad Italiya

    MemberMay 1, 2009

    Take a look here for interfacing Circuit.
    #-Link-Snipped-#
    Are you sure? This action cannot be undone.
    Cancel
  • mohit30goel

    MemberMay 1, 2009

    mosh11
    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

    #include "C:\Program Files\Mikroelektronika\mikroC\Examples\EasyPic4\extra_examples\HID-library\USBdsc.c"
    unsigned char Read_buffer[64];
    unsigned char Write_buffer[64];
    unsigned char num,x;
    //
    // Timer interrupt service routine
    //
    void interrupt()
    {
    HID_InterruptProc(); // Keep alive
    TMR0L = 100 ; // Re-load TMR0L
    INTCON.TMR0IF = 0 ;  // Reload TMR0
    }
     
     
    void main()
    {
     
    ADCON1 = 0xFF; // Set PORTB to digital I/O
    TRISB=0; // Set PORTB to outputs
    TRISD=0;
    TRISC=1;
    PORTD=0;
    PORTB = 0; // Clear all outputs
     
    INTCON=0;
    INTCON2=0xF5;
    INTCON3=0xC0;
    RCON.IPEN=0;
    PIE1=0;
    PIE2=0;
    PIR1=0;
    PIR2=0;
    //
    // Configure TIMER 0 for 3.3ms interrupts. Set prescaler to 256
    // and load TMR0L to 100 so that the time interval for timer
    // interrupts at 48MHz is 256.(256-100).0.083 = 3.3ms
    //
    // The timer is in 8-bit mode by default
    //
    T0CON = 0x47; // Prescaler = 256
    TMR0L = 100; // Timer count is 256-156 = 100
    INTCON.TMR0IE = 1; // Enable T0IE
    T0CON.TMR0ON = 1; // Turn Timer 0 ON
    INTCON = 0xE0; // Enable interrupts
     
    // Enable USB port
     
    Hid_Enable(&Read_buffer, &Write_buffer);
    Delay_ms(2000);
     
    // Read from the USB port. Number of bytes read is in num
     
    for(;;) // do forever
    {
     
    if(Read_buffer[0]=='P' && Read_buffer[1]=='=' && Read_buffer[2]=='?' && Read_Buffer[3]=='?' ){
     
     
    while(!Hid_Write(PORTC, 1)){
                       x = Hid_Write(PORTC, 1);
                       }
    }
     
     // Read from the USB port. Number of bytes read is in num
     
    if(Read_buffer[0] == 'P' && Read_buffer[1] == '=' && Read_buffer[3] == 'T')
    {
    if(Read_buffer[2]=='1'){
    PORTD=1; // opens gate
    }
    PORTB = Read_buffer[2];
    }
    }
    Hid_Disable();
    }
     
     
     
    


    Its not a much bigger problem man....? u can test ur connections over making a locan lan panel b/w no. of computers u have..u can also try for the php server method .....that can also be beneficial 4 u???
    Are you sure? This action cannot be undone.
    Cancel
  • debu

    MemberMay 2, 2009

    @mosh11: <a href="https://debusinnovations.blogspot.com/2008/02/usb-101.html" target="_blank" rel="nofollow noopener noreferrer">Debu's Innovations: USB-101</a> should help you. It explains how to write both PC side and Device side USB code, meant for beginners.

    Hope this helps,

    Regards,

    Debu 😀
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register