"sleep" inside a loop

For general Flowcode discussion that does not belong in the other sections.
ELECTRONICA67
Posts: 143
http://meble-kuchenne.info.pl
Joined: Wed Dec 02, 2020 3:59 pm
Has thanked: 26 times
Been thanked: 9 times

Re: "sleep" inside a loop

Post by ELECTRONICA67 »

Hello mnfisher,

Forgive my ignorance, it should be easy to wake up in sleep mode, however, I can't achieve it without affecting the function that the program performs, I've been trying for days, (I think because I'm an electronics and not a programmer, what I've learned about programming I owe it to Flowcode since I started in version 3 I need to read the subject) :oops:

I think everything is in the PIC configuration (Project Option), for example in the example you send me that configuration does not look like the PIC16F1847 or 16F1840

I see the data sheet and it mentions that it can be woken up with Timer1 which continues working even if sleep is selected, there I made the clock, I added a B7 led to turn on and off for 1 second, but the program starts and nothing happens, I added the command before sleep: WDTCONbits. WDTPS = 0b01010; to put it to sleep for 1 second.

The program starts correctly, then goes to sleep, then goes to sleep, the power consumption goes down and it does nothing. Maybe from what you're telling me, I have the time set for longer than I think and that's why it doesn't do anything.

Could you please guide me on how to configure the time it remains in sleep mode :?:

Regards and Thanks
Enrique
Attachments
IOC_1847.fcfx
(29.8 KiB) Downloaded 108 times

ELECTRONICA67
Posts: 143
Joined: Wed Dec 02, 2020 3:59 pm
Has thanked: 26 times
Been thanked: 9 times

Re: "sleep" inside a loop

Post by ELECTRONICA67 »

Hello Martin,

I measure the current value with the multimeter 0.012mA, in fact the reading taken in "sleep" I suppose, is quite good for a few months working with batteries.

The values ​​you mention for WDT, I can't find them, I assume it's in Project Option, but I don't see where to select in the configuration of the pic16f1847 (Project Option) or how to do it.
Last edited by ELECTRONICA67 on Thu Nov 14, 2024 1:03 am, edited 1 time in total.

ELECTRONICA67
Posts: 143
Joined: Wed Dec 02, 2020 3:59 pm
Has thanked: 26 times
Been thanked: 9 times

Flowcode v10 Re: "sleep" inside a loop

Post by ELECTRONICA67 »

I am translating some comments with Google and correcting them, but I think that in some others it does not translate as I would like. I apologize if any comment is misunderstood.

mnfisher
Valued Contributor
Posts: 1548
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 138 times
Been thanked: 737 times

Re: "sleep" inside a loop

Post by mnfisher »

Sorry for the delay - sleepy time here...

Your early post seemed to use a 12f1680 MCU - and it seems to allow some time setting for the WDT in the datasheet.

You also set the wdt (not timer1) using
WDTCONbits. WDTPS = 0b01010; to put it to sleep for 1 second.
You have the WDT disabled (in the build->properties tab) - try enabling it there? According to the data sheet it should allow up to about 250s . Also set auto-clear wdt.

I'd 'aimed' for the simplest possible program displaying a sleep / wake cycle - but different MCUs always make life a little harder :-(

How do you measure the current for the power consumption? Programming not electronics is my background....

Martin

chipfryer27
Valued Contributor
Posts: 1605
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 357 times
Been thanked: 565 times

Re: "sleep" inside a loop

Post by chipfryer27 »

Hi

No hardware to play with :(

This is a follow up post to a previous one and two different PICs have been used, each with slightly different features, so I can only be generic in what little I can offer.

When sleeping there should be no loss of "data" and any ouputs will remain at the state they were in before the PIC goes to sleep (so make sure anything not needed is killed including WPU if used). When the chip awakens it should just start where it left off, but good practise is to include a NOP straight after sleep.

The PIC can wake up by IOC, WDT or Timer1, none of which will cause loss of data as the PIC will carry on from where it went to sleep. Note that WDT action by itself (if not used to wake from sleep) will cause the PIC to jump back to the beginning of your chart so be sure to check Clear WDT in properties.

Timer1, depending on PIC, can be configured to run when sleeping from an internal 32kHz source or external. When the timer "rolls over" it will wake the chip. Again no loss of data.

You could have within your sleep loop something like

Loop
Sleep
NOP
Count=Count+1
If Count=x do something useful
Loop

In theory using the above you could "sleep" for an exceptionally long time depending on frequency / scaling / count etc before actually doing anything useful.

A simple test to see if you are actually reseting due to WDT is to include at the beginning of your chart a simple 1-second flasher, perhaps flashing three times before you actually do anything like initialising or enabling interupts etc. If it flashes you have reset.

Regards

PS
My current background is a thunderstorm, awaiting the arrival of another typhoon :lol:

mnfisher
Valued Contributor
Posts: 1548
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 138 times
Been thanked: 737 times

Re: "sleep" inside a loop

Post by mnfisher »

Wales - watching the RAF training?

Or somewhere more exotic?

chipfryer27
Valued Contributor
Posts: 1605
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 357 times
Been thanked: 565 times

Re: "sleep" inside a loop

Post by chipfryer27 »

Haha...

Asia and about two weeks ago we had super-typhoon KONG... Now that's a name. None of our "a bit windy today Alice" or other namby-pamby names. Run away... KONG is coming.....

Government insists not to go to work, so the locals go to the KTV....:)

Regards

mnfisher
Valued Contributor
Posts: 1548
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 138 times
Been thanked: 737 times

Re: "sleep" inside a loop

Post by mnfisher »

Sounds exciting..

Hang on to your hat!

Another point - some pics reset on wdt overflow (though not the 12f1680 as far as I can tell.)
Then it is possible to check a flag on restart and follow a different action if required)

And what is a KTV?

Martin

ELECTRONICA67
Posts: 143
Joined: Wed Dec 02, 2020 3:59 pm
Has thanked: 26 times
Been thanked: 9 times

Re: "sleep" inside a loop

Post by ELECTRONICA67 »

Hello everyone, thank you very much for your opinions, comments and help,

I'm still looking for a solution, among all the help in the forum I've found a lot of information and examples, I tried a program in my circuit that I think was published by Chipfreyer, it's a good example of how to use sleep, it works perfectly in hardware, I changed the chip for the PIC12F1840, it works great, but when adding TMR1 according to what I understand from the data sheet, this timer keeps running when the sleep command comes into operation in the program, I added a 24 hour clock, I did tests starting it with the data saved in eeprom and other options, however in the program after sleep a led turns on for 500mS, it turns off for another 500 mS, I included an option "if MINUTES=1 activate led", but not work when the minute is reached, I would appreciate if you could take a look? I have the hardware set up to do tests, I would appreciate some ideas so that TMR1 keeps working and I can have the Timer data.

Best Regards
Enrique
Attachments
WDTwithTMR1.fcfx
not work
(13.82 KiB) Downloaded 106 times
WDT.fcfx
working
(12.26 KiB) Downloaded 102 times

chipfryer27
Valued Contributor
Posts: 1605
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 357 times
Been thanked: 565 times

Re: "sleep" inside a loop

Post by chipfryer27 »

Hi

"And what is a KTV?"

If you see that sign it means Karaoke bar, but not quite what you may expect...

The bar is sub divided into individual private rooms, which you hire by the hour. Drinks are brought on demand by waitresses. It's as much fun to me as it may sound to you.... <s>

I don't see any reason to use both WDT and Timer1.

With the 1840 you can use WDT to get the chip to sleep from 1mS to 256 seconds and this will most likely be your best option.

Just say you set it for 4s (or other multiple that can be exactly divided into 60 / 3600 / 86400). This means every four seconds it awakes. If when it awakes and increments a counter, when the counter reaches 15, one minute would have elapsed. A count of 75 would be five minutes etc.

You could test for the count value and if reached "do something", if not then just go back to sleep.

With a sleep of 128 seconds, a count of 675 would be 24hrs... I'm sure you get the idea.

Using Timer1 would most likely consume more power as it would interrupt far more frequently. However, and this really does depend on your project, it could be more accurate over a given time frame. Using Timer1, when the count reaches your target value it will "do something", but unless you disable the interrupt then your count would still continue in the background. Using WDT once the count is reached the time taken to process before sleeping again is "lost".

WDT cannot measure elapsed "running time" only the time asleep. Timer1 can measure elapsed time from when enabled to disabled which includes sleep and "running time".

I guess it is a trade off of sorts.

Do note I'm just mentioning the above and for most projects the accuracy isn't really that important.

Loop
Sleep
NOP
Count=Count+1
If Count = x "do something"
Loop

Using WDT the above could be run every 1mS to every 256 seconds depending on WDT settings.

Using Timer-1

Enable Interrupt on Timer-1 *You would need an ISR to increment your counter*
Loop
Sleep
NOP
If Count = x "do something"
Loop

With Timer-1 it would be run many times per second.

Regards

Post Reply