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

How to program for device to be used with and without usb

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







How to program for device to be used with and without usb
PostPosted: Wed Mar 05, 2008 7:29 am     Reply with quote

Hi all,

I'm trying to create a device using PIC18F4550 which is able to :
- connect to the computer through USB to perform operations
- and also be used without a connection to the computer (as a stand-alone device)

I believe the the function 'usb_wait_for_enumeration()' runs infinitely until the PIC enumerates with the host. So, I cannot enter this function to the program, right?

I've tried using 'usb_enumerated()' to determine whether it has been enumerated or not. However, i have to add a certain delay before 'usb_enumerated()' or else it will always detect it as a stand-alone device. The rough structure of the program is:


//delay for the device to enumerate
delay_ms(3000);

// USB mode
if( usb_enumerated() == 1 )
{
//further codes here
}

// Stand-alone mode
else if( usb_enumerated() != 1 )
{
//further codes here
}

My question is, is there a more efficient solution other than inserting a delay?
mskala



Joined: 06 Mar 2007
Posts: 100
Location: Massachusetts, USA

View user's profile Send private message

PostPosted: Wed Mar 05, 2008 7:54 am     Reply with quote

I use what's in the CCS examples, a couple 100K resistors from USB power to ground, tapped in the middle and sent to a pin on the PIC. A routine checks that pin for a high to know that you've plugged into USB port.
hyper
Guest







PostPosted: Wed Mar 05, 2008 8:19 am     Reply with quote

I believe you're talking about USB_CONNECTION_SENSE_PIN ?? I have considered that but wouldn't that only determine whether the PIC's supply voltage is turned on? Because VBUS is pin1 of the USB connecter which is also VCC.

However, I have an external battery to power the PIC and if i'm not mistaken, would also cause a high on VBUS (VCC) although the USB is not plugged in? Please correct me if I'm wrong.
mskala



Joined: 06 Mar 2007
Posts: 100
Location: Massachusetts, USA

View user's profile Send private message

PostPosted: Wed Mar 05, 2008 2:01 pm     Reply with quote

Yes, you'll need a diode to isolate USB power from VCC while operating from another power source. I don't have it right in front of me, but it's just a $0.20 Schotky diode.
hyper
Guest







PostPosted: Thu Mar 06, 2008 3:21 am     Reply with quote

Could you please further explain how that would solve the problem? I still don't really get it.

Thanks for the quick replies Smile
Ttelmah
Guest







PostPosted: Thu Mar 06, 2008 3:56 am     Reply with quote

You have two potential voltage 'sources'. Vusb, and the local Vcc. There are two configurations to deal with:
1) You draw power from Vusb if it is available.
2) You only use local power.

In the former case, what you have to do, is connect the Vusb pin to your local power rail, using a low voltage drop diode (either a schottky diode, or a MOSFET gate).

Then in either case, you feed the voltage at the Vusb _pin_, to your 'sense' input via the resistors. The sense input, only sees a 'high', when this pin has voltage on it (doesn't care what the local rail is doing).

Then in your ''main', you have a loop, with tests like:
Code:

   while (true) {
      if (usb_attached()){
         if (USB_IS_OFF) {
            usb_init();
            USB_ON;
         }
      }
      else USB_OFF;
      if (USB_IS_ON) {
         usb_task();
         if (usb_enumerated()) {
            LED=TRUE;
         }
      }
      //rest of main code here
   }

'USB_IS_ON', is a local flag/test, set 'true' by 'USB_ON', and 'false' by USB_OFF.
What then happens, is that if the USB Vusb voltage becomes present, when the USB_IS_OFF (wasn't present before), the USB initialisation code is called. If the USB Vusb is present, and the USB initialisation has occured, the USB_TASK is called. This also handles recovery, if the voltage disappears. In this code, an LED is turned on, when the connection is present, _and_ the system has been enumerated.

Best Wishes
hyper
Guest







PostPosted: Thu Mar 06, 2008 4:24 am     Reply with quote

Hey,

Thanks for the explanation. I will be trying this out as soon as I can get my hands on the schottky diode. Hope it works. Smile
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