CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

18F4550 USB unpredictable behavior

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
rikotech8



Joined: 10 Dec 2011
Posts: 376
Location: Sofiq,Bulgariq

View user's profile Send private message

18F4550 USB unpredictable behavior
PostPosted: Thu Jan 02, 2014 1:39 pm     Reply with quote

Hello!
I am trying to establish steady usb_cdc connection between PC and 18F4550 MCU. I almost succeeded Very Happy .
The problem occurs when I plug the USB connector in (which also supplies the MCU) and some times (randomly) I get the windows message
Quote:
USB Device Not Recognized
. It also disconnect and connect the bus even during continuous work. In other hand I receive the data being send from the MCU, but the connection somehow is not reliable.
Code:

#include <18F4550.h>
#fuses NOMCLR,HSPLL,PUT,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock = 48M)
//#use rs232(baud=9600, xmit=PIN_,rcv=PIN_)

#include <pic18_usb.h>
#include <usb_cdc.h>

#define D_LED PIN_D1

void main()
{
   delay_ms(100);
   usb_init_cs();
   int16 d_time=500;

while (true)
   {
      if(usb_enumerated())
         d_time=100;
      usb_task();
      output_high(D_LED);
      delay_ms(d_time);
      usb_cdc_putc('R');
      output_low(D_LED);
      delay_ms(d_time);
   }
}

Where do I mistake?
Thanks in advance!
_________________
A person who never made a mistake never tried anything new.
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Fri Jan 03, 2014 2:28 am     Reply with quote

Several things:

1) If this device is being powered by the USB bus, there is no point in using usb_init_cs. Just use usb_init instead. The point about the CS version is that this is for devices that have their own power, and need to wake the USB peripheral, and go and do other things while waiting for the USB connection to be made.
2) A common cause for the problem you describe is the capacitor on Vusb. If this is too large, the reference voltage used for the USB bus takes too long to wake up. This should be something like a polyester 0.47uF capacitor. No smaller than 0.22uF, no larger than 1uF.
3) Delays..... Use a hardware timer, and wait for this to expire, rather than using software delays in the main loop. The standard code uses interrupt support, rather than polling (with polling, usb_task must be called every mSec), but the task still has to be called at quite fast intervals. With your long delays in the loop, there is a risk the task is not being called often enough....
4) You are trying to 'putc', before the device may be enumerated. Enumeration only starts when 'init' is called and will take several mSec to complete.

Best Wishes
rikotech8



Joined: 10 Dec 2011
Posts: 376
Location: Sofiq,Bulgariq

View user's profile Send private message

PostPosted: Fri Jan 03, 2014 10:56 am     Reply with quote

Thank you Ttelmah! I modified my software and hardware according to what you reccomended, and now I haven't same problems as I used to. BUT, now, somehow the MCU resets after 133 secs being connected, and can't be enumerated again. Debugged via Terraterm. When I reset (Power reset) the MCU the connection is ok until 133 secs expired.
The code:
Code:

#include <18F4550.h>
#fuses NOMCLR,HSPLL,PUT,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock = 48M)
#include <usb_cdc.h>
#define D_LED_Y PIN_D1
#define D_LED_G PIN_D0

#INT_TIMER0
void TMR0(){
usb_task();

}

void main()
{
   char str[9];
   int16 i=0;
   usb_init_cs();
   SETUP_TIMER_0(T0_DIV_1);

   while(!usb_enumerated())
      {
      output_high(D_LED_Y);
      delay_ms(200);
      usb_task();
      output_low(D_LED_Y);
      delay_ms(200);
      usb_task();
      }
   output_low(D_LED_Y);
   ENABLE_INTERRUPTS(INT_TIMER0);
   ENABLE_INTERRUPTS(GLOBAL);   
while (true)
   {
      delay_ms(1000);
      if(usb_enumerated())
         output_high(D_LED_G);
     i++;
     sprintf(str,"\r%lu sec",i);
     usb_cdc_puts(str);
   }
}


I made a short video example:
http://youtu.be/8XNlOpoLD6o
_________________
A person who never made a mistake never tried anything new.
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Fri Jan 03, 2014 1:15 pm     Reply with quote

I'd suspect it is watch-dogging.
Add NOWDT to the fuses.

A nominal 131secs, is the maximum watchdog timeout.

Best Wishes
rikotech8



Joined: 10 Dec 2011
Posts: 376
Location: Sofiq,Bulgariq

View user's profile Send private message

PostPosted: Fri Jan 03, 2014 2:25 pm     Reply with quote

You are a genius ! Surprised
One more thing - The previous reply, you said to me:
Quote:
there is no point in using usb_init_cs. Just use usb_init instead
but when I am using usb_init instead of usb_init_cs I recieve neither data nor enumeration "TRUE". Anyway I am very grateful for support.
_________________
A person who never made a mistake never tried anything new.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group