0
\$\begingroup\$

I'm making a USB HID device using PIC18F2550 using 20 MHz crystal input and using the MikroC PRO C compiler for PIC.

These are my configuration settings:

Enter image description here

unsigned char readbuff[64] absolute 0x500;
unsigned char writebuff[64] absolute 0x540;
char cnt;
char kk;

void interrupt() {
    USB_Interrupt_Proc();
}

void main()
{
    TRISA = 0;
    ADCON1  |= 0x0f;
    CMCON   |= 7;
    HID_Enable(&readbuff, &writebuff);
    while(1)
    {
        /*
            LATA=0;
            delay_ms(200);
            LATA=0xFF;
            delay_ms(200);
        */

        while(!HID_Read())
            ;

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

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

Also included in the project is the file USBdesc.c which can be generated inside the HID terminal of MikroC. But the device is not detected at all...

[UPDATE]

I've tried the older version on MikroC, that is, version 8.2, and I got it working (the HID device gets detected.). But I still can't make it work with the MikroC PRO.

PS.: To make sure the microcontroller is working fine, I loaded a sample hex file for a USB HID mouse onto the microcontroller, and it was working. I just can't figure out what's wrong with my code...

\$\endgroup\$
  • \$\begingroup\$ I'm not familiar with MikroC, but don't you have to do a bit more to declare an ISR than just call it "interrupt"? And once you've declared the ISR correctly, don't you still have to actually enable interrupts somewhere? \$\endgroup\$ – brhans Apr 6 '15 at 13:27
  • \$\begingroup\$ @brhans I've looked everywhere but i can't seem to find out how it can be done... i even looked into the examples for HID bootloaders etc.. but none of them are working for me. \$\endgroup\$ – Suraj Bhawal Apr 6 '15 at 18:12
  • \$\begingroup\$ I was wrong - MikroC regards "interrupt" as special and you don't need anything else to declare an ISR. So have you enabled the USB interrupt in the appropriate PIE register as well as peripheral and global interrupts in INTCON? \$\endgroup\$ – brhans Apr 7 '15 at 16:01
1
+50
\$\begingroup\$

I have made a data acquisition device using PIC18F2550 using MikroC PRO. It seems your configuration is OK. But there are another things to consider. You must insert a 1 kohm resistor between D+ and VCC. Otherwise the PC can not detect it as a high-speed device.

Another important thing is to add a capacitor (10 µf or something like this, electrolytic) to the VUSB pin which is pin number 14 of PIC18F2550. I was in problem for a long period only for this issue. Disable the USB voltage regulator, and then make a trial. You can see this tutorial; it might help.

I don't know whether your device is bus-powered or self-powered. You must declare it in file USBdsc.c:

const char USB_SELF_POWER = 0x80; //When it is bus powered

If your device is self-powered then replace it with

const char USB_SELF_POWER = 0xC0; //When it is self powered

An HID device can operate in two modes, interrupt or polling. From your code, you are using the interrupt method. You must declare it in USBdsc.c by the code

const char USB_TRANSFER_TYPE = 0x03; //0x03 is for interrupt

Enter image description here

Attach the USBDsc.c file to your project. Sometimes it is created by the USB HID Terminal Tool, but not attached - make sure that your USBDsc.c is attached.

\$\endgroup\$
1
\$\begingroup\$

Reading your latest update (about the fact that it works ok with an older version of MikroC), note the following comment from the latest compiler's USB library documentation:

USB

So, if you haven't done so, try recreating the descriptor file as the comment says:

... it is necessary to create a new descriptor file in order to make your project work.

\$\endgroup\$
  • \$\begingroup\$ yes. I've created an new usbdsc.c file from the newer HIDterminal app. Please note that I've not migrated the project from older version of MikroC. but started the project from scratch. \$\endgroup\$ – Suraj Bhawal Apr 7 '15 at 8:37
1
\$\begingroup\$

When your USB connection doesn't Ga-dunk, the problem can be in many places: it can be in your USB stack, it can be in your descriptor, it can be in your hardware connections, or it can be in your own code. Without professional USB protocol tools, this can be a very frustrating problem to run down

Start from a working example from MikroC, and change one thing at a time until it breaks!

As to your code, I don't know the MikroE middleware, but USB code I've used also waits for enumeration before doing anything USB.

Also, many compilers require that you enable your interrupts individually. I don't see where you enable your global or USB interrupt.

There is some example code for the USB library at https://www.mikroe.com/forum/viewtopic.php?f=88&t=60821 , and indeed, it enables interrupts

\$\endgroup\$
  • 1
    \$\begingroup\$ It Doesn't seem like an answer. I've shown u my code. and descriptor is correct as it's directly generated by the HID terminal. and about the Hardware, I can assure u that there is no problem in it. as i've mentioned in the question.. PS. to make sure the microcontroller is working fine. i loaded a sample hex file for USB HID mouse onto the microcontroller and it was working. i just can't figure out whats wrong with my code.. \$\endgroup\$ – Suraj Bhawal Apr 6 '15 at 18:36
  • \$\begingroup\$ So what are the code differences between the sample and what you have written above? \$\endgroup\$ – David Apr 7 '15 at 7:51
  • \$\begingroup\$ @SurajBhawal -- you've shown your component of the code, but there's a whole bunch of middleware with it that isn't feasible to debug, as well as the descriptor. You ran an example hex file, which is great, and shows that all your hardware connections are good. Next step is to start with some compilable code that works, and then change stuff one at a time as you develop your project. As to your code, I don't know the MikroE middleware, but putting HID writes in a while loop doesn't feel right. USB code I've used also waits for enumeration before doing anything USB. \$\endgroup\$ – Scott Seidman Apr 7 '15 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.