View previous topic :: View next topic |
Author |
Message |
jahan
Joined: 04 Apr 2005 Posts: 63
|
how to sleep after 2 minutes inactivity |
Posted: Thu Jun 16, 2005 2:23 pm |
|
|
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 ... |
|
|
valemike Guest
|
|
Posted: Thu Jun 16, 2005 4:29 pm |
|
|
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 |
|
|
valemike Guest
|
|
Posted: Thu Jun 16, 2005 4:31 pm |
|
|
228 should be 2280. |
|
|
jahan
Joined: 04 Apr 2005 Posts: 63
|
and how do I wakeup from RB0? |
Posted: Thu Jun 16, 2005 4:46 pm |
|
|
Thankx Mike,
I get the idea; Now how do I wake the PIC by lowering RB0?
Thankx again |
|
|
valemike Guest
|
|
Posted: Thu Jun 16, 2005 4:52 pm |
|
|
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
|
int_ext ?? |
Posted: Thu Jun 16, 2005 4:55 pm |
|
|
when RB0 does interrupt the PIC, do you know if the program starts from beginning ? or continues from next statment after sleep? |
|
|
valemike Guest
|
|
Posted: Thu Jun 16, 2005 6:08 pm |
|
|
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
|
|
Posted: Fri Jun 17, 2005 1:06 am |
|
|
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
|
|
Posted: Fri Jun 17, 2005 5:25 am |
|
|
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
|
|
Posted: Fri Jun 17, 2005 9:10 am |
|
|
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
|
|
Posted: Sun Jun 19, 2005 5:38 am |
|
|
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 ? |
|
|
|