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.

18f4550 usb need help in a simple program

Status
Not open for further replies.

sector9

Junior Member level 1
Joined
Oct 22, 2010
Messages
19
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,427
I am using mikroc and this is the code

Code:
unsigned char readbuff[64] absolute 0x500;   // Buffers should be in USB RAM, please consult datasheet
unsigned char writebuff[64] absolute 0x540;

char cnt;
char kk;

void interrupt(){
   USB_Interrupt_Proc();                   // USB servicing is done inside the interrupt
}

void main(void){
  ADCON1 |= 0x0F;                         // Configure all ports with analog function as digital
  CMCON  |= 7;                            // Disable comparators

  HID_Enable(&readbuff,&writebuff);       // Enable HID communication

  while(1){
    while(!HID_Read())
      ;

    for(cnt=0;cnt<64;cnt++)
      writebuff[cnt]=readbuff[cnt];

    while(!HID_Write(&writebuff,64))
      ;
  }
}

USBdsc.c

Code:
const unsigned int USB_VENDOR_ID = 0x1235;
const unsigned int USB_PRODUCT_ID = 0x0001;
const char USB_SELF_POWER = 0x80;            // Self powered 0xC0,  0x80 bus powered
const char USB_MAX_POWER = 50;               // Bus power required in units of 2 mA
const char HID_INPUT_REPORT_BYTES = 64;
const char HID_OUTPUT_REPORT_BYTES = 64;
const char USB_TRANSFER_TYPE = 0x03;         //0x03 Interrupt
const char EP_IN_INTERVAL = 1;
const char EP_OUT_INTERVAL = 1;

const char USB_INTERRUPT = 1;
const char USB_HID_EP = 1;
const char USB_HID_RPT_SIZE = 33;

/* Device Descriptor */
const struct {
    char bLength;               // bLength         - Descriptor size in bytes (12h)
    char bDescriptorType;       // bDescriptorType - The constant DEVICE (01h)
    unsigned int bcdUSB;        // bcdUSB          - USB specification release number (BCD)
    char bDeviceClass;          // bDeviceClass    - Class Code
    char bDeviceSubClass;       // bDeviceSubClass - Subclass code
    char bDeviceProtocol;       // bDeviceProtocol - Protocol code
    char bMaxPacketSize0;       // bMaxPacketSize0 - Maximum packet size for endpoint 0
    unsigned int idVendor;      // idVendor        - Vendor ID
    unsigned int idProduct;     // idProduct       - Product ID
    unsigned int bcdDevice;     // bcdDevice       - Device release number (BCD)
    char iManufacturer;         // iManufacturer   - Index of string descriptor for the manufacturer
    char iProduct;              // iProduct        - Index of string descriptor for the product.
    char iSerialNumber;         // iSerialNumber   - Index of string descriptor for the serial number.
    char bNumConfigurations;    // bNumConfigurations - Number of possible configurations
} device_dsc = {
      0x12,                   // bLength
      0x01,                   // bDescriptorType
      0x0200,                 // bcdUSB
      0x00,                   // bDeviceClass
      0x00,                   // bDeviceSubClass
      0x00,                   // bDeviceProtocol
      8,                      // bMaxPacketSize0
      USB_VENDOR_ID,          // idVendor
      USB_PRODUCT_ID,         // idProduct
      0x0001,                 // bcdDevice
      0x01,                   // iManufacturer
      0x02,                   // iProduct
      0x00,                   // iSerialNumber
      0x01                    // bNumConfigurations
  };

/* Configuration 1 Descriptor */
const char configDescriptor1[]= {
    // Configuration Descriptor
    0x09,                   // bLength             - Descriptor size in bytes
    0x02,                   // bDescriptorType     - The constant CONFIGURATION (02h)
    0x29,0x00,              // wTotalLength        - The number of bytes in the configuration descriptor and all of its subordinate descriptors
    1,                      // bNumInterfaces      - Number of interfaces in the configuration
    1,                      // bConfigurationValue - Identifier for Set Configuration and Get Configuration requests
    0,                      // iConfiguration      - Index of string descriptor for the configuration
    USB_SELF_POWER,         // bmAttributes        - Self/bus power and remote wakeup settings
    USB_MAX_POWER,          // bMaxPower           - Bus power required in units of 2 mA

    // Interface Descriptor
    0x09,                   // bLength - Descriptor size in bytes (09h)
    0x04,                   // bDescriptorType - The constant Interface (04h)
    0,                      // bInterfaceNumber - Number identifying this interface
    0,                      // bAlternateSetting - A number that identifies a descriptor with alternate settings for this bInterfaceNumber.
    2,                      // bNumEndpoint - Number of endpoints supported not counting endpoint zero
    0x03,                   // bInterfaceClass - Class code
    0,                      // bInterfaceSubclass - Subclass code
    0,                      // bInterfaceProtocol - Protocol code
    0,                      // iInterface - Interface string index

    // HID Class-Specific Descriptor
    0x09,                   // bLength - Descriptor size in bytes.
    0x21,                   // bDescriptorType - This descriptor's type: 21h to indicate the HID class.
    0x01,0x01,              // bcdHID - HID specification release number (BCD).
    0x00,                   // bCountryCode - Numeric expression identifying the country for localized hardware (BCD) or 00h.
    1,                      // bNumDescriptors - Number of subordinate report and physical descriptors.
    0x22,                   // bDescriptorType - The type of a class-specific descriptor that follows
    USB_HID_RPT_SIZE,0x00,  // wDescriptorLength - Total length of the descriptor identified above.

    // Endpoint Descriptor
    0x07,                   // bLength - Descriptor size in bytes (07h)
    0x05,                   // bDescriptorType - The constant Endpoint (05h)
    USB_HID_EP | 0x80,      // bEndpointAddress - Endpoint number and direction
    USB_TRANSFER_TYPE,      // bmAttributes - Transfer type and supplementary information    
    0x40,0x00,              // wMaxPacketSize - Maximum packet size supported
    EP_IN_INTERVAL,         // bInterval - Service interval or NAK rate

    // Endpoint Descriptor
    0x07,                   // bLength - Descriptor size in bytes (07h)
    0x05,                   // bDescriptorType - The constant Endpoint (05h)
    USB_HID_EP,             // bEndpointAddress - Endpoint number and direction
    USB_TRANSFER_TYPE,      // bmAttributes - Transfer type and supplementary information
    0x40,0x00,              // wMaxPacketSize - Maximum packet size supported    
    EP_OUT_INTERVAL         // bInterval - Service interval or NAK rate
};

const struct {
  char report[USB_HID_RPT_SIZE];
}hid_rpt_desc =
  {
     {0x06, 0x00, 0xFF,       // Usage Page = 0xFF00 (Vendor Defined Page 1)
      0x09, 0x01,             // Usage (Vendor Usage 1)
      0xA1, 0x01,             // Collection (Application)
  // Input report
      0x19, 0x01,             // Usage Minimum
      0x29, 0x40,             // Usage Maximum
      0x15, 0x00,             // Logical Minimum (data bytes in the report may have minimum value = 0x00)
      0x26, 0xFF, 0x00,       // Logical Maximum (data bytes in the report may have maximum value = 0x00FF = unsigned 255)
      0x75, 0x08,             // Report Size: 8-bit field size
      0x95, HID_INPUT_REPORT_BYTES,// Report Count
      0x81, 0x02,             // Input (Data, Array, Abs)
  // Output report
      0x19, 0x01,             // Usage Minimum
      0x29, 0x40,             // Usage Maximum
      0x75, 0x08,             // Report Size: 8-bit field size
      0x95, HID_OUTPUT_REPORT_BYTES,// Report Count
      0x91, 0x02,             // Output (Data, Array, Abs)
      0xC0}                   // End Collection
  };

//Language code string descriptor
const struct {
  char bLength;
  char bDscType;
  unsigned int string[1];
  } strd1 = {
      4,
      0x03,
      {0x0409}
    };


//Manufacturer string descriptor
const struct{
  char bLength;
  char bDscType;
  unsigned int string[10];
  }strd2={
    22,           //sizeof this descriptor string
    0x03,
    {'s','e','c','t','o','r','I','N','D','2'}
  };

//Product string descriptor
const struct{
  char bLength;
  char bDscType;
  unsigned int string[4];
}strd3={
    10,          //sizeof this descriptor string
    0x03,
    {'g','p','s','2'}
 };

//Array of configuration descriptors
const char* USB_config_dsc_ptr[1];

//Array of string descriptors
const char* USB_string_dsc_ptr[3];

void USB_Init_Desc(){
  USB_config_dsc_ptr[0] = &configDescriptor1;
  USB_string_dsc_ptr[0] = (const char*)&strd1;
  USB_string_dsc_ptr[1] = (const char*)&strd2;
  USB_string_dsc_ptr[2] = (const char*)&strd3;
}
Capture.PNGDSC_0285.jpg


xtal is 20MHZ cap. is 0.47uf
when i plug it in to the usb nothing happens at all ! where did i go wrong ?
thank you in advance :)
 

What should happen according to you? Have you opened mikroC HID terminal and check if it shows your Product name and Vendor name. Did you try sending data from USB HID terminal?
 

It doesnt get detected at all not in device manager nor HID
 

MCLR is disabled and i have tried it with too different chips , i have the Vusb connected to the 0.47 uf cap. and the cap. is connected to ground
 

here is the mikroc project ,thank you a lot for helping me out
 

Attachments

  • stepping.rar
    36.8 KB · Views: 67

It works fine. I simulated it in Proteus. When I ran the simulation it detected the device and installed the driver and mikroe USB HID Terminal showed gps2 and I sent 'A' from Terminal and it sent it back. Maybe there is USB conflict in your system. Download USBDview from nirsoft.com and see if any device is already using the VID and PID you have used. If yes remove it or change VID and PID in yor descriptor file.

98336d1384183370-usbsim.png
 

Attachments

  • stepping rev1.rar
    209.2 KB · Views: 95
  • usbsim.png
    usbsim.png
    131.3 KB · Views: 143
Last edited:
thank you so much and yes it is working on the sim but when i try it on the board just wont work , maybe something wrong with my config bits ? or cap value ?
 

hey its workinggggggggg thankkkkkkkkkkkkkk youuuuuuuuuuuuuuuuuuuu

- - - Updated - - -

i think it was a compiler problem coz when i recompiled it just now it didnt work , then i used your hex file it worked ! in sim when i recompile it gives me error trying to execute illegal opcode
 

I had the same problem like sector9, but when i load files form jayanth.devarayanadurga from post #9 the PIC18f4550 start working. The computer start recognize it like USB HID like I expect. Thanks jayanth.devarayanadurga !
 

Which version of mikroC PRO PIC Compiler are you using?
 

Unfortunately i still have problem :( I use MikroC PRO for PIC Ver. 6.0.0. The files from post #9 works fine I can simulate with Proteus and on real PIC18F4550 board, but when i compile the projekt with MicroC the compilation process finished successfully but the hex file changes from 17k to 1k and the projekt stop working. I did not change the files from post #9 just open the projekt, I also start MikroC like administratot I use Win7. I suspect library paths, so can someone share library paths for MicroC. I also reinstall MicroC few time with registry cleaner. This is my log:

0 1 mikroCPIC1618.exe -MSF -DBG -pP18F4550 -DL -O11111114 -fo48 -N"C:\Users\Computer\Downloads\stepping rev1\stepping\stepping.mcppi" -SP"C:\Program Files\mikroC PRO for PIC\defs\" -SP"C:\Program Files\mikroC PRO for PIC\uses\P18\" -SP"C:\Users\Computer\Downloads\stepping rev1\stepping\" -SP"C:\Program Files\Mikroelektronika\mikroC PRO for PIC 6.0.0\defs\" -SP"C:\Program Files\Mikroelektronika\mikroC PRO for PIC 6.0.0\uses\P18\" -SP"C:\Users\System Administrator\Desktop\stepping\stepping\" -SP"C:\Users\Public\Documents\Mikroelektronika\mikroC PRO for PIC\defs\" -SP"C:\Users\Public\Documents\Mikroelektronika\mikroC PRO for PIC\uses\P18\" -SP"D:\stepping\" -SP"D:\stepping\dec\" -SP"C:\Users\System Administrator\Desktop\stepping\stepping\dec\" -SP"C:\Users\Computer\Downloads\stepping rev1\stepping\dec\" "stepping.c" "USBdsc.c" "__Lib_Math.mcl" "__Lib_MathDouble.mcl" "__Lib_System.mcl" "__Lib_Delays.mcl" "__Lib_CType.mcl" "__Lib_CString.mcl" "__Lib_CStdlib.mcl" "__Lib_CMath.mcl" "__Lib_MemManager.mcl" "__Lib_Conversions.mcl" "__Lib_Sprintf.mcl" "__Lib_PrintOut.mcl" "__Lib_Sprinti.mcl" "__Lib_Sprintl.mcl" "__Lib_Time.mcl" "__Lib_Trigonometry.mcl" "__Lib_Button.mcl" "__Lib_Keypad4x4.mcl" "__Lib_Manchester.mcl" "__Lib_OneWire.mcl" "__Lib_PS2.mcl" "__Lib_Sound.mcl" "__Lib_SoftI2C.mcl" "__Lib_SoftSPI.mcl" "__Lib_SoftUART.mcl" "__Lib_ADC_A_D.mcl" "__Lib_EEPROM_256.mcl" "__Lib_FLASH_w32_e64.mcl" "__Lib_I2C_b10.mcl" "__Lib_PWM_c21.mcl" "__Lib_SPI_b10c7.mcl" "__Lib_UART_c67.mcl" "__Lib_USB_genHID.mcl" "__Lib_PortExpander.mcl" "__Lib_CANSPI.mcl" "__Lib_CF.mcl" "__Lib_CFFat16.mcl" "__Lib_GlcdFonts.mcl" "__Lib_Glcd.mcl" "__Lib_LcdConsts.mcl" "__Lib_Lcd.mcl" "__Lib_Mmc.mcl" "__Lib_MmcFat16.mcl" "__Lib_MmcFat16Constants.mcl" "__Lib_RS485.mcl" "__Lib_S1D13700.mcl" "__Lib_T6963C.mcl" "__Lib_SPIGlcd.mcl" "__Lib_SPILcd.mcl" "__Lib_SPILcd8.mcl" "__Lib_SPIT6963C.mcl" "__Lib_EthEnc28j60.mcl" "__Lib_TFT.mcl" "__Lib_TFT_Defs.mcl" "__Lib_TFT_16bit.mcl" "__Lib_TFT_16bit_Defs.mcl" "__Lib_TouchPanel_TFT.mcl" "__Lib_EthEnc24j600.mcl" "__Lib_TouchPanel.mcl"
0 1501 Specified search path does not exist: 'C:\Program Files\Mikroelektronika\mikroC PRO for PIC 6.0.0\defs'
0 1501 Specified search path does not exist: 'C:\Program Files\Mikroelektronika\mikroC PRO for PIC 6.0.0\uses\P18'
0 1501 Specified search path does not exist: 'C:\Users\System Administrator\Desktop\stepping\stepping'
0 1501 Specified search path does not exist: 'C:\Users\Public\Documents\Mikroelektronika\mikroC PRO for PIC\defs'
0 1501 Specified search path does not exist: 'C:\Users\Public\Documents\Mikroelektronika\mikroC PRO for PIC\uses\P18'
0 1501 Specified search path does not exist: 'D:\stepping'
0 1501 Specified search path does not exist: 'D:\stepping\dec'
0 1501 Specified search path does not exist: 'C:\Users\System Administrator\Desktop\stepping\stepping\dec'
0 1139 Available RAM: 2027 [bytes], Available ROM: 32768 [bytes]
0 122 Compilation Started P18F4550.c
1610 123 Compiled Successfully P18F4550.c
0 122 Compilation Started __Lib_Delays.c
172 123 Compiled Successfully __Lib_Delays.c
0 122 Compilation Started __Lib_MemManager.c
34 1508 Implicit conversion of int to ptr __Lib_MemManager.c
231 123 Compiled Successfully __Lib_MemManager.c
0 122 Compilation Started __Lib_GlcdFonts.c
1207 123 Compiled Successfully __Lib_GlcdFonts.c
0 122 Compilation Started __lib_mmcfat16.h
71 123 Compiled Successfully __Lib_MmcFat16Constants.c
0 122 Compilation Started __Lib_TFT_Defs.c
1892 123 Compiled Successfully __Lib_TFT_Defs.c
0 122 Compilation Started __Lib_TFT_16bit_Defs.c
1776 123 Compiled Successfully __Lib_TFT_16bit_Defs.c
0 126 All files Preprocessed in 78 ms
0 122 Compilation Started stepping.c
27 123 Compiled Successfully stepping.c
0 122 Compilation Started USBdsc.c
166 123 Compiled Successfully USBdsc.c
0 127 All files Compiled in 93 ms
0 1144 Used RAM (bytes): 277 (14%) Free RAM (bytes): 1750 (86%) Used RAM (bytes): 277 (14%) Free RAM (bytes): 1750 (86%)
0 1144 Used ROM (bytes): 6470 (20%) Free ROM (bytes): 26298 (80%) Used ROM (bytes): 6470 (20%) Free ROM (bytes): 26298 (80%)
0 125 Project Linked Successfully stepping.mcppi
0 128 Linked in 687 ms
0 129 Project 'stepping.mcppi' completed: 2169 ms
0 103 Finished successfully: 19 Apr 2014, 20:11:19 stepping.mcppi
 
Last edited:

Zip and attache your mikroC Project files. Are you using licensed version of mikroC?
 

I found that my problem is "Demo limit" problem.
 

then what should be the X-tal frequency? and controller frequency?

I have 20MHz and 4MHz crystals only.

The crystal must be 20MHz it work good (without extra wires) on breadboard, tested by me without capacitors. You must fill 48Mhz in project settings tab in MickroC compiler.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top