MIKROC PRO AND 18F4550 USB WORKING CODE AND CONFIG NEEDED?

General discussion on mikroC PRO for PIC.
Author
Message
inspirational4real
Posts: 59
Joined: 03 Jun 2009 03:48
Location: benin city, Nigeria

MIKROC PRO AND 18F4550 USB WORKING CODE AND CONFIG NEEDED?

#1 Post by inspirational4real » 12 Aug 2009 23:37

HI ALL, I HAVE BEEN BREAD BOARDING FOR ALMOST 48 HOURS NOW COS I WANTED TO EXHAUST MYSELF BEFORE LETTING OTHERS HELP ME. I AM HAVING PROBLEM MAKING THE EXAMPLE CODE IN MIKROC PRO FOR USB HID TO WORK. IT SEEMS NOT TO RESPOND AT ALL AND THOUGH RELATED MOST ANSWERS FOR USB HID ARE GIVEN IN RESPECT TO MIKROC AND MOST PEOPLE HURRIEDLY JUST REFERENCE THE POPULAR THREAD http://www.mikroe.com/forum/viewtopic.php?t=10646 FOR CONFIG FOR MIKROC. SO I DONT EXPECT THAT AS A REFERENCE AND I WANT EVERY REPLY TO BE STRICKLY MIKROC PRO. HOW DO I SET CONFIG ASSUMING I AM USING 4MHZ XTAL AND WANT TO RUN USB AND ALSO RUN THE CPU AT 48MHZ. I GET CONFUSED WITH THE XT:XTPLL-USBXT STUFF, DONT KNOW THE IMPLICATION OF THE USBXT IN IT,IF YOU HAVE BREAD BOARDED THIS TO WORK WHAT ARE THE DIFFICULTIES AND WHAT ARE THE LIKELY SOLUTION. IT SEEMS WHEN MOST PEOPLE SUCCEED WITH A PROBLEM, THEY NEVER COME HERE TO POST HOW THEY WENT ABOUT IT. IF I SUCCEED I AM HOPING TO BRING A COMPREHENSIVE EXAMPLE ON USB FUNCTIONALITY IN MIKROC PRO ONLY. PLEASE TAKE NOTE ONLY MIKROC PRO NOT MIKRO C. THANKS FOR READING AND COMPLYING WITH THE RULES. :cry:

inspirational4real
Posts: 59
Joined: 03 Jun 2009 03:48
Location: benin city, Nigeria

#2 Post by inspirational4real » 27 Aug 2009 23:43

OK EVERYONE, I AM JUST REPLYING TO MY POST, I HAVE SUCCEEDED IN GETTING A CODE THAT WORKS FOR USB ONLY THAT I AM AT THE STAGE WHERE WINDOWS SHOW USB DEVICE NOT RECOGNIZED, AT ANOTHER TIME IT IS RECOGNIZED AND LOADS THE HID DRIVER AND THEN AGAIN IT DISPALYS UNKNOWN DEVICE, I HAVE BEEN WORKING ON THIS FOR ABOUT A WEEK NOW TRYING OUT DIFFERENT CODES FOR USB HID. I AM WONDERING WHAT COULD BE THE PROBLEM. OR IF ANYONE HAS A BETTER USB CODE, YOU CAN POST IT HERE, SO THAT I CAN TEST MY USB HID FUNCTIONALITY, MY CODE IS GIVEN BELOW.

Code: Select all

unsigned short m,k;

unsigned char userWR_buffer[64];
unsigned char userRD_buffer[64];



void interrupt()
{
  Hid_InterruptProc;
}


void Init_Main()
{

  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;

   ADCON1 = 0x07;

  TRISA = 0xFF;
   TRISB = 0x00;
   TRISC = 0xFF;
   TRISD = 0x00;
   TRISE = 0x07;

   LATA = 0;
   LATB = 0;
   LATC = 0;
   LATD = 0;
   LATE = 0;
   // Clear user RAM
  // Banks [00 .. 07] ( 8 x 256 = 2048 Bytes )

}


void main()
{
  Init_Main();
  Hid_Enable(&userRD_buffer, &userWR_buffer);
  delay_ms(1000);
  delay_ms(1000);
  m=0;
  do {
    for (k = 0; k <= 63; k++) {
      // Prepare send buffer
      userWR_buffer[k] = m;
      }
      // Send the number via USB
      while(!Hid_Write(&userWR_buffer, 64));
      m++;
      } while (m<=63);

  Hid_Disable();
}

RacerX10
Posts: 209
Joined: 02 Aug 2007 23:19

#3 Post by RacerX10 » 28 Aug 2009 03:41

Are you using internal oscillator or do you have a crystal on the chip ? I've heard some of those serial functions are very timing sensitive.

yt7pwr
Posts: 89
Joined: 22 Jul 2008 08:50
Location: Serbia

#4 Post by yt7pwr » 28 Aug 2009 07:35

Can you give us more info about hardware and software in project.

p.s. turn off Caps Lock on keyboard.

inspirational4real
Posts: 59
Joined: 03 Jun 2009 03:48
Location: benin city, Nigeria

#5 Post by inspirational4real » 29 Aug 2009 23:12

thanks everyone, i am happy that i have finally got you guys to respond to my post, i just want a sample code that works with the usb stuff and i can take it from there, for my test i am using a 4mhz xtal oscillator and have 470nf capacitor across vusb.i can get the 3.3v from vusb pin. so i set the config to not divide so the pll can get 4mhz input, i am running at 48mhz for cpu and usb pheripherals, i am using a programmer called top2049 where you have to set the config fuses from the programmer software, so to the best of my knowledge i cant tell if the code has problem, but sometimes windows recognize it and then later it says unknown device. i just want you guys to advice me on the problems you faced or have faced before when working with usb hid protocol, i am so fustrated that i was begining to think the mikroc library may not be consistent. so my only thinking now is that i should use a 20mhz xtal oscillator and then use pll divide by 5 as input. but i will appreciate any functional usb code that is working, so i can try it out myself.

yt7pwr
Posts: 89
Joined: 22 Jul 2008 08:50
Location: Serbia

#6 Post by yt7pwr » 31 Aug 2009 07:30

Please clarify:after windows recognize your device how long program work and does sending&receiving is OK?
Another way to test configuration settings is to use Delay(1000) library and check it with some LED( this should give you 1s period ).
p.s. what is MicroC compiler version you use?

inspirational4real
Posts: 59
Joined: 03 Jun 2009 03:48
Location: benin city, Nigeria

#7 Post by inspirational4real » 31 Aug 2009 23:43

Thanks for your reply, now the device is not recognized at all. i have intially read your previous post in mikroc forum and tried to correct for most things you suggested before coming here. i use mikroc pro v2.15. i was able to get the pic to run at 48mhz by using phase lock loop, this i confirmed by using 48mhz in the project settings but running from a 4mhz clock source. i was able to get the blinking of one seconds period. before using the pll it used to last for 12s on and off. so i think i know how to use the pll correctly as my code run at 48mhz, but what i dont get is whether the usb pheripheral is functioning at all. so i can get a sample usb code that is working. that will just be fine. by the way i am using breadboard, do you think this is a problem, but i dont think i should face problem since sometimes it says usb device found. Lastly i observed that trying to debug the examples hid in the library and manual, i found some discrepancies. i will like you to take a good look at them and see that they are different. i have read your previous post in which you mentioned that if the buffer size is not equal to size of data being sent or received, the usb hid does not work. This is the example from the help file in the software and it does not work at all

Code: Select all

unsigned short m, k;
unsigned short userRD_buffer[64];
unsigned short userWR_buffer[64];

void interrupt() {
  asm CALL _Hid_InterruptProc
  asm nop
}

void Init_Main() {
  // Disable all interrupts
  // Disable GIE, PEIE, TMR0IE, INT0IE,RBIE
  INTCON = 0;
  INTCON2 = 0xF5;
  INTCON3 = 0xC0;
  // Disable Priority Levels on interrupts
  RCON.IPEN = 0;
  PIE1 = 0;
  PIE2 = 0;
  PIR1 = 0;
  PIR2 = 0;

  // Configure all ports with analog function as digital
  ADCON1 |= 0x0F;

  // Ports Configuration
  TRISA = 0;
  TRISB = 0;
  TRISC = 0xFF;
  TRISD = 0xFF;
  TRISE = 0x07;

  LATA = 0;
  LATB = 0;
  LATC = 0;
  LATD = 0;
  LATE = 0;

  // Clear user RAM
  // Banks [00 .. 07] ( 8 x 256 = 2048 Bytes )
  asm {
    LFSR     FSR0, 0x000
    MOVLW    0x08
    CLRF     POSTINC0, 0
    CPFSEQ   FSR0H, 0
    BRA      $ - 2
  }

  // Timer 0
  T0CON  = 0x07;
  TMR0H  = (65536-156) >> 8;
  TMR0L  = (65536-156) & 0xFF;
  INTCON.T0IE = 1;            // Enable T0IE
  T0CON.TMR0ON = 1;
}


/** Main Program Routine **/

void main() {
  Init_Main();
  Hid_Enable(&userRD_buffer, &userWR_buffer);

  do {
    for (k = 0; k < 255; k++) {
      // Prepare send buffer
      userWR_buffer[0] = k;

      // Send the number via USB
      Hid_Write(&userWR_buffer, 1);
    }
  } while (1);

  Hid_Disable();
}

p.erasmus
Posts: 3391
Joined: 05 Mar 2009 10:28

#8 Post by p.erasmus » 01 Sep 2009 07:50

your code

Code: Select all

void main() { 
  Init_Main(); 
  Hid_Enable(&userRD_buffer, &userWR_buffer); 

  do { 
    for (k = 0; k < 255; k++) { 
      // Prepare send buffer 
      userWR_buffer[0] = k; 

      // Send the number via USB 
      Hid_Write(&userWR_buffer, 1); 
    } 
  } while (1); 

  Hid_Disable(); 
}
In my humble opinion you are over flowing the Buffer as you are pushing data out as fast as the micro is running
use a small delay to give windows a chance to empty the buffer

Code: Select all

void main() { 
  Init_Main(); 
  Hid_Enable(&userRD_buffer, &userWR_buffer); 

  do { 
    for (k = 0; k < 255; k++) { 
      // Prepare send buffer 
      userWR_buffer[0] = k; 

      // Send the number via USB 
      Hid_Write(&userWR_buffer, 1);
      delay_ms(10) // slow Transactions down
    } 
  } while (1); 

  Hid_Disable(); 
}
P.Erasmus
Saratov,Russia
--------------------------------------------------------------

inspirational4real
Posts: 59
Joined: 03 Jun 2009 03:48
Location: benin city, Nigeria

#9 Post by inspirational4real » 01 Sep 2009 22:30

thanks p erasmus, i will try your code tomorrow and post my feedback here

yt7pwr
Posts: 89
Joined: 22 Jul 2008 08:50
Location: Serbia

#10 Post by yt7pwr » 02 Sep 2009 09:05

If you define userRD and userWR buffer by 64 do same thing in usb descriptor file.After that you should send data like this:

Code: Select all

while(!Hid_Write(&userWR_buffer, 64);
This is blocking code and if USB dont work at all your program will hang :(
Another way to debug USB stack is to put something like this:

Code: Select all

void interrupt() { 
  LED =~LED;
  asm CALL _Hid_InterruptProc 
  asm nop 
} 
This way you can track if interrupt occur and how often.

And finaly erase code:

Code: Select all

  // Clear user RAM 
  // Banks [00 .. 07] ( 8 x 256 = 2048 Bytes ) 
  asm { 
    LFSR     FSR0, 0x000 
    MOVLW    0x08 
    CLRF     POSTINC0, 0 
    CPFSEQ   FSR0H, 0 
    BRA      $ - 2 
  } 
or modify it.For me this code hangs PIC.Unfortunatly I dont have net at home and valid licence key for MikroE C at work.Tomorrow will try to send more info.Hope this will help you :D

inspirational4real
Posts: 59
Joined: 03 Jun 2009 03:48
Location: benin city, Nigeria

#11 Post by inspirational4real » 03 Sep 2009 03:43

thanks very much i will try it out and give you feedback, by the way i hope using a breadboard does not in any way affect usb functionality, since the pll help generate the 48mhz, i dont think stray inductance and capacitance from the breadboard will affect it.

yt7pwr
Posts: 89
Joined: 22 Jul 2008 08:50
Location: Serbia

#12 Post by yt7pwr » 03 Sep 2009 09:11

In mikroC 8.2 this code work:

Code: Select all

  asm {
    LFSR     FSR0, 0x000
    MOVLW    0x08
    CLRF     POSTINC0, 0
    CPFSEQ   FSR0H, 0
    BRA      $ -2
    }
but in mikroC PRO not :(
Try this:

Code: Select all

  asm {
    LFSR     FSR0, 0x000
    MOVLW    0x08
q:
    CLRF     POSTINC0, 0
    CPFSEQ   FSR0H, 0
    BRA      q
    }

inspirational4real
Posts: 59
Joined: 03 Jun 2009 03:48
Location: benin city, Nigeria

THANKS EVERYONE

#13 Post by inspirational4real » 22 Sep 2009 23:24

Ok, After a whole lot of efforts and furstration, i finally got my usb hid to work. i even went further to write a software that collects the data sent my the microcontroller through usb. so i intend to implement other things though hid. but i have learnt some valuable lessons when working with usb hid.

1. DO NOT IGNORE THE DECOUPLING CAPACITOR for the microcontroller. i have always got away without using this capacitor, but for usb, before you figure out this is the cause of your problem, you must have given up on the whole experiment, thinking Mikroe are the cause of your problem. i used a 0.47uf capacitor close to vdd and vss. this solved most of my problem.

2. some usb cable do not work at all, and again it is the shorter the cable the better. normal usb printer cable may and may not work depending on the quality of the cable. the best for me was a short usb cable for a usb to serial adaptor. short and strong. it was perfect. All my usb hid device that did not work before worked with this device.

3. I doubt if bread board is the way to go for usb, better solder everything to the veroboard or pcb and try to be as careful as when dealing with radio frequency circuit in parts placement and jumper wire lenght if you are not using pcb. I never succeeded using breadboard or project board as some people will call it.



4. the best value of user write buffer and user read buffer is 64, for some technical reasons, this is the best to use, don't try to say other values will work, of course other values will work, but you don't want to face the problems they may cause if this is what makes your code not work.

5. the code below is working perfect and get count of 1 to 255 on the textbox of the software i wrote to receive data from it. So the only thing that will make it not work is if you don't get the configuration fuse right and your descriptor file set properly, or better still you fail to heed the advice above

Code: Select all

unsigned short m, k;
unsigned short userRD_buffer[64];
unsigned short userWR_buffer[64];

void interrupt() {
  asm CALL _Hid_InterruptProc
  asm nop
}

void Init_Main() {
  // Disable all interrupts
  // Disable GIE, PEIE, TMR0IE, INT0IE,RBIE
  INTCON = 0;
  INTCON2 = 0xF5;
  INTCON3 = 0xC0;
  // Disable Priority Levels on interrupts
  RCON.IPEN = 0;
  PIE1 = 0;
  PIE2 = 0;
  PIR1 = 0;
  PIR2 = 0;

  // Configure all ports with analog function as digital
  ADCON1 |= 0x0F;

  // Ports Configuration
  TRISA = 0;
  TRISB = 0;
  TRISC = 0xFF;
  TRISD = 0xFF;
  TRISE = 0x07;

  LATA = 0;
  LATB = 0;
  LATC = 0;
  LATD = 0;
  LATE = 0;

  // Clear user RAM
  // Banks [00 .. 07] ( 8 x 256 = 2048 Bytes )

}


/** Main Program Routine **/

void main() {
  Init_Main();
  Hid_Enable(&userRD_buffer, &userWR_buffer);

  do {
    for (k = 0; k < 255; k++) {
      // Prepare send buffer
      userWR_buffer[0] = k;

      // Send the number via USB
      while(!Hid_Write(&userWR_buffer, 64));
    }
  } while (1);

  Hid_Disable();
}

VCC
Posts: 463
Joined: 08 Jun 2009 18:31
Location: Romania

Re: THANKS EVERYONE

#14 Post by VCC » 23 Sep 2009 07:13

inspirational4real wrote:3. I doubt if bread board is the way to go for usb, better solder everything to the veroboard or pcb and try to be as careful as when dealing with radio frequency circuit in parts placement and jumper wire lenght if you are not using pcb. I never succeeded using breadboard or project board as some people will call it.
Hi, I use only breabords when prototyping an USB circuit. I works every time. I don't know if your breadboard comes with a metal plate, but it should. This is some sort of ground plane to be glued on the bottom of your breadboard. When working with PICs put as much capacitors as you can on the power lines. The capacitors must be of good quality e.g. tantalum types for "high" capacities (no more than 100uF) and can be ceramic for lower capacities. Another important thing about capacitors is to put different types of capacities for as many frequencies as possible. The lower capacity capacitors must be closer to PIC than high ones. If you look at the schematic from mikroE USB example in mikroC, you may find that there are two 100nF capacitors instead of one 220nF for the USB power pin. Two capacitors connected in parallel give a twice smaller inductance, so it is better to use them like this. Put a 100uF capacitor and a 100nF one on your USB power lines if you power your breadboard from USB because much noise comes from computer. You said that a short USB cable is better than a long one. I agree, and no matter how short it is, try to place your board as far as possible from computer, again because of electromagnetic noise.
Just tell me if all these will help and make your breadboard work :D

VCC
Posts: 463
Joined: 08 Jun 2009 18:31
Location: Romania

Re: THANKS EVERYONE

#15 Post by VCC » 23 Sep 2009 07:37

VCC wrote:
inspirational4real wrote:3. I doubt if bread board is the way to go for usb, better solder everything to the veroboard or pcb and try to be as careful as when dealing with radio frequency circuit in parts placement and jumper wire lenght if you are not using pcb. I never succeeded using breadboard or project board as some people will call it.
When working with PICs put as much capacitors as you can on the power lines.
Do not put too much high capacity capacitors because the voltage raising rate would be too slow and if you power your bread board from your computer, too much capacity can reset the computer. So put at most 5 to 6 100uF capacitors as the highest capacity values on your breadboard. :D

Post Reply

Return to “mikroC PRO for PIC General”