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 sleep after 2 minutes inactivity

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



Joined: 04 Apr 2005
Posts: 63

View user's profile Send private message Send e-mail

how to sleep after 2 minutes inactivity
PostPosted: Thu Jun 16, 2005 2:23 pm     Reply with quote

16F628A
RB1: LED
RB0: Pull-up with Push button
normal operation: LED blinks

Question:
How can make PIC go to sleep if push button has not been pressed for 2 minutes (2 minutes idle)?
AND how do I wakeup the PIC when the push button is pressed?

same code please ... Shocked
valemike
Guest







PostPosted: Thu Jun 16, 2005 4:29 pm     Reply with quote

Code:

#INT_TIMER1                        // This function is called every time
void clock_isr()                   // timer 1 overflows (65535->0), which is
{                                   // approximately 19 times per second for

    if (GLOBAL_keypad_inactivity_timer > 0)
    {
        GLOBAL_keypad_inactivity_timer--;
    }
}

...

void main(void)
{
...
   while(1)
   {
...
       if (!GLOBAL_keypad_inactivity_timer)
     {
        set a flag that tells you that you should go to sleep.
     }
...
       // if your inactivity flag is set, then go to sleep here i guess.
   }
}


Say i get 19 interrupts per second.
There are 19 * 60 interrupts per minute
There are 2 minutes.
So 2 * 19 * 60 = 228.

set GLOBAL_keypad_inactivity_timer to 228 when you want to start the inactivity counter. If there is activity, then you have to reset it back to 228.

-Wake up from sleep during an edge transition of RB0. Read the manual.
- Use time delays (e.g. delay_ms()) to blink your LED,

I've shown the pseudocode and steps. i guess the full implementation is left as an assignment. (or do an exhaustive google search )

-Mike Twisted Evil
valemike
Guest







PostPosted: Thu Jun 16, 2005 4:31 pm     Reply with quote

228 should be 2280. Embarassed
jahan



Joined: 04 Apr 2005
Posts: 63

View user's profile Send private message Send e-mail

and how do I wakeup from RB0?
PostPosted: Thu Jun 16, 2005 4:46 pm     Reply with quote

Thankx Mike,
I get the idea; Now how do I wake the PIC by lowering RB0?

Thankx again
valemike
Guest







PostPosted: Thu Jun 16, 2005 4:52 pm     Reply with quote

When RB0 goes low, and you have falling edge interrupts enabled (#int_ext), then the PIC will wake up.
jahan



Joined: 04 Apr 2005
Posts: 63

View user's profile Send private message Send e-mail

int_ext ??
PostPosted: Thu Jun 16, 2005 4:55 pm     Reply with quote

when RB0 does interrupt the PIC, do you know if the program starts from beginning ? or continues from next statment after sleep?
valemike
Guest







PostPosted: Thu Jun 16, 2005 6:08 pm     Reply with quote

It starts at the next statement after sleep.

I believe you are advised to put a NOOP in the statement after sleep.
delay_cycles(1); ought to do the trick.

I recently wrote a program using sleep in assembly,and i put the noop in there. Not sure what to do in CCS.
jds-pic



Joined: 17 Sep 2003
Posts: 205

View user's profile Send private message

PostPosted: Fri Jun 17, 2005 1:06 am     Reply with quote

in foobar-ioreg.h
Code:

ext_int_edge(L_TO_H);      // external interrupt is tied to the switch
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);


in function definitions
Code:

#int_ext
void pushbutton_isr() {             // got an pushbutton INT
   pushbutton_flag=TRUE;
   }

#define TIMEOUT_USERWAIT   20    // seconds


in main()
Code:

void main() {
   int result=DELAY_COMPLETE;
   int firstpass=TRUE;

   internal_eeprom(INCREMENT16, EEPROM_RESET, DONT_CARE);

   if (internal_eeprom(READ16, EEPROM_FW_VER, DONT_CARE) != (int16)FW_VERSION ) {
      internal_eeprom(INCREMENT16, EEPROM_FLASH_UPD, DONT_CARE);
      internal_eeprom(WRITE16, EEPROM_FW_VER, (int16)FW_VERSION);
      }

   #include "foobar-ioreg.h"    // configure register and timing control defs
   lcd_preinit();
   lcd_init();

   while(TRUE) {

sleepnow:   // that awful philw influence... :*)

      pushbutton_flag=FALSE;
      firstpass=TRUE;
      lcd_init();
      lcd_clear();
      printf(lcd_putc,"   (sleeping)");   // debug only...
      delay_ms(500);
      set_power_mode(SLEEP);
      sleep();                            // wait for pushbutton INT
      delay_ms(1);
      set_power_mode(RUN);

mainloop:
      lcd_clear();
      pushbutton_flag=FALSE;
      timeout_flag=FALSE;
      result=DELAY_COMPLETE;

      while (serial_port_byte_avail())   // clear serial buffer
         getch();

      switch (detect_module(MOD_NONE)) {
         case MOD_BOTH: {  result=display_message(MSG_TWOMODS,MOD_BOTH,TIMEOUT_USERWAIT); break; }
         case MOD_FAIL: {  result=display_message(MSG_MODFAIL,MOD_FAIL,TIMEOUT_MODFAIL) ; break; }
         case MOD_RSFP:  {  result=display_data(MOD_RSFP); break; }
         case MOD_RXFP:  {  result=display_data(MOD_RXFP); break; }
         case MOD_NONE: {  if (firstpass) {
                              result=display_message(MSG_BANNER,MOD_NONE,TIMEOUT_BANNER);
                              delay_ms(500);
                              if (INPUT(PIN_PB_SWITCH)) {     // user requests flash upgrade
                                 result=DELAY_COMPLETE;
                                 user_mode_request();
                                 break;
                                 }
                              if (serial_port_byte_avail()) { // PC requests host mode
                                 host_mode_request();
                                 result=DELAY_COMPLETE;
                                 break;
                                 }
                              if (result==DELAY_MOD_FLAG)
                                 break;
                              }
                           result=display_message(MSG_INSERT,MOD_NONE,TIMEOUT_USERWAIT); break; }

         default:       {  break; }
         }
      firstpass=FALSE;

      if (result==DELAY_SERIAL_FLAG) {    // handles case where module
         result=DELAY_COMPLETE;           // is present at power-up.
         host_mode_request();
         }

      if (result!=DELAY_COMPLETE)         // i.e. module state change or button press
         goto mainloop;

      goto sleepnow;                      // else it was timeout, so hit the hay
      }

      // should never ever get here!
      reset_cpu();
}

Steven
Guest







PostPosted: Fri Jun 17, 2005 5:25 am     Reply with quote

I tested your code and it's ok but i dont get it how you
can create something else ( accept lcd shout down ).

Let me explain myself:

if (GLOBAL_keypad_inactivity_timer == 0)
{
// shut lcd down
doSleep();
}

if i run this in main loop i loos much of power.
The code is acting weird and i cant get the right performance...

Do you have some extended solution ?.
valemike
Guest







PostPosted: Fri Jun 17, 2005 9:10 am     Reply with quote

Haven't seen your code, so i'm making a guess...

Do you have your wdog enabled? If so, then this will wake you up from sleep fairly quickly.

And do you decrement your inactivity timer, such that it wraps from 0 to 0xffff? You should not decrement past 0, or else you won't be able to sleep again for a long time.
Steven
Guest







PostPosted: Sun Jun 19, 2005 5:38 am     Reply with quote

If i go directly on sleep() function

if (GLOBAL_keypad_inactivity_timer == 0)
{
// shut lcd down
sleep();
}

i stuck in the sleep mode direkt after clicking on reset.
I can not execute the rest of the program ( and the program in not waiting for 2 minutes to go to sleep ). He hits the sleep part directly after press on reset... Why is he not waiting 2 minutes to go to sleep.

All other stuff is working. If i shut just lcd this will work but the part above will not ?
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