Page 1 of 6

"sleep" inside a loop

Posted: Sat Nov 09, 2024 7:11 pm
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

Re: "sleep" inside a loop

Posted: Mon Nov 11, 2024 7:43 pm
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

Re: "sleep" inside a loop

Posted: Tue Nov 12, 2024 7:37 am
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?

Re: "sleep" inside a loop

Posted: Tue Nov 12, 2024 2:09 pm
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

Re: "sleep" inside a loop

Posted: Tue Nov 12, 2024 4:35 pm
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

Re: "sleep" inside a loop

Posted: Tue Nov 12, 2024 11:30 pm
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

Re: "sleep" inside a loop

Posted: Wed Nov 13, 2024 4:58 am
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

Re: "sleep" inside a loop

Posted: Wed Nov 13, 2024 5:19 pm
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

Re: "sleep" inside a loop

Posted: Wed Nov 13, 2024 9:19 pm
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

Re: "sleep" inside a loop

Posted: Wed Nov 13, 2024 10:13 pm
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