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 support@ccsinfo.com

How to set sleep mode 18f2550 using USB after PC shut down?

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



Joined: 20 Aug 2012
Posts: 4

View user's profile Send private message

How to set sleep mode 18f2550 using USB after PC shut down?
PostPosted: Mon Aug 20, 2012 8:32 pm     Reply with quote

Hi guys.

I am trying to create a USB device and I want to set sleep mode 18f2550 after PC shut down.

My device is always blink LED after PC shut down.
I want to detect PC shutdown or PC sleep mode.

I tried to write this code, but it doesn't work.
Code:

if(usb_enumerated())             //
{

}
else // it doesn't move this line when PC shut down
{
   output_a(0b00000001);
}

How do I write?

Thank you.
Ttelmah



Joined: 11 Mar 2010
Posts: 19347

View user's profile Send private message

PostPosted: Tue Aug 21, 2012 2:08 am     Reply with quote

Do you have the USB attached connection made?.
If so, use 'usb_attached', rather than usb_enumerated.
Problem is that 'enumeration', is a 'one shot' event. Once enumerated, a device is considered to remain enumerated, until something disconnects it.
Provided the usb connector you are using is not one of the ones that remains powered when the system is in standby (usually the two usb connectors furthest from the slots), then 'usb_attached' will go false.
For sleep mode, you need to look at the bit UCON_SUSPND. This is set, when the PC sends a suspend command. When this goes true, you can then sleep, but _must_ have the USB interrupt enabled, so the routines can respond to the 'active' interrupt, when the PC asks the device to wake up.

Best Wishes
misumi



Joined: 20 Aug 2012
Posts: 4

View user's profile Send private message

PostPosted: Tue Aug 21, 2012 5:53 am     Reply with quote

Thank you for your reply!

I tried to include '#define USB_CON_SENSE_PIN PIN_B3' line.
I quoted this line from Examples folder 'ex_usb_serial.c'.

I can't understand what below comment say.
Code:

///only the 18F4550 development kit has this pin


Couldn't I use '#define USB_CON_SENSE_PIN PIN_B3' line?

And I tried to use 'usb_attached()' method, but don't work.
Code:

#include <18F2550.h>
#device ADC = 10

#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV3,VREGEN, NOMCLR
#use delay(clock=24000000) // 24MHz

//// MSSP I2C モード使用宣言
#use i2c(MASTER, SDA=PIN_B0, SCL=PIN_B1, FAST, FORCE_HW)
#use fast_io(A)
#use fast_io(c)


/////////////////////////////////////////////////////////////////////////////
//
// If you are using a USB connection sense pin, define it here.  If you are
// not using connection sense, comment out this line.  Without connection
// sense you will not know if the device gets disconnected.
//       (connection sense should look like this:
//                             100k
//            VBUS-----+----/\/\/\/\/\----- (I/O PIN ON PIC)
//                     |
//                     +----/\/\/\/\/\-----GND
//                             100k
//        (where VBUS is pin1 of the USB connector)
//
/////////////////////////////////////////////////////////////////////////////
///only the 18F4550 development kit has this pin
#define USB_CON_SENSE_PIN PIN_B3

#DEFINE USB_HID_DEVICE  TRUE

#define USB_EP1_TX_ENABLE  USB_ENABLE_INTERRUPT   //turn on EP1 for IN bulk/interrupt transfers
#define USB_EP1_TX_SIZE 20

#define USB_EP1_RX_ENABLE  USB_ENABLE_INTERRUPT   //turn on EP1 for IN bulk/interrupt transfers
#define USB_EP1_RX_SIZE 8

#include <pic18_usb.h>
#include <usb_desc_hid 8-byte.h>               //USB Configuration and Device descriptors for this UBS device
#include <usb.c>                                 //handles usb setup tokens and get descriptor reports

// ==========================================================================
// proto type
void Calc_button_assign();
void i2c_send();
void i2c_recv();
void Create_SndBuf();

// ==========================================================================
//// global
const int8 SndBufCnt = 2;
char SndBuf[SndBufCnt];        // I2C送信バッファ
char RcvBuf;                   // I2C受信バッファ 1byte

int8 out_data[USB_EP1_TX_SIZE];                // USB 送信データ用バッファ
int8 in_data [USB_EP1_RX_SIZE];          // USB受信データ用バッファ
int8  Eje0=0;
int8 Eje1=0;
int8 Eje2=0;
int8 Eje3=0;

void main() {
   
   set_tris_a(0b11111100);        // 0-5:IN
   //set_tris_b(0b11111100);        // 0,1:I2C  2-7:IN
   set_tris_c(0b11111111);        // 0-2,6,7:IN  4,5:USB
   set_tris_e(0b00001000);        // 3:IN
   
   port_b_pullups(false);               
   
   // Don't use CCP
   setup_ccp1(CCP_OFF);
   setup_ccp2(CCP_OFF);
   
   output_float(PIN_B0);         //I2C pin float
   output_float(PIN_B1);         //I2C pin float
                    
   usb_init_cs();
   
   while (TRUE)
   {
      
      if (usb_attached())
      {
         output_a(0b00000011);   
         
         usb_task();
         
         // USB送信処理
         if(usb_enumerated())             //si esta configurado el Joysick
         {
            delay_ms(10);
         }
      }
      else
      {
         output_a(0b00000010);   
      }
   }
}


I hope you don’t mind answering my question.
Thank you.
misumi



Joined: 20 Aug 2012
Posts: 4

View user's profile Send private message

PostPosted: Tue Aug 21, 2012 6:11 am     Reply with quote

And I have more question.

I checked VBUS voltage when PC shut down, and It still provide 5V.
Is windows7's USB port always providing 5V when PC shut down?

thank you.
sseidman



Joined: 14 Mar 2005
Posts: 159

View user's profile Send private message

PostPosted: Tue Aug 21, 2012 6:54 am     Reply with quote

Many newer motherboards leave power on at least some USB ports to allow people to charge devices when the computer is off. It has nothing to do with any operating system. I'm not sure if this can be disabled in the BIOS or not
Ttelmah



Joined: 11 Mar 2010
Posts: 19347

View user's profile Send private message

PostPosted: Tue Aug 21, 2012 7:01 am     Reply with quote

Yes. Normally power is permanently on at least two of the ports. Usually they were the ones furthest from the motherboard slots. These were meant to be used by the mouse/modem/keyboard, allowing you to have 'wake up on keyboard', and 'wake on dialup' functions. If you plug into these slots, the USB device remains powered, and enumerated, since it is then assumed to be a device offering such abilities....
The number of such connectors, is fixed on some motherboards, but adjustable on others (BIOS).
I made this point in the fourth line of my original post.

Best Wishes
sseidman



Joined: 14 Mar 2005
Posts: 159

View user's profile Send private message

PostPosted: Tue Aug 21, 2012 10:42 am     Reply with quote

This is a bit different, I think, from any sleep or hibernate functionality. These USB connectors stay hot when the motherboard is actually OFF. Well- more accurately, whatever state the full shutdown command leaves the Mobo in.
Ttelmah



Joined: 11 Mar 2010
Posts: 19347

View user's profile Send private message

PostPosted: Tue Aug 21, 2012 1:47 pm     Reply with quote

Modern PSU's, have a small supply, required to give between 1 and 2A at 5v, that, that runs these sockets. You can normally program the 'state' that the machine goes to for shutdown, and for hibernate. The default now is for 'shutdown' to still leave these powered. Many modern keyboards offer a 'power on' button (distinct from the hibernate wake-up), which uses this, BIOS HID drivers, and wakes the machine from a full shutdown. A couple of Lenovo machines I've been using recently, have a power button on the keyboard that does this. The function only works with their own keyboard, and if you look at the USB devices, when this is attached, you see it has a second device, described as 'Lenovo USB power button', in device manager.
A you say, this is distinct from hibernate, but it can be disabled in the BIOS.

Best Wishes
misumi



Joined: 20 Aug 2012
Posts: 4

View user's profile Send private message

PostPosted: Wed Aug 22, 2012 8:24 am     Reply with quote

Ttelmah, sseidman, Thank you very much!

I can understand the modern PC's behavior.
My PC doesn't have the USB power setting in BIOS.

but it's OK. I will try to create Windows Program, that control my PIC device.

I really appreciate all your help.
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