News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Sleep command and Watchdog timer configuration bits?

Started by trastikata, May 09, 2023, 07:22 AM

Previous topic - Next topic

trastikata

Hello,

It is not clear to me how the compiler behaves when using the Sleep command and the Watchdog is disabled in the Configuration bits?

QuoteSleep uses the Watchdog Timer so it is independent of the oscillator frequency

Another question here - does the compiler take into consideration the Watchdog prescaller as set in the Configuration bits? 

TimB

I would imagine its a legacy thing. In the long distant past Sleep may have been more intelligent. But with the way Mchip has changed stuff I doubt it will try to do so now.

Sleep is from memory a Pic command and the compiler just brings it forward. On my temperature meter project I used sleep to reduce the power consumption as much as possible.

It basically was just tell the pic what the time period was and then Sleep

            WDTCON0 = %00000011                                    ' sleep for 2ms
            Sleep

Sorry it did not answer all your questions

Here is some code I wrote to keep a software clock running so timers etc would still run properly


Proc SleepFor2ms()
    Dim bTimerCortCnt As Byte

    If xBPWMRequired = cFalse Then      ; No sleeping while PWM is active

            fTmr_Enable = cFalse                                    ; First stop the timer
            Inc bTimerCortCnt
            If bTimerCortCnt >=4 Then
                bTimerCortCnt = 0
                dMS_Timer = dMS_Timer + 3                          ; Correction value
            Else
                dMS_Timer = dMS_Timer + 2
            EndIf


            WDTCON0 = %00000011                                    ' sleep for c2ms
            Sleep
            Nop
            fTmr_Enable = cTrue                                    ; Re start the timer
            WDTCON0 = 0

    Else
        DelayMS 2
    EndIf

EndProc


trastikata

#2
Quote from: trastikata on May 09, 2023, 07:22 AMIt is not clear to me how the compiler behaves when using the Sleep command and the Watchdog is disabled in the Configuration bits?
Another question here - does the compiler take into consideration the Watchdog pre/post scaler as set in the Configuration bits? 

Thanks TimB, I got to the same conclusion myself after some testing. To clarify:

The Sleep command simply counts the number of Watchdog timer resets, thus:

- The Watchdog Timer period depends on the Watchdog Timer source and frequency
- The Watchdog Timer pre/post scaler needs to be properly set for the desired reset period considering the Watchdog Timer source and frequency
- The Watchdog must be enabled in software before issuing the Sleep command
- The Watchdog must be disabled in software after the Watchdog Timer reset if not used for other than Sleep command

So, for example PIC18F25K50 (from datasheet)
- gets its Watchdog Timer clock from the low power INTRC oscillator 32.5 kHz - equivalent of clock period of 0.0307692308 ms.
- there's a fixed WDT Counter/Divider of 128 - thus 0.0307692308 * 128 = 3.94 ms for Watchdog Timer reset
- using the Programmable Postscaler 1:1 to 1:32,768, if set to 256, the Watchdog Timer reset will occur in 256*3.94ms = 1009 ms or roughly 1s
- issuing Sleep 120 command will count 120 resets before continuing code execution
- This means the MCU will wake up 120 times for a brief periods to increment the Sleep command counter before it continues code execution.

Thus if we need about 2 minutes of low-power, the best way to do it is if we set-up the Programmable Postscaler to 32768 i.e. Watchdog Timer reset will occur in 32768 * 0.00394s = 129s and issue Sleep 1 command.

top204

The Sleep command was first implemented in the compiler when the devices did not have much manipulation of the watchdog ratio time, and the default was 1:128. So this is what the compiler's default config fuses are set for.

The Sleep command with a parameter after it, counts the amount of times the device has woken, so it does not keep the device in low power mode. The newer devices have a lot more control over the watchdog timer, and some have software control over it via SFRs, so it is always better to examine the datasheet and see if the device can be put to deep sleep with an element of control over its timings.

Without a parameter after the Sleep command, the compiler issues the Sleep mnemonic, so the device goes into deep sleep until a peripheral wakes up the device, or the watchdog timer overflows and wakes it up. The newer devices also have the PMD SFRs so that peripherals that are not used in a program can be permanently disabled when the device is in sleep mode, reducing the current consumption even more.