Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

USB (HID) communication problem b/w pic18f4550 and PC

Status
Not open for further replies.

Hamid Ullah Jan

Newbie level 3
Joined
Dec 8, 2010
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,409
Hi Everyone!

I want to communicate between pic18f4550 and pc through usb communication, i have written the code for pic and when i connect it to the pc, the pc should detect and install the drivers for this. but pc does not even detect it. i m posting the code here so please help me what might be the problem.


void main()
{
// unsigned char ii=0;
unsigned int prog_data=0;
//===================usb===================================
unsigned char i, ch,m;
unsigned char index;
unsigned char buff[70];
//=========================================================
// ANSEL=0x00; //All pins as digital i/O
ADCON1=0x0F; //All pins of PORTA as digital i/O
PORTA=0x00;
TRISA=0xE0;
PORTB=0x00;
TRISB=0x00;//4D;//0xCD;
PORTC=0x00;
TRISC=0x00;

INTCON = 0; // Disable GIE, PEIE, TMR0IE,INT0IE,RBIE
INTCON2 = 0xF5;
INTCON3 = 0xC0;
RCON.IPEN = 0; // Disable Priority Levels on interrupts
PIE1 = 0;
PIE2 = 0;
PIR1 = 0;
PIR2 = 0;
//------------------------------
Pwm_Init(2000); // Initialize PWM module
Pwm_Change_Duty(51); // Duty cycle 20%
Pwm_Start(); // Start PWM
//------------------------------
prg_mode_on();
delay_ms(300);

HID_Enable(&userRD_buffer, &userWR_buffer);
Delay_ms(1000);
Delay_ms(1000);

while(1)
{
PORTB.F7=~PORTB.F7;
}


will waiting for your response please.
 

what is the HID device you want to get detected as??
did u load all the necessary files?
Or may be to check the communication first, you should try mounting the HID codes from microchip applications library.
 

thanks shatruddha,

my HID device is pic18f4550.
i am posting my full code below (this code is in pic18f4550).
and please tell me what other necessary files are needed?

Reply please

void load_data_cmd(unsigned char cmd,unsigned int data); // load data for program memory command
unsigned int read_data_cmd(unsigned char cmd);
void inc_addr(unsigned char cmd);
unsigned char match_DevID();
void any_cmd(unsigned char cmd,unsigned int dly_ms);
void ms_delay(unsigned int dly);
//--------------------------------------------------------
void bulk_erase_PIC18Fxx2_8();
//--------------------------------------------------------
void send_byte(unsigned char var11);
void erase_chip();
void write_cmd(unsigned char byte);
void write_payload(unsigned int pload);
void set_tblptr(unsigned long TBLPTR);
void prg_mode_on();
void prg_mode_off();
unsigned char read_byte();
void init_write();
void continue_write();
void write_cmd1(unsigned char byte);

void init_read();
void continue_read();
//void resend_read();
void read_configuration();

void write_Cfg_mem(unsigned char addr,unsigned char byt); //algo=2
void write_Cfg_mem3(unsigned char addr,unsigned char byt); //algo=3
void config_cfg_mem();
void write_configuration();

unsigned char flag_urec=0;

//----------comm------------
void exe_cmd_erase();
void exe_cmd_read();
void exe_cmd_write();

unsigned char rec_byte=0;
unsigned char cnt=0;
unsigned char buffer[70];
unsigned long TBL_PTR=0;
unsigned int code_addr=0;
unsigned char buffer_tx[70];
unsigned char device_serz=0;

unsigned int id=0;
unsigned char algo=0;
//=================================================================================
//=================================================================================
//============================USB=====================================================
unsigned char k;
unsigned char userWR_buffer[64], userRD_buffer[64];
void empty_buffer();
void send_frame(void);
//**************************************************************************************************
// Main Interrupt Routine
//**************************************************************************************************
void interrupt()
{
HID_InterruptProc();
// LED=~LED;
}
//**************************************************************************************************

void main()
{
// unsigned char ii=0;
unsigned int prog_data=0;
//===================usb===================================
unsigned char i, ch,m;
unsigned char index;
unsigned char buff[70];
//=========================================================
// ANSEL=0x00; //All pins as digital i/O
ADCON1=0x0F; //All pins of PORTA as digital i/O
PORTA=0x00;
TRISA=0xE0;
PORTB=0x00;
TRISB=0x00;//4D;//0xCD;
PORTC=0x00;
TRISC=0x00;

INTCON = 0; // Disable GIE, PEIE, TMR0IE,INT0IE,RBIE
INTCON2 = 0xF5;
INTCON3 = 0xC0;
RCON.IPEN = 0; // Disable Priority Levels on interrupts
PIE1 = 0;
PIE2 = 0;
PIR1 = 0;
PIR2 = 0;
//------------------------------
Pwm_Init(2000); // Initialize PWM module
Pwm_Change_Duty(51); // Duty cycle 20%
Pwm_Start(); // Start PWM
//------------------------------
prg_mode_on();
delay_ms(300);

HID_Enable(&userRD_buffer, &userWR_buffer);
Delay_ms(1000);
Delay_ms(1000);

while(1)
{
PORTB.F7=~PORTB.F7;
}
//======================usb=====================================================
 

It seems to me that you haven't created your descriptor file. If I am not mistaken you are using MikroC. Go to Tools -> HID Terminal -> Descriptor, change the vendor name and product name to anything you want. SAve descriptor and add it to your project if it is not added automatically. Your project should now work.
 

thanks Hisham.D,

ya, u r right i m using mikroC. Here is my descriptor file and it has been added

but still not detecting. what might be the problem?

//******************************************************************************
//
// File Version 1.01
//
//******************************************************************************

#include "Definit.h"
//#include "VARs.h"

//******************************************************************************
// The number of bytes in each report,
// calculated from Report Size and Report Count in the report descriptor
//******************************************************************************
unsigned char const HID_INPUT_REPORT_BYTES = 45;//2
unsigned char const HID_OUTPUT_REPORT_BYTES = 1;

unsigned char const HID_FEATURE_REPORT_BYTES = 2;
//******************************************************************************
// Byte constants
//******************************************************************************
unsigned char const NUM_ENDPOINTS = 2;
unsigned char const ConfigDescr_wTotalLength = USB_CONFIG_DESCRIPTOR_LEN + USB_INTERF_DESCRIPTOR_LEN + USB_HID_DESCRIPTOR_LEN + (NUM_ENDPOINTS * USB_ENDP_DESCRIPTOR_LEN);
unsigned char const HID_ReportDesc_len = 47;

unsigned char const Low_HID_ReportDesc_len = HID_ReportDesc_len;
unsigned char const High_HID_ReportDesc_len = HID_ReportDesc_len >> 8;

unsigned char const Low_HID_PACKET_SIZE = HID_PACKET_SIZE;
unsigned char const High_HID_PACKET_SIZE = HID_PACKET_SIZE >> 8;



//******************************************************************************
// Descriptor Tables
//******************************************************************************
unsigned char const DescTables[USB_DEVICE_DESCRIPTOR_ALL_LEN*2] = {

// Device Descriptor
USB_DEVICE_DESCRIPTOR_LEN, 0, // bLength - Length of Device descriptor (always 0x12)
USB_DEVICE_DESCRIPTOR_TYPE, 0, // bDescriptorType - 1 = DEVICE descriptor
0x00, 0, // bcdUSB - USB revision 2.00 (low byte)
0x02, 0, // (high byte)
0x00, 0, // bDeviceClass - Zero means each interface operates independently (class code in the interface descriptor)
0x00, 0, // bDeviceSubClass
0x00, 0, // bDeviceProtocol
EP0_PACKET_SIZE, 0, // bMaxPacketSize0 - maximum size of a data packet for a control transfer over EP0
0x34, 0, // idVendor - Vendor ID (low byte)
0x12, 0, // (high byte)
0x01, 0, // idProduct - Product ID (low byte)
0x00, 0, // (high byte)
0x01, 0, // bcdDevice - ( low byte)
0x00, 0, // (high byte)
0x01, 0, // iManufacturer - String1
0x02, 0, // iProduct - String2
0x00, 0, // iSerialNumber - ( None )
0x01, 0, // bNumConfigurations - 1

// Configuration Descriptor
USB_CONFIG_DESCRIPTOR_LEN, 0, // bLength - Length of Configuration descriptor (always 0x09)
USB_CONFIG_DESCRIPTOR_TYPE, 0, // bDescriptorType - 2 = CONFIGURATION descriptor
ConfigDescr_wTotalLength, 0, // wTotalLength - Total length of this config. descriptor plus the interface and endpoint descriptors that are part of the configuration.
0x00, 0, // ( high byte)
0x01, 0, // bNumInterfaces - Number of interfaces
0x01, 0, // bConfigurationValue - Configuration Value
0x00, 0, // iConfiguration - String Index for this configuration ( None )
0xA0, 0, // bmAttributes - attributes - "Bus powered" and "Remote wakeup"
50, 0, // MaxPower - bus-powered draws 50*2 mA from the bus.

// Interface Descriptor
USB_INTERF_DESCRIPTOR_LEN, 0, // bLength - Length of Interface descriptor (always 0x09)
USB_INTERFACE_DESCRIPTOR_TYPE, 0, // bDescriptorType - 4 = INTERFACE descriptor
0x00, 0, // bInterfaceNumber - Number of interface, 0 based array
0x00, 0, // bAlternateSetting - Alternate setting
NUM_ENDPOINTS, 0, // bNumEndPoints - Number of endpoints used in this interface
0x03, 0, // bInterfaceClass - assigned by the USB
0x00, 0, // bInterfaceSubClass - Not A boot device
0x00, 0, // bInterfaceProtocol - none
0x00, 0, // iInterface - Index to string descriptor that describes this interface ( None )

// HID Descriptor
USB_HID_DESCRIPTOR_LEN, 0, // bLength - Length of HID descriptor (always 0x09)
USB_HID_DESCRIPTOR_TYPE, 0, // bDescriptorType - 0x21 = HID descriptor
0x01, 0, // HID class release number (1.01)
0x01, 0,
0x00, 0, // Localized country code (none)
0x01, 0, // # of HID class descriptor to follow (1)
0x22, 0, // Report descriptor type (HID)
Low_HID_ReportDesc_len, 0,
High_HID_ReportDesc_len, 0,

// EP1_RX Descriptor
USB_ENDP_DESCRIPTOR_LEN, 0, // bLength - length of descriptor (always 0x07)
USB_ENDPOINT_DESCRIPTOR_TYPE, 0, // bDescriptorType - 5 = ENDPOINT descriptor
0x81, 0, // bEndpointAddress - In, EP1
USB_ENDPOINT_TYPE_INTERRUPT, 0, // bmAttributes - Endpoint Type - Interrupt
Low_HID_PACKET_SIZE, 0, // wMaxPacketSize - max packet size - low order byte
High_HID_PACKET_SIZE, 0, // - max packet size - high order byte
1, 0, // bInterval - polling interval (1 ms)

// EP1_TX Descriptor
USB_ENDP_DESCRIPTOR_LEN, 0, // bLength - length of descriptor (always 0x07)
USB_ENDPOINT_DESCRIPTOR_TYPE, 0, // bDescriptorType - 5 = ENDPOINT descriptor
0x01, 0, // bEndpointAddress - Out, EP1
USB_ENDPOINT_TYPE_INTERRUPT, 0, // bmAttributes - Endpoint Type - Interrupt
Low_HID_PACKET_SIZE, 0, // wMaxPacketSize - max packet size - low order byte
High_HID_PACKET_SIZE, 0, // - max packet size - high order byte
1, 0, // bInterval - polling interval (1 ms)

// HID_Report Descriptor
0x06, 0, // USAGE_PAGE (Vendor Defined)
0xA0, 0,
0xFF, 0,
0x09, 0, // USAGE ID (Vendor Usage 1)
0x01, 0,
0xA1, 0, // COLLECTION (Application)
0x01, 0,
// The Input report
0x09, 0, // USAGE ID - Vendor defined
0x03, 0,
0x15, 0, // LOGICAL_MINIMUM (0)
0x00, 0,
0x26, 0, // LOGICAL_MAXIMUM (255)
0x00, 0,
0xFF, 0,
0x75, 0, // REPORT_SIZE (8)
0x08, 0,
0x95, 0, // REPORT_COUNT (2)
HID_INPUT_REPORT_BYTES, 0,
0x81, 0, // INPUT (Data,Var,Abs)
0x02, 0,
// The Output report
0x09, 0, // USAGE ID - Vendor defined
0x04, 0,
0x15, 0, // LOGICAL_MINIMUM (0)
0x00, 0,
0x26, 0, // LOGICAL_MAXIMUM (255)
0x00, 0,
0xFF, 0,
0x75, 0, // REPORT_SIZE (8)
0x08, 0,
0x95, 0, // REPORT_COUNT (2)
HID_OUTPUT_REPORT_BYTES, 0,
0x91, 0, // OUTPUT (Data,Var,Abs)
0x02, 0,
// The Feature report
0x09, 0, // USAGE ID - Vendor defined
0x05, 0,
0x15, 0, // LOGICAL_MINIMUM (0)
0x00, 0,
0x26, 0, // LOGICAL_MAXIMUM (255)
0x00, 0,
0xFF, 0,
0x75, 0, // REPORT_SIZE (8)
0x08, 0,
0x95, 0, // REPORT_COUNT (2)
HID_FEATURE_REPORT_BYTES, 0,
0xB1, 0, // FEATURE (Data,Var,Abs)
0x02, 0,
// End Collection
0xC0, 0 // END_COLLECTION
};
//******************************************************************************
unsigned char const LangIDDescr[8] = {
0x04, 0,
USB_STRING_DESCRIPTOR_TYPE, 0,
0x09, 0, // LangID (0x0409) - Low
0x04, 0 // - High
};
//******************************************************************************
unsigned char const ManufacturerDescr[52] = {
26, 0,
USB_STRING_DESCRIPTOR_TYPE, 0,
};
//******************************************************************************
unsigned char const ProductDescr[36] = {
};
//******************************************************************************
unsigned char const StrUnknownDescr[4] = {
2, 0,
USB_STRING_DESCRIPTOR_TYPE, 0
};
//******************************************************************************







//******************************************************************************
// Initialization Function
//******************************************************************************
void InitUSBdsc()
{
Byte_tmp_0[0] = NUM_ENDPOINTS;
Byte_tmp_0[0] = ConfigDescr_wTotalLength;
Byte_tmp_0[0] = HID_ReportDesc_len;
Byte_tmp_0[0] = Low_HID_ReportDesc_len;
Byte_tmp_0[0] = High_HID_ReportDesc_len;
Byte_tmp_0[0] = Low_HID_PACKET_SIZE;
Byte_tmp_0[0] = High_HID_PACKET_SIZE;


DescTables;

LangIDDescr;
ManufacturerDescr;
ProductDescr;
StrUnknownDescr;

}
//******************************************************************************
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top