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

Putting the PIC to sleep

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



Joined: 04 Nov 2004
Posts: 33

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

Putting the PIC to sleep
PostPosted: Thu Nov 04, 2004 2:06 pm     Reply with quote

Another basic question with another basic solution hopefully:

We are playing with PIC 16F627A and we are trying to put it to sleep. Apparently there is more to it than simply using:
sleep();

In our case the code just keeps executing, regardless of the sleep function that we call.

We do realize that the compiler automatically inserts sleep(); at the end of main, but that is not helping us in this case.

any advice will be appreciated.

Thanx
Extreme Beginners
Guest








PostPosted: Thu Nov 04, 2004 2:11 pm     Reply with quote

I think you forget to setup WDT?
Sensor



Joined: 04 Nov 2004
Posts: 33

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

PostPosted: Thu Nov 04, 2004 2:15 pm     Reply with quote

As far as we are aware right now, the WDT is not enabled. For a simple reason. Namely we don't know how to use it yet :-)

We intend to use the Ext_Int RB0 to get it out of the sleep mode so therefore we are not using the WDT. However that doesn't mean that will not use WDT later for other purposes.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

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

PostPosted: Thu Nov 04, 2004 3:08 pm     Reply with quote

Read this first:
http://www.ccsinfo.com/forum/viewtopic.php?t=17013

Then come back and post a small but complete sample demonstrating the problem.
Sensor



Joined: 04 Nov 2004
Posts: 33

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

PostPosted: Thu Nov 04, 2004 3:13 pm     Reply with quote

Here is sample code (16F627A):

Code:

while(TRUE)
{
   if(!input (PIN_B1))
       sleep();
}

Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

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

PostPosted: Thu Nov 04, 2004 4:12 pm     Reply with quote

I guess I was not clear.
Quote:
Then come back and post a small but complete sample demonstrating the problem.


The word complete means a complete program. That means everything. Now this program should be tested and verified by you to exhibit the problem at hand. Don't retype in the post. Cut and paste from the source and remember to use the code button.
asmallri



Joined: 12 Aug 2004
Posts: 1635
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Thu Nov 04, 2004 5:23 pm     Reply with quote

Did you remember to clear the external interupt flag before you put the processor to sleep?
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
Sensor



Joined: 04 Nov 2004
Posts: 33

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

PostPosted: Fri Nov 05, 2004 2:15 pm     Reply with quote

here is the complete EXAMPLE program from CCS in which sleep does not work for us:

Code:
#include <16f627a.h>
#use delay(clock=4000000)
#fuses INTRC_IO,NOWDT

// global flag to send processor into sleep mode
short sleep_mode;

// external interrupt when button pushed and released
#INT_EXT
void ext_isr() {
static short button_pressed=FALSE;

   if(!button_pressed)        // if button action and was not pressed
   {
      button_pressed=TRUE;    // the button is now down
      sleep_mode=TRUE;        // activate sleep
      //printf("The processor is now sleeping.\r");
      ext_int_edge(L_TO_H);   // change so interrupts on release
   }
   else                       // if button action and was pressed
   {
      button_pressed=FALSE;   // the button is now up
      sleep_mode=FALSE;       // reset sleep flag
      ext_int_edge(H_TO_L);   // change so interrupts on press
   }
   if(!input(PIN_B0))         // keep button action sychronized wth button flag
      button_pressed=TRUE;
   delay_ms(100);             // debounce button
}

// main program that increments counter every second unless sleeping
void main()   {
   long counter;

   sleep_mode=FALSE;          // init sleep flag

   ext_int_edge(H_TO_L);      // init interrupt triggering for button press
   enable_interrupts(INT_EXT);// turn on interrupts
   enable_interrupts(GLOBAL);

   //printf("\n\n");

   counter=0;                 // reset the counter
   while(TRUE)
   {
      if(sleep_mode)          // if sleep flag set
         sleep();             // make processor sleep
      //printf("The count value is:  %5ld     \r",counter);
      counter++;              // display count value and increment
      delay_ms(1000);         // every second
   }
}
asmallri



Joined: 12 Aug 2004
Posts: 1635
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Fri Nov 05, 2004 3:29 pm     Reply with quote

You did not clear the init interrupt flag

Code:
 
clear_interrupts(INT_EXT);  // clear the initial interrupt flag
enable_interrupts(INT_EXT);// turn on interrupts


You have used delay inside and outside the interrupt handler. To handle this scenario the complier will disable the external interrupt during the mainline call to delay this means you will not get any interrupt during the 1 second you are delaying (you may have done this on purpose).

Your interrupt handler will get multiple interrupts as a result of switch debounce.
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
pom



Joined: 25 Nov 2004
Posts: 30
Location: Germany

View user's profile Send private message

Choosing interrupts?
PostPosted: Wed Dec 22, 2004 8:12 am     Reply with quote

I wonder, where I can say, to which interrupt the controller shall wake-up. Or will he wake up, equal which interrupt appears?

I made a program, where a controller shall sleep and wake-up, if a can-message appears. Receiving the can-message by interrupt works. But if I insert the command "sleep()" the controller stops working, but does not come back by receiving a message.

I have no hardware to try the example shown in a post before. So, do I have to chose the "alarm clock" for the PIC or should it wake-up by any interrupt?

Thank you for help and have a nice christmas time, pom
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