View previous topic :: View next topic |
Author |
Message |
meereck
Joined: 09 Nov 2006 Posts: 173
|
sleep and wake up |
Posted: Mon Sep 08, 2008 1:10 pm |
|
|
hello, i just want to make sure i get it right.
1) sleep
I just disable interrupts which i dont want to be used for waking the PIC up.
I call sleep();
2) wake up
Interrupts enabled only will wake the PIC up.
Is there anything else i should be aware of?
I am using 18LF2420 particularly.
cheers
Meereck |
|
|
RLScott
Joined: 10 Jul 2007 Posts: 465
|
Re: sleep and wake up |
Posted: Mon Sep 08, 2008 2:29 pm |
|
|
meereck wrote: | hello, i just want to make sure i get it right.
1) sleep
I just disable interrupts which i dont want to be used for waking the PIC up.
I call sleep();
2) wake up
Interrupts enabled only will wake the PIC up.
... |
Yes, only the specific interrupts whose flags you have enabled will wake up the PIC. This will happen even if the Global Interrupt Enable is turned off. In that case, execution after wakeup will continue just after the Sleep instruction. Then you could poll to find out which interrupt flag without actually having an interrupt. _________________ Robert Scott
Real-Time Specialties
Embedded Systems Consulting |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Sep 08, 2008 2:38 pm |
|
|
You need to tell us what you want to do upon wake-up from sleep.
1. What interrupt do you want to use ?
2. Do you want to wake up and immediately continue operation of the program at the next line of code after the sleep() function ?
3. Or, do you want to wake-up and first execute an isr for that interrupt
before resuming execution at the next line after sleep() ? |
|
|
Ttelmah Guest
|
|
Posted: Tue Sep 09, 2008 3:16 am |
|
|
One more comment.
If not using an interrupt handler, you need to clear the interrupts used, before you next go to sleep.
Best Wishes |
|
|
meereck
Joined: 09 Nov 2006 Posts: 173
|
|
Posted: Wed Sep 10, 2008 3:59 am |
|
|
hey guys,
thanks for the replies.
Here is what I basically need:
Sleep the PIC when there is no external action for particular time (I can call sleep() in a timer int routine or in the main loop --- I dont care). I have some external peripherals which would also be good to put in sleep mode by turning low a PIN of the PIC.
There arises one question : can be a pin held low when the PIC is in sleep?
Waking up the PIC should be carried out by action on any PORTB pins. So I was thinking about INT_RB. Therefore, if I get it right, before calling sleep(), I should disable all timer interrupts, and the only interrupt enabled should be INT_RB. Is that correct?
I dont care if the PIC executes the isr routine at first.
Thanks a lot!
Meereck |
|
|
Ttelmah Guest
|
|
Posted: Wed Sep 10, 2008 4:21 am |
|
|
Yes. Logic outputs are 'static', and can remain operated while the chip is asleep.
For low power, you should be careful to ensure that none of the PIC inputs is 'floating'. Especially consider whether turning off the other chips, might leave pins like this.
How many pins of port B, are available for INT_RB, depends on the PIC. On most, only the high four pins support this. Only some of the latter chips have this available on all the pins. Check the data sheet.
The sequence for 'wake up', without using an interrupt handler, would basically be:
Code: |
disable_interrupts(global); //Turn off calling interrupt handlers
disable_interrupts(INT_TIMER0); //do this for all timers in use
//Operate your 'turn off' pin here
dummy=input_b(); //You must read the port, to ensure the latch matches
clear_interrupts(INT_RB);
enable_interrupts(INT_RB);
sleep();
delay_cycles(1); //This puts a 'nop' after the sleep instruction
disable_interrupts(INT_RB);
//Now enable all timer interrupts you want to use
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL); //re-enable calling interrupt handlers
//You may also want to clear the timer interrupts before re-enabling
//the global flag. They should have stopped running when the sleep was
//called, so it depends whether you want to 'catch' any timer overflows
//in the bits before/after the sleep.
|
Now, the chip will wake, if any of the inputs changes after the read of the port. So will not actually sleep if this occurs in the next couple of instructions. Otherwise, it'll wake, whenever an input changes. Remember that once 'asleep', wake-up, will take quite a few uSec, as the oscillator has to restart, and stabilise before the code starts running again. Also note that the instruction _after_ the sleep, is 'pre-fetched' by the chip, before it goes to sleep,so should really be a 'dummy' instruction, or some odd behaviours may appear (tests for example, will reflect the status before sleeping, rather than after, if put here). Hence the 'nop' in the code.
Best Wishes |
|
|
meereck
Joined: 09 Nov 2006 Posts: 173
|
|
Posted: Wed Sep 10, 2008 6:19 am |
|
|
Thanx for the hints and the example src.
I am using 18LF2420. Entire PORTB with internal pull-ups can be used for the interrupt.
How can I assure none of inputs is floating? By external weak pull-ups? For example, I will be using an external chip with a RS232 interface connected to the PIC's HW UART.
have a nice day |
|
|
Ttelmah Guest
|
|
Posted: Wed Sep 10, 2008 10:06 am |
|
|
If the external RS232 driver is still powered, then 'no problem'. If you are turning this off, then I'd add external weak _pull downs_ (rather than pull-ups). Otherwise the protection diodes in the inputs/outputs of the other chips, could result in these being powered.
Best Wishes |
|
|
meereck
Joined: 09 Nov 2006 Posts: 173
|
|
Posted: Wed Sep 10, 2008 3:48 pm |
|
|
great, that is exactly what I was looking for.
Cheers |
|
|
|