View previous topic :: View next topic |
Author |
Message |
Sensor
Joined: 04 Nov 2004 Posts: 33
|
Putting the PIC to sleep |
Posted: Thu Nov 04, 2004 2:06 pm |
|
|
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
|
|
Posted: Thu Nov 04, 2004 2:11 pm |
|
|
I think you forget to setup WDT? |
|
|
Sensor
Joined: 04 Nov 2004 Posts: 33
|
|
Posted: Thu Nov 04, 2004 2:15 pm |
|
|
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
|
|
|
Sensor
Joined: 04 Nov 2004 Posts: 33
|
|
Posted: Thu Nov 04, 2004 3:13 pm |
|
|
Here is sample code (16F627A):
Code: |
while(TRUE)
{
if(!input (PIN_B1))
sleep();
}
|
|
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Nov 04, 2004 4:12 pm |
|
|
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: 1634 Location: Perth, Australia
|
|
Posted: Thu Nov 04, 2004 5:23 pm |
|
|
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
|
|
Posted: Fri Nov 05, 2004 2:15 pm |
|
|
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: 1634 Location: Perth, Australia
|
|
Posted: Fri Nov 05, 2004 3:29 pm |
|
|
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
|
Choosing interrupts? |
Posted: Wed Dec 22, 2004 8:12 am |
|
|
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 |
|
|
|