PIC18F..K22 sleep mode

Discuss PIC and electronic related things

Moderators: David Barker, Jerry Messina

Post Reply
SHughes_Fusion
Posts: 219
Joined: Wed Sep 11, 2013 1:27 pm
Location: Chesterfield

PIC18F..K22 sleep mode

Post by SHughes_Fusion » Fri Jun 05, 2015 8:29 am

I'm developing a battery powered application and I'm trying to understand how to best use sleep mode on the K22 series processors.

At the moment I'm using the PMD module to turn off all peripherals when I go in to sleep mode but it has just hit me, do I need to? Does this actually save any additional power in sleep, or is it just meant for use in Idle mode?

Conversely, does disabling the peripherals I don't use at all save any power in run mode?

The 18F26K22 datasheet isn't that clear on this, the manual for the PIC24 series has much more information but I don't know how this translates to the 18F series.

Also, I have implemented a mode where if the device hasn't been used for 10 minutes it shuts down and only a key press or plugging in the charger will wake it. However, I'm seeing the unit occasionally wake up for no apparent reason. Re-reading the datasheet I wonder if this could be due to interrupts? I'd assumed if I just use GIE to globally disable interrupts that would be enough, however, re-reading the manual it reads as if as interrupt that is enabled, regardless of the GIE bit will wake from sleep. Is this correct? I'm wondering that as I use Timer0 - which isn't affected by the PMD module - it will be incrementing each time the unit wakes from sleep to check the inputs. Therefore, eventually it will overflow and set the interrupt flag which will wake the processor.

Does this sound likely?

Is it good practice to clear all interrupt enable bits in INTCON/PIE registers before going to sleep and re-set them on wake if you don't want to wake by interrupt?

Is it similarly good practice to have a ClrWDT immediately on wake? (I'm wondering if the watchdog is waking the processor from sleep then resetting the unit)

On wake I do While Not FVSTR Went so nothing happens until the FVR is stable while - I'm assuming - will mean that should the battery have dropped very low the BOR will trigger. Can anyone see any issues with that?

I do that as I have a function on startup which looks for a brown-out reset and will lock the unit in sleep mode until the charger is connected. Am I just asking for trouble doing this as presumably once the oscillator kicks back up the brown-out module will be triggered anyway? (I runat 64MHz normally but kick down to 31KHz when in power-down mode)

I think using sleep modes is the sort of thing that could do with a wiki entry as it isn't as simple as it appears!

Jerry Messina
Swordfish Developer
Posts: 1237
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: PIC18F..K22 sleep mode

Post by Jerry Messina » Fri Jun 05, 2015 1:31 pm

At the moment I'm using the PMD module to turn off all peripherals when I go in to sleep mode but it has just hit me, do I need to? Does this actually save any additional power in sleep, or is it just meant for use in Idle mode? Conversely, does disabling the peripherals I don't use at all save any power in run mode?
Supposedly disabling the peripheral using the PMD gets you the absolute minimum power consumption since it removes the power and clocks from all the registers and circuitry, in any mode.
Seems like a reasonable thing to do even though most of the clocks are stopped in sleep mode. It might get you just a few more uA, but if you're not using it then you're not using it. Obviously, don't disable any peripheral you might use to wake from sleep.
However, I'm seeing the unit occasionally wake up for no apparent reason. Re-reading the datasheet I wonder if this could be due to interrupts? I'd assumed if I just use GIE to globally disable interrupts that would be enough, however, re-reading the manual it reads as if as interrupt that is enabled, regardless of the GIE bit will wake from sleep. Is this correct?
Yes. When using sleep mode, GIEH/GIEL determine WHAT happens when it wakes up, but the individual PIEx/INTCONx bits control IF the peripheral will wake it up (see the Interrupt Logic diagram in the Interrupts section of the datasheet). When it comes out of sleep, if GIE = 1 then you'll branch to the ISR, otherwise code execution will just continue after the SLEEP instruction. Some peripherals can't wake you from sleep (since they need the clock running), but others can. There's more info in the PIC18 Family Reference Manual (http://ww1.microchip.com/downloads/en/D ... 39500a.pdf)
Is it good practice to clear all interrupt enable bits in INTCON/PIE registers before going to sleep and re-set them on wake if you don't want to wake by interrupt?
I think so. At least that way it's very clear how you'll wake up, if nothing more.
Is it similarly good practice to have a ClrWDT immediately on wake? (I'm wondering if the watchdog is waking the processor from sleep then resetting the unit)
Yup. Otherwise you might wake up because of peripheral X, only to then have the WDT timeout a few instructions later which since you're not sleeping will now get you a RESET. The WDT timer is cleared when you do the SLEEP instruction, but not when it exits.


For the BOR questions I think some of that depends on how you have the BOR setup (disabled in sleep, software controlled, etc)
I think using sleep modes is the sort of thing that could do with a wiki entry as it isn't as simple as it appears!
That's for sure, especially with some of the newer chips and all their power-managed modes!

SHughes_Fusion
Posts: 219
Joined: Wed Sep 11, 2013 1:27 pm
Location: Chesterfield

Re: PIC18F..K22 sleep mode

Post by SHughes_Fusion » Tue Jun 09, 2015 12:49 pm

Thanks, Jerry cleared up a few things I'm not sure about.

The only thing left to try and find out is how much effect using the PMD actually has in sleep mode. The disadvantage is that the modules need to be reinitialised when you turn them back on. As I alter a few settings as part of the process I've ended up writing the software so a wake from power-down mode restarts the process as this is the easiest way to ensure I'm in a known state. If I didn't power-down the peripherals I use then I could avoid this.

Brown-out is disabled in sleep. I'm intending to use it as a battery protection device. I'm running off a single cell LiPo and my thinking is that if the voltage (after a 0.1V LDO) triggers the brown-out set at 2.5V then I need to inhibit operation of the device until the charger is connected.

Originally I was thinking that each time the unit wakes from sleep it would be useful to have this check taking place. However, now I think about it I won't gain anything doing that. The battery voltage won't really dip until the unit kicks back up to full clock speed and that is when I'll get a brown-out if there is to be one.

Also, in practice current drain won't be much lower when the unit is inhibited than in sleep. Sleep mode wakes every 0.5 second to check a switch and the charger status inputs. Inhibit mode wakes every second to check the charger inputs. It might use a few uA less but in the scheme of things I can't see that causing an issue. The unit would have had to be sitting in power-down mode for, well, months, years, certainly a long time before such a low drain would actually harm the battery.

Post Reply