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
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:
#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();
}