Software on PIC will come in loop after disconnect USB connection


Closed Thread
Results 1 to 16 of 16

Hybrid View

  1. #1
    Join Date
    Sep 2007
    Posts
    9

    Default Software on PIC will come in loop after disconnect USB connection

    My application is connected via USB to a PC. Within my application I'm using a 18F2550 PIC. A status change of a I/O will be send via USB to PC.

    The code which I'm using for outgoing:

    USBwriteStatus:
    USBService ' Must service USB regularly
    USBOUT 3, USBBUFFER ,CNT_UIT, USBWRITESTATUS
    RETURN

    The problem is when I disconnect the USB cable it seems that the the PIC program commes in an unendless loop of "USBwriteStatus. I have mode some modifications, see below, however this will still not solve the problem.

    USBwriteStatus:
    USBService ' Must service USB regularly
    USBOUT 3, USBBUFFER ,CNT_UIT, exitusbwritestatus
    exitusbwritestatus:
    RETURN

    When I'm taking this SUB out of my application everything works also without USB connection to PC.

    Is there somebody who knows what I'm doing wrong?
    Is it possible to see the status of the USB connection (connect/disconnect) before calling the SUB USBwriteStatus.

  2. #2
    Join Date
    Jul 2003
    Posts
    2,405

    Default

    Hi Ronald,

    There's a very simple solution to this. Assign a single I/O-pin as the USB bus-attach input.

    Here's how;

    ground --100k resistor----PIC input pin ----4.7k resistor----+V from USB connector.

    When the USB cable is plugged in, the input will see +V from the USB port. When the USB
    cable is removed, it will see ground.

    Don't attempt to send/receive any data until you see +V on the USB bus-attach input pin.
    Very simple. Very effective.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  3. #3
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    257

    Default

    Bruce,

    Thats a great tip, (although I havent seen this problem come up before).

    I'm wondering if this would be concidered a fail safe "Design Rule" and should be implemented into all USB PIC based circuits?

    Cheers
    Squib

  4. #4
    Join Date
    Jul 2003
    Posts
    2,405

    Default

    Hi Squib,

    I can't take credit for the idea. It's an option on the Microchip USB development board, and
    in the USB C firmware.

    There's no reason to enable the USB module if it's not attached, so it's not a bad idea to
    add in.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  5. #5
    Join Date
    Sep 2007
    Posts
    9

    Default

    Hi Bruce, thank you for your almost exelent and simple sollution. Almost exelent because it will cost me 1 I/O and I don't have one I/O left. I have made a mistake because after testing this code again it was working.

    USBwriteStatus:
    USBService
    USBOUT 3, USBBUFFER ,CNT_UIT, exitusbwritestatus
    exitusbwritestatus:
    RETURN

    However this sollution has a disadvantage because when the outgoing USBBuffer is full it will also goto "exitusbwritestatus" without sending the information via USB, so some status information from PIC to PC will be lost.

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959

    Default

    You should be able to determine if it's plugged-in or not by monitoring the usb_device_state variable.
    Code:
    usb_device_state   VAR BYTE EXT
    CONFIGURED_STATE   CON EXT
    
    IF (usb_device_state = CONFIGURED_STATE) THEN
        ; OK to send
    ELSE
        ; Detached or Enumerating, don't try to send.
    ENDIF
    DT

Similar Threads

  1. Simple USB Comms Problem
    By awmt102 in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 6th January 2010, 20:17
  2. Using input from Switch to control a loop
    By MrRoboto in forum mel PIC BASIC
    Replies: 9
    Last Post: - 2nd February 2009, 05:02
  3. Reading a slave USB with a pic
    By pcaccia in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 25th October 2008, 12:00
  4. Two USB devices with same software, easyHID
    By Josuetas in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 31st October 2007, 06:19
  5. PIC 2 PIC wireless connection
    By MegaADY in forum General
    Replies: 1
    Last Post: - 24th June 2004, 01:46

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts