Question: How do I detect when a self-powered USB device is plugged in?
The best way to detect, when the USB cable of self-powered device is plugged in, is to monitor VBUS with one of the I/O port pins. VBUS can be either monitored in the main loop or another popular method is to setup an interrupt timer to a fixed interval, and in the interrupt ISR look at the port pin (VBUS). You can then take the appropriate action depending on the state of VBUS. For more details on implementing VBUS monitoring, refer to application note “Monitoring the EZ-USB FX2LP™ VBUS - AN15813”.