"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

Flowcode v10 "sleep" inside a loop

Post by ELECTRONICA67 »

Hello

Is it possible to maintain a "sleep" inside a loop :?: That the program keeps doing its function until the expected is fulfilled and momentarily exits the loop to reset the necessary variables, and returns to the loop where the "sleep" is.
Previously I made a small program using "sleep", it considerably lowered the consumption in the batteries, it used 4 AA batteries and it was working constantly for 1 year. Now I try the same, but in that program the PIC did its routine and when it finished it restarted. Now with this program it affects me that the program restarts from the beginning when the PIC wakes up, I added a command at several points, MX_CLEAR_WATCHDOG; I thought that with this I eliminated the restart and simply continued in the loop, re-entering "sleep" and continuing with the routine:

Loop
WDTCONbits. WDTPS = 0b01010; (1 second)
SLEEP();
NOP();
MX_CLEAR_WATCHDOG; (CLEARS OR REMOVES SLEEP)
Program (until a condition is met and sends "go to")
Loop

I tried to make changes in timings, in the PIC configuration, commands, and always in all cases the program crashes or restarts, I need to keep the values ​​of the variables; any help is appreciated.
I include the program.

Best Regards
Enrique
Attachments
IOC_1840test.fcfx
(21.98 KiB) Downloaded 152 times

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 managed to get the PIC to enter "sleep", in fact it wakes up when the pulses are detected in IOC, but the tmr1 that contains the elapsed minutes and hours is reset to zero 0 and I can't keep the time count when the PIC is in "sleep" mode.

How do I get the tmr1 clock not to lose data during sleep :?:

Any help please. . .or any ideas

I appreciate it in advance.
Enrique

medelec35
Matrix Staff
Posts: 1994
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 630 times
Been thanked: 666 times

Re: "sleep" inside a loop

Post by medelec35 »

Hi Enrique.
Just before the sleep command, what about saving variables to EEPROM and set a variable e.g. called FlagEEPROM.
Then you can load the variables via EEPROM back up when the pic awakes.
To check if the pic has awaken by the sleep or just normally started, the PD bit of the status register could be read.
If = 1, don't restore from EEPROM as a normal poer up has occurred e.g power first applied.
If = 0, restore from EEPROM only if FlagEEPROM = 0. and then prevent further reads and restore by setting FlagEEPROM to 1

I have not tried this myself, so I can't say for 100% it will work.

The only issue I can see is that clearing the watchdog causes PD bit to be 1.
Hopefully the exception is if pic wakes up from sleep?
Martin

chipfryer27
Valued Contributor
Posts: 1607
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

I think maybe I'm reading wrong, but are you

A) wanting your count to continue whilst sleeping

Or

B) wake at a defined period (e.g. 1 second) and update counters before sleeping again?

Depending on chip both are possible. Option B would consume more though.

Regards

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 »

Hello Martin and Lain :)

I really appreciate your help. I'm going to start implementing what Martin suggested. I'll send results. . . .and in Lain's question, it would be A) that the counting continues while sleeping.

Save energy but don't lose data.

Saludos

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 »

Hello Martin and Lain

I just finished the program, following Martin's recommendations, it was simple and easy, I added Flash eeprom to save the data and update it as it goes into sleep and resets. I didn't get to try it today.

Tomorrow I will do some tests with it.

Saludos
Attachments
IOC_1840test.fcfx
(31.74 KiB) Downloaded 139 times

chipfryer27
Valued Contributor
Posts: 1607
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

That chip supports Timer1 running whilst sleeping. That may offer better savings but I've no hardware at present to play with.

Regards

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 »

Hello, good morning

I just performed the test using FlashEEPROM, it does the function of putting the circuit to sleep, but it remains the same, it resets and does not perform the function, maybe I am wrong at some point, I will continue trying to achieve the objective
Attachments
sle.jpeg
sle.jpeg (149.25 KiB) Viewed 6280 times

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

Re: "sleep" inside a loop

Post by mnfisher »

I thought it odd that you didn't get the values saved - they should be on waking from a sleep.

I experimented with a PIC16lf15313 - and used the wdt to wake from sleep. Obviously the time asleep will depend on the clock speed - the MCU I am using has a INTOSC of 32MHz - and with the wdt set to 1:131072 gives a sleep of 4.24 seconds. Note that I just toggle a pin (A5 in my case) - but it retains the current state and toggles it on each 'wake'

I did try getting it to wake on TMR0 and TMR1 but failed :-( though it should be possible.

The wdt can be set to various times for example (1:32 (set in build->properties) gives 1.10ms (ooops had delay 'lingering' from some tests).

Martin
Attachments
picsleep.fcfx
(8.06 KiB) Downloaded 137 times

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

Re: "sleep" inside a loop

Post by mnfisher »

A little more experimentation - using a WDT value of 1:1024 (giving a more oscilloscope friendly 33.3ms pulse) - shows that whatever value I set the internal oscillator value HFINTOSC (32Mhz) -> 1Mhz -> 32kHz doesn't seem to affect the WDT interrupt time.

By my calculations - the longest WDT interval would be ~270s

Note that I have WDT enabled in sleep - in your code - you have it disabled in sleep (and I guess the 'enabled' option would be the one to pick)

How do you measure the power consumption ? - My USB power meter shows '0' very easily - it 'should' be possible using a multimeter?

Martin

Post Reply