Help Please: Lowest-power sleep and 25s wake-up

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
Brendan
Posts: 243
Joined: Tue Nov 27, 2012 12:53 pm
Location: Cambridge, UK
Has thanked: 140 times
Been thanked: 118 times
Contact:

Help Please: Lowest-power sleep and 25s wake-up

Post by Brendan »

Hello All.

I'm currently mid-project and trying to figure my way around the various timer features for the 18LF26k22 device. It's quite a feature-rich chip and I'd greatly appreciate expert guidance before I set off down the road... so-to-speak.

What I must do is put the chip to sleep (lowest achievable power mode) after running a routine, and periodically wake on timer after each 25s sleep interval to repeat the routine. However, I must maintain memory values (in RAM) during sleep so the watchdog timer is therefore a no-go. Small sub-second inaccuracy isn't critical if there's a significant trade-off with sleep current as the primary concern.

The primary clock is internal R-C, though I did include a tuning fork crystal (secondary oscillator pins) on the PCB as available to use if preferable for any reason, though as I say, it's sleep current that is the primary concern and no particular need to use the crystal if it benefits timer/sleep current.


Many thanks in advance,

Brendan

hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times
Contact:

Re: Help Please: Lowest-power sleep and 25s wake-up

Post by hyperion007 »

Could you use an RTC chip like DS3231 with an alarm function?

That way you can, just before you put the PIC to sleep, read the current time then update the alarm of the RTC with the current time + 25 seconds, followed by the sleep command.

Brendan
Posts: 243
Joined: Tue Nov 27, 2012 12:53 pm
Location: Cambridge, UK
Has thanked: 140 times
Been thanked: 118 times
Contact:

Re: Help Please: Lowest-power sleep and 25s wake-up

Post by Brendan »

Hi, and thank you for the suggestion.

I had originally considered such a device but sadly the cost was prohibitive, as this is for what could well develop into a volume product. Certainly a contender for other projects though :)

The project involves two boards largely built the same with an RF link between them, and the boards are already designed and built. Any additional cost would be doubled per paired boards and the resulting $3 + $3 budgetary price for the timer alone would have been far too high. The specs of the PIC however indicate that very low-power sleep timer operation is achievable, but the various operating modes, registers, and timers available, suggest the need of help from someone more experienced than myself to identify the right settings to achieve this result.


All the best,

Brendan

Kenrix2
Flowcode v5 User
Posts: 211
Joined: Tue Feb 19, 2013 9:51 pm
Has thanked: 72 times
Been thanked: 177 times
Contact:

Re: Help Please: Lowest-power sleep and 25s wake-up

Post by Kenrix2 »

No idea how to set it up for an 18LF series but for the 16LF series like the PIC16LF1936 it is simply:
wdtcon =0b00xxxxx1; where the x's are the time and the 1 enables the wdt.
sleep();
If the gie bit is clear it will not vector when the wdt times out. The wdt configuration bits are set to 'controlled by swdten'.

Brendan
Posts: 243
Joined: Tue Nov 27, 2012 12:53 pm
Location: Cambridge, UK
Has thanked: 140 times
Been thanked: 118 times
Contact:

Re: Help Please: Lowest-power sleep and 25s wake-up

Post by Brendan »

Hello, and thanks for the suggestion :)

I understand that use of the WDT would initiate a reset, and the problem there would be that I'd lose stored values in RAM when next woken up by WDT Reset.

Due to the desire to keep wake-up processes short, I think I may have found the relevant information from the datasheet (obviously supplemented with C code sat within the process loop).
12.9 Timer1/3/5 Operation During Sleep
Timer1/3/5 can only operate during Sleep when set up
in Asynchronous Counter mode. In this mode, an
external crystal or clock source can be used to
increment the counter. To set up the timer to wake the
device:
• TMRxON bit of the TxCON register must be set
• TMRxIE bit of the PIE1/2/5 register must be set
• PEIE/GIEL bit of the INTCON register must be set
• TxSYNCbit of the TxCON register must be set
• TMRxCS bits of the TxCON register must be
configured
• TxSOSCEN bit of the TxCON register must be
configured
The device will wake-up on an overflow and execute
the next instruction. If the GIE/GIEH bit of the INTCON
register is set, the device will call the Interrupt Service
Routine.
The secondary oscillator will continue to operate in
Sleep regardless of the TxSYNCbit setting.
Due to the simple wake on overflow, merely commencing from the next instruction (rather than calling an interrupt routine every time), I believe I could simply cascade multiple timers in the loop sequence in a kind of 'course + finer + fine' timing arrangement, so by presetting the timer registers I could set the combined period to whatever I needed. For example:

Power-up routine (including timer setup)
Instruction clock source = Primary INTRC oscillator
Timers 1,3,5 clock source = Secondary Xtal oscillator (32.786kHz)

<loop start>
Execute normal routine
Enable wake on TMR1 overflow
Sleep instruction
<WakeUp>
Stop TMR1
Enable wake on TMR2 overflow
Sleep instruction
<WakeUp>
Stop TMR2
Enable wake on TMR3 overflow
Sleep instruction
<WakeUp>
Stop TMR3
<repeat loop>

As wake on timer is new and unfamiliar territory for me, not entirely sure if I'm talking naive gobbledygook :lol: or if there's a more traditional/preferential strategy, given that absolute accuracy isn't essential.


All the best,
Brendan

Kenrix2
Flowcode v5 User
Posts: 211
Joined: Tue Feb 19, 2013 9:51 pm
Has thanked: 72 times
Been thanked: 177 times
Contact:

Re: Help Please: Lowest-power sleep and 25s wake-up

Post by Kenrix2 »

I understand that use of the WDT would initiate a reset
It doesn't, it just wakes it up. You can turn off the wdt after it wakes up if you like.
Due to the desire to keep wake-up processes short
If your using a crystal, try enabling 2 speed startup if it's available.

Brendan
Posts: 243
Joined: Tue Nov 27, 2012 12:53 pm
Location: Cambridge, UK
Has thanked: 140 times
Been thanked: 118 times
Contact:

Re: Help Please: Lowest-power sleep and 25s wake-up

Post by Brendan »

Thanks for that :)

As I suspected... gobbledygook regarding WDT and RAM retention, but that will go down to internet disinformation.

The 18F.. chip does indeed support two-speed start-up, though the issue for me is establishing the best implementation with the plethora of timers, oscillator configurations, and interdependent register configurations with this chip. Doubtless '101 ways to skin a cat', so-to-speak, but choosing the best cat to skin is quite another matter :|

Right.. Time for further self-education.

All the best,
Brendan

Post Reply