Using Watchdog Timer for long delays

Discussion in 'Embedded Systems and Microcontrollers' started by Guinness1759, Jul 11, 2011.

  1. Guinness1759

    Thread Starter Member

    Dec 10, 2010
    I'm need my microcontroller to wakeup every 20-30 minutes. During the idle times it will be in sleep mode. I've successfully got the watchdog timer to start the circuit using the watchdog timer interrupt.

    The datasheet for my microcontroller is here:

    The WDTPS only allows for 256 second delay. See page 99. How can I make a longer delay?

    I thought about putting 5 of these sleep commands in a row in the code, but the extra power to restart I think would be too much.
  2. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    Personally, I wouldn't use the WDT as a period timer. It is generally used to get your code out of trouble (like an infinite loop), and its timing accuracy is not all that good.

    I'd use a 32khz watch crystal on timer 1. This will use very little power in sleep mode.

    With a 8:1 prescaler, your code will wake every 16 seconds. Then in code, you would update a counting register and go back to sleep until your 20 to 30 minutes has expired. You can do this using your 32khz clock (on timer 1), again using very little power.

    Once you've determined the processor slept during the proper amount of time, you can then wake it fully, change oscillators, and run at full speed till the next sleep period.
    Guinness1759 likes this.
  3. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    If you don't need the precise timing offered by a 32kHz xtal then juts use multiple WDT delays.

    The amount of power needed to wake up and increment a variable is incredibly small, compared to 256 seconds of sleep. For example if your powered-up time is 100uS that is only 1/2560000 of the total sleep time and the increase in power used will be practically nothing.
    Guinness1759 likes this.
  4. Guinness1759

    Thread Starter Member

    Dec 10, 2010
    I don't need precise time, so you're suggesting I could do this


    And it will wakeup only to go into another sleep and not consume much power at all? I guess that is the same as doing a for loop with sleep in the loop and incrementing a variable.
  5. guitarguy12387

    Active Member

    Apr 10, 2008
    Definitely NOT! If you're in a long loop, you're not putting the micro in sleep mode.

    Here is the suggestion:

    - Use the 'slowest' regular timer you have on the chip that can run in sleep mode (by 'slowest', i mean that you should account for 16-bit vs 8 bit counters etc)
    - Enable interrupts on overflow for that timer
    - Put the micro to sleep
    - When the timer overflows, wake up!
    - In the ISR, increment a counter register (as big as you want until you get the desired timing)
    - if (counter == WAKE_UP_TIME) {// do stuff; reset counter}
    - clear timer interrupt flag
    - go back to sleep
    Guinness1759 likes this.
  6. Markd77

    Senior Member

    Sep 7, 2009
    Looking at the datasheet for that PIC there isn't much power difference between sleep mode and using the LFINTOSC which runs at 31kHz - about 60μA for LFINTOSC and about 40μA for sleep mode + watchdog timer.
    Guinness1759 likes this.