Putting a PIC in sleepmode for 1 minute

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 8.

Moderator: Benj

Post Reply
MJU
Posts: 502
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 121 times
Been thanked: 108 times
Contact:

Putting a PIC in sleepmode for 1 minute

Post by MJU »

I'm looking to make a project where a PIC is used, powered by a battery.

This project needs only to work a few seconds each minute. The rest of the time it should use as little power as possible.
I was looking for the PIC18F14K50, as a NanoPower device that can be but into sleep mode. (https://ww1.microchip.com/downloads/en/ ... 01350F.pdf)
But this one can not wake up using it's TM0 (oscillator is stopped in sleep mode), the datasheet says that it can wake up using TM01 (page 105), by using an external oscillator with a 32Khz crystal.

Anyone used this feature already?
What would in Flowcode be the way to
1- Get the PIC into sleepmode after completing a task (as low power possible)
2- Wake it up after a certain time (not critical, about every minute)

Or are there other ways of getting the PIC into a low power state for a longer period and wake up by itself?

Thanks!

chipfryer27
Valued Contributor
Valued Contributor
Posts: 618
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: Putting a PIC in sleepmode for 1 minute

Post by chipfryer27 »

Hi

Have a look at the Watchdog Timer feature. It can be used to wake from sleep and can be configured for between 4ms and 2.18 minutes. See page 291 of datasheet. Once enabled you will need to ensure you include the appropriate "Clear" commands at strategic places or else your program will reset to beginning. I've missed that once or twice......<s>

Regards

MJU
Posts: 502
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 121 times
Been thanked: 108 times
Contact:

Re: Putting a PIC in sleepmode for 1 minute

Post by MJU »

By mistake I don't have the proposed MCU here right now ( PIC18F14K50), but another NanoWatt MCU is lying here next to me: the 18F4550.
For testing purposes it seems that this old PIC will work too?

In the datasheet (https://www.google.com/url?sa=t&rct=j&q ... qtY6bH5ED6), page 297, tells the same story for the Watchdog timer as the PIC18F14K50.

I've made a dummy Flowcode project in which I want to get the PIC to sleep after completing a certain task, and wake up/resume after about a minute.

Is there someone willing to add the code for sleep/wakeup to this chart?
The sole purpose for this is to save battery power and if needed for the battery life I will get myself some more (energy effecient) PIC18F14K50's.
SleepPIC.fcfx
(8.47 KiB) Downloaded 158 times
Thanks in advance

chipfryer27
Valued Contributor
Valued Contributor
Posts: 618
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: Putting a PIC in sleepmode for 1 minute

Post by chipfryer27 »

Hi

Getting best battery life is indeed a "black art" and in my opinion you need to consider this from the onset. Personally I think you need to design around such rather than add it in later. Microchip have many guides for enabling "Sleep" and "Deep Sleep" function. If you do a search for "microchip power saving guide" or such like you will get good results. This is just an example:-

https://ww1.microchip.com/downloads/en/ ... er%202.pdf

I don't know much about your application but first figure out what chip peripheral(s) you don't need and ensure they are disabled at the start. Depending on your application it may be a good idea to have your main loop pretty much just sleep........ When the WDT wakes it up after 60s (or whatever) it tests for a condition and if met it branches to a Macro to handle such, if not it just loops around and goes back to sleep.

Another possibility is to use Interrupt On Change which can also bring it out of sleep. It sleeps until a designated pin(s) changes state. I've used this many times and have obtained a couple of years of use from two AA's (obviously not much changed in those years <s>).

In your example chart Main Loop is displaying some "mumbo jumbo", waits 5s then displays "mumbo jumbo" for another 5 seconds. I assume the second "mumbo jumbo" is different to the first? Could it sleep between updates or just update if changes occur?

To put it to sleep use a C-Code block containing sleep();
Personally I prefer to follow that with nop(); to ensure the next instruction after it wakes does nothing.
If using the WDT you need to enable that in project options and perhaps also strategically place C-Code blocks containing clear_wdt();
This will however greatly depend on how long the WDT is set for. The datasheet will tell you what to set the postscaler to for a given time.

However unless you have disabled unused peripherals and considered other option such as making unused ports outputs etc you may not see great changes. Maybe if you can share a bit more of what you are doing (even by PM) I could help a bit further.

Hope the above was of some help,
Regards

MJU
Posts: 502
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 121 times
Been thanked: 108 times
Contact:

Re: Putting a PIC in sleepmode for 1 minute

Post by MJU »

chipfryer27 wrote:
Sat Jun 19, 2021 9:48 pm
Hi

Getting best battery life is indeed a "black art" and in my opinion you need to consider this from the onset. Personally I think you need to design around such rather than add it in later. Microchip have many guides for enabling "Sleep" and "Deep Sleep" function. If you do a search for "microchip power saving guide" or such like you will get good results. This is just an example:-

---- snip....

Hope the above was of some help,
Regards
Thanks, what a great answer.

I'm aware of the things I need to consider getting as much power saving as possible. I've studied the hardware requirements in the past.
One things that sometimes get forgotten is the power regulator.

But I think that the PIC in Sleep mode consumes a lot less power then in normal (running) mode.
The example is not really what I want to make.
What I want to make is a sensor that gets read ones every few minutes and sends it data via a 433Mhz transceiver module.
This module will be placed in sleep mode also after sending it's data.

So the example I've posted is nowhere near the thing I want to make. (thanks in advance for viewing it!)
It was just an example of something that needs to stop at a certain point and start running after a short break.

So the thing I want is to just sample a sensor, wake up the transceiver and after sending the data, put the transceiver back to sleep and get the PIC into sleep mode (less power consumption).. make it sleep for a while. Wake it up and start all over again.

So the example is just a way of getting started to learn how to get this working. Not a real thing I want to make.

chipfryer27
Valued Contributor
Valued Contributor
Posts: 618
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: Putting a PIC in sleepmode for 1 minute

Post by chipfryer27 »

Hi

You can certainly reduce your power to the point you can barely measure consumption unless you have some serious test kit <s>

It seems like you may be sending out values every minute or so. Without question the RF when transmitting will be you biggest draw so try and minimise this. Depending on your module(s) you can set output power so perhaps experiment to find the least power that gives reliable transmission.

Also, would it be possible to sense then only send if the value has changed significantly? Say I was sending temperature and currently it's 22C. Would I really care if it falls/rises by 0.xC especially if my fridge full of Leffe is working fine <s>

Finding out just how little power you can draw can be quite addictive..

Regards

MJU
Posts: 502
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 121 times
Been thanked: 108 times
Contact:

Re: Putting a PIC in sleepmode for 1 minute

Post by MJU »

chipfryer27 wrote:
Sun Jun 20, 2021 6:53 am
Hi

You can certainly reduce your power to the point you can barely measure consumption unless you have some serious test kit <s>

It seems like you may be sending out values every minute or so. Without question the RF when transmitting will be you biggest draw so try and minimise this. Depending on your module(s) you can set output power so perhaps experiment to find the least power that gives reliable transmission.

Also, would it be possible to sense then only send if the value has changed significantly? Say I was sending temperature and currently it's 22C. Would I really care if it falls/rises by 0.xC especially if my fridge full of Leffe is working fine <s>

Finding out just how little power you can draw can be quite addictive..

Regards
Hahaha Chipfryer, my fridge doesn't alone contains Leffe, there are many more great Belgian beers. A few of them: https://www.bierhandelwillems.be/nl/ont ... lijst-5951

The way you suggest to reduce power, I already use that in a project that monitors my garage wireless.
If something changes, it sends the change to the receiver, but if nothing changes the receiver needs to get an status every 15minutes.
If there is no transmission for >15minutes, the receiver reports that there is an error in the transmission.
This way, I can be sure that after 15minutes the send/receive works or not.
I can use this in this project too.

But still I don't know how to implement this Sleep mode into a project.
Can someone help me get started with the thing I want to reach?
Get the PIC to get to sleep and wake up, starting his task again?

Thanks in advance..

chipfryer27
Valued Contributor
Valued Contributor
Posts: 618
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: Putting a PIC in sleepmode for 1 minute

Post by chipfryer27 »

Hi

Sounds as though you have a good handle on what you want to do.

Putting it to sleep is quite easy, just include in a C-Code block :-

sleep();

In project options you will find one to enable WDT (varies upon chip). You will also need to set the postscaler to suit your needs and your datasheet will give details of what to set.

I'm busy tomorrow during the day but I'll knock up a basic example of using wdt/sleep once I get a chance. It most likely won't be for your chip but you will be able to adapt as the principles are the same.

Still jealous that you are driving distance to my second favourite pub ever...Cambrinus (King of beer for those unaware) in Bruge. Only 400 odd Belgian beers available at any time and their wooden bound guide is legendary... Think that might be my first trip once this covid is dealt with.

Regards

MJU
Posts: 502
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 121 times
Been thanked: 108 times
Contact:

Re: Putting a PIC in sleepmode for 1 minute

Post by MJU »

Hey thank you chipfryer:

I've edited the testfile like this:
SleepPIC.fcfx
(8.63 KiB) Downloaded 207 times
The WDT is set to 1/32768 postscaler in the settings, so it will start the loop again after 32768 cycles (I think 1 cycle is 4ms).
So this should work?

chipfryer27
Valued Contributor
Valued Contributor
Posts: 618
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: Putting a PIC in sleepmode for 1 minute

Post by chipfryer27 »

Hi

I'm not familiar with that chip / options but your pretty much there except I forgot that the sleep instruction should be upper case

SLEEP();

I have attached two files (No WDT and With WDT) using a less complicated / older chip a 16F1939 to illustrate usage. The file(s) flash an LED connected to Port A0 at a rate of approx 0.5 seconds and is in a loop with a count of three. This Loop is called Do Something (and can be anything you want, not necessarily a loop) and this sits inside an endless loop.
No WDT.fcfx
(7.57 KiB) Downloaded 157 times
When "No WDT" is run, the LED flashes three times then loops back to the beginning so in reality it is constantly flashing.
With WDT.fcfx
(8.29 KiB) Downloaded 166 times
In "With WDT" I have enabled WDT under Project Options and I've also set the WDTCON to give a timeout of 16s. The datasheet will give you your full options. After the Do Something loop I have added a C-Code block containing SLEEP();

Now when you run, the led flashes three times as before, but then goes to sleep for 16s before looping back (note you need to run on Hardware it won't Simulate)

I stress that this is just an example of how you can use the WDT/Sleep functions and that to get any real power savings you need to do a lot more as mentioned in previous posts such as disabling unused peripherals, setting unused ports as outputs, running at the lowest speed you can etc. You can get amazing results and it is very addictive getting power down.

Hope this is of help,
Regards

User avatar
AbhijitR
Posts: 298
Joined: Fri Nov 07, 2014 12:48 pm
Location: Pune, India
Has thanked: 279 times
Been thanked: 78 times
Contact:

Re: Putting a PIC in sleepmode for 1 minute

Post by AbhijitR »

Hello! Chipfryer27

Many thanks for this explanation as well the example, I am for sure going to try that, this will be my first time to use/see how WDT works, i shall keep posted.

Abhi

chipfryer27
Valued Contributor
Valued Contributor
Posts: 618
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: Putting a PIC in sleepmode for 1 minute

Post by chipfryer27 »

Hi Abhi

I first used the WDT a few years back (FCv5 I think) when I had some comms issues I struggled to resolve. Unexpectedly it would lock up. Probably me doing something stupid <s>. I enabled the WDT and then placed the associated clear instruction(s) within. If, and it did, lock up then the clear(s) would not get executed and it would then reset itself.

Later, when I started playing with battery powered devices, usually communications, I started messing with the sleep instruction using both WDT and IOC to wake it up, do something, then sleep again.

Sleep / IOC / WDT can be really useful. I see in more powerful chips RTC and Calendars are used so it can sleep for years... I think my cat has one of them too.....

Regards

MJU
Posts: 502
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 121 times
Been thanked: 108 times
Contact:

Re: Putting a PIC in sleepmode for 1 minute

Post by MJU »

Hey guys,

After playing a few hours with the settings for the 18F4550 on a Matrix EB board, I finally got it to flash a few LED's :-(
The problem with the EB006 seems to be the switches for the XTAL and others.

So after a few hours I got it to flash leds and show a text on a display.
My goal was to (like Chipfryer did) flash a few times and the get the chip to go to sleep, and after a while start with its loop again.

But I just can't get it go to sleep.
Unlike in Chipfryers chart, I did some config in the project options:
- set the OSC to the point that the LED's flash in a normal rhythm (so this way I know the Osc setting is OK)
- Enable the watchdog
- I set the wathdog postcaler to 1/32768

All of these in the project options/configure settings.
If I look in the 18F4550 datasheet, the WDTCON is a setting that allows software enable the watchdog timer. (page 298)
Isn't this the same as enabling the Watchdog in the options?

It's late already, need to go to sleep, but it will keep me bothering why in this attached flowchart, the sleep function doesn't work.
Anyone that is willing to investigate it? Help me some more?
SleepPIC2.fcfx
(8.95 KiB) Downloaded 114 times

chipfryer27
Valued Contributor
Valued Contributor
Posts: 618
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: Putting a PIC in sleepmode for 1 minute

Post by chipfryer27 »

Hi

In your second loop that "does something" you have set it to Loop While = 5
Try setting it to Count = 5
It should now loop five times and move on to the Sleep
As you weren't reaching your Sleep instruction (which clears WDT) the program would also reset after WDT expires but you would easily overlook this as the display would only change slightly.

I notice that you don't have Autoclear Watchdog enabled in your settings. This automatically inserts clear instructions when you set delays to prevent the chip resetting during such if the delay(s) exceed WDT. It's always good to check as it's easily overlooked (well by me anyway <s>).

Hope this helps.

MJU
Posts: 502
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 121 times
Been thanked: 108 times
Contact:

Re: Putting a PIC in sleepmode for 1 minute

Post by MJU »

OH MY!!!

Chipfryer, this is unbelievable embarrassing.
This is not only a proof that I must be stupid, but also that playing with this stuff beyond bedtime isn't a good idea for me.
Thank you, now it works!

I was searching the datasheet for what I did wrong, never asked myself if the simplest part of the whole flowchart..
Thank you!

But I can't find the suggested feature in the settings: "Autoclear Watchdog enabled". I've been looking in the datasheet and can't find it?

Thanks again for your help, appreciate it very much.

Another topic I need to start is how to disable hardware features of the chip.
To reduce power you've suggested to disable some features of the chip. I ones tried that, but can't find in what project.

This will be the topic for a next post..
Thanks!!

chipfryer27
Valued Contributor
Valued Contributor
Posts: 618
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: Putting a PIC in sleepmode for 1 minute

Post by chipfryer27 »

Hi

Sometimes you can't see the Abbey for the Trappiste... :lol: :lol:

In Project Options / General Options, just under where you set Clock Speed there is a check box for Autoclear Watchdog. If you are using the WDT to reset if things go wrong, and have low timeouts, it's handy to prevent a reset when using delays.

It's quite simple to disable a peripheral(s). From the datasheet you will find the appropriate register and bit to enable/disable. For the 16F1939 in my previous example, page 150 of the datasheet tells us how to disable the ADC, in this case Bit 0 (ADON) of the ADCON0 register.

"bit 0 ADON: ADC Enable bit
1 = ADC is enabled
0 = ADC is disabled and consumes no operating current"

One way to do this is to use a C-Code block containing clear_bit(ADCON0,0); This instructions clears Register / Bit position (makes it 0).

I thought I'd read somewhere that FC now disables all peripherals by default, but I can't find any reference to such in a search so I guess I must have read it in my letter to Santa :lol: :lol:

Hope the above has helped. Remember too if the chip doesn't have it, you don't need to disable it. I rediscovered some of the smaller devices when using battery power as it was too easy to become complacent with the National Grid behind you :lol:

Regards

jgu1
Posts: 1333
Joined: Tue Oct 06, 2009 9:39 am
Has thanked: 1135 times
Been thanked: 299 times
Contact:

Re: Putting a PIC in sleepmode for 1 minute

Post by jgu1 »

Hi Chipfreyer and MJU!

I sense you're done, I will not hijack :D . I dare to ask. I have no experience with the Sleep function, very little. Can make you make a small example of how to wake it up after sleep using the trigger one of the pin on a port.

First do something, then sleep, and after, if one of the pin of a port is triggered, then do something again, and over again. Thank´s in advance.

Jorgen

chipfryer27
Valued Contributor
Valued Contributor
Posts: 618
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: Putting a PIC in sleepmode for 1 minute

Post by chipfryer27 »

Hi

In this very simple example I have again used a 16F1939 but with LEDs on A0 and A1, and also a button on B7.

I created a Macro called Flash and inside is a "do something" loop flashing A0 three times then exiting.

Instead of enabling WDT to wake it up, this time we enable IOC on our desired pin(s). This is chip specific though and datasheet will provide full details of interrupts.

We then enter the Main loop.

Inside the Main Loop we flash A1 once then go to sleep.

The chip enjoys it's well earned rest until we push the button. It wakes, performs the Macro Flash (flash A0 three times), exits then loops around flashing A1 once before going back to sleep.

The reason I include A1flashing once in the loop is to illustrate that the chip is actually sleeping not just running around the loop until you push the button.

Remember this won't simulate, you will need to run on Hardware and I'm sure you can easily adapt for whatever chip you have lying around.

Hope this helps

Regards
Attachments
Wake on IOC.fcfx
(11.18 KiB) Downloaded 122 times

User avatar
AbhijitR
Posts: 298
Joined: Fri Nov 07, 2014 12:48 pm
Location: Pune, India
Has thanked: 279 times
Been thanked: 78 times
Contact:

Re: Putting a PIC in sleepmode for 1 minute

Post by AbhijitR »

Hello! Chipfryer27
Good morning

Too good, what a fantastic explanation, can't wait to try, hope find some time this weekend.

Thank you.

Abhi

jgu1
Posts: 1333
Joined: Tue Oct 06, 2009 9:39 am
Has thanked: 1135 times
Been thanked: 299 times
Contact:

Re: Putting a PIC in sleepmode for 1 minute

Post by jgu1 »

Hi chipfreyer!

Thank you very much for your explanation and the example you have made for me. I'm doing a test here over the weekend, and you hear :D

Br Jorgen.

chipfryer27
Valued Contributor
Valued Contributor
Posts: 618
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: Putting a PIC in sleepmode for 1 minute

Post by chipfryer27 »

Hi Abhi and Jorgen

Your very welcome and I hope you have fun with it.

Let me know how you get on.

Regards

jgu1
Posts: 1333
Joined: Tue Oct 06, 2009 9:39 am
Has thanked: 1135 times
Been thanked: 299 times
Contact:

Re: Putting a PIC in sleepmode for 1 minute

Post by jgu1 »

Hi chipfreyer!

I have tested your sleep program.
It works perfectly. I can see when it falls asleep, consumption drops from a little bit to almost nothing. and when i activate iot it wakes up again and led flashen. I am working on a project where I have to make a multi-channel remote control driven by battery, so it would be nice to save power :lol:
Your example gives a really good understanding of the sleeping function. Now I Will play with it.

Thank`s again. :D

Br. Jorgen

MJU
Posts: 502
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 121 times
Been thanked: 108 times
Contact:

Re: Putting a PIC in sleepmode for 1 minute

Post by MJU »

chipfryer27 wrote:
Fri Jun 25, 2021 5:49 am
Hi Abhi and Jorgen

Your very welcome and I hope you have fun with it.

Let me know how you get on.

Regards
Hey Chipfryer, again let me thank you for all the help given.
My testproject works so this means a lot to me.
But, like always, several things came up that need my attention more than my hobby.

Thanks for your advice!

chipfryer27
Valued Contributor
Valued Contributor
Posts: 618
Joined: Fri Jun 06, 2014 3:53 pm
Has thanked: 184 times
Been thanked: 195 times
Contact:

Re: Putting a PIC in sleepmode for 1 minute

Post by chipfryer27 »

Hi MJU, Abhi and Jorgen

Thanks for the kind words, appreciated, and I'm very happy to hear your having success with your projects.

Bonne chance!

Regards
PS
MJU, does that mean I get two beers in Cambrinus now? :D

Post Reply