Need Help in Connecting PIC to USB

Thread Starter

a.salah

Joined Mar 24, 2008
11
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

Rich (BB code):
#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();
}
 
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.
 

CDRIVE

Joined Jul 1, 2008
2,219
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. ;)
 
Top